Show More
@@ -1,142 +1,142 b'' | |||||
1 | import os |
|
1 | import os | |
2 | import sys |
|
2 | import sys | |
3 | import time |
|
3 | import time | |
4 | import errno |
|
4 | import errno | |
5 |
|
5 | |||
6 | from warnings import warn |
|
6 | from warnings import warn | |
7 | from multiprocessing.util import Finalize |
|
7 | from multiprocessing.util import Finalize | |
8 |
|
8 | |||
9 | from rhodecode import __platform__, PLATFORM_WIN |
|
9 | from rhodecode import __platform__, PLATFORM_WIN | |
10 |
|
10 | |||
11 | if __platform__ in PLATFORM_WIN: |
|
11 | if __platform__ in PLATFORM_WIN: | |
12 | import ctypes |
|
12 | import ctypes | |
13 |
|
13 | |||
14 | def kill(pid): |
|
14 | def kill(pid, sig): | |
15 | """kill function for Win32""" |
|
15 | """kill function for Win32""" | |
16 | kernel32 = ctypes.windll.kernel32 |
|
16 | kernel32 = ctypes.windll.kernel32 | |
17 | handle = kernel32.OpenProcess(1, 0, pid) |
|
17 | handle = kernel32.OpenProcess(1, 0, pid) | |
18 | return (0 != kernel32.TerminateProcess(handle, 0)) |
|
18 | return (0 != kernel32.TerminateProcess(handle, 0)) | |
19 |
|
19 | |||
20 | else: |
|
20 | else: | |
21 | kill = os.kill |
|
21 | kill = os.kill | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | class LockHeld(Exception): |
|
24 | class LockHeld(Exception): | |
25 | pass |
|
25 | pass | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | class DaemonLock(object): |
|
28 | class DaemonLock(object): | |
29 | """daemon locking |
|
29 | """daemon locking | |
30 | USAGE: |
|
30 | USAGE: | |
31 | try: |
|
31 | try: | |
32 | l = DaemonLock(desc='test lock') |
|
32 | l = DaemonLock(desc='test lock') | |
33 | main() |
|
33 | main() | |
34 | l.release() |
|
34 | l.release() | |
35 | except LockHeld: |
|
35 | except LockHeld: | |
36 | sys.exit(1) |
|
36 | sys.exit(1) | |
37 | """ |
|
37 | """ | |
38 |
|
38 | |||
39 | def __init__(self, file=None, callbackfn=None, |
|
39 | def __init__(self, file=None, callbackfn=None, | |
40 | desc='daemon lock', debug=False): |
|
40 | desc='daemon lock', debug=False): | |
41 |
|
41 | |||
42 | self.pidfile = file if file else os.path.join( |
|
42 | self.pidfile = file if file else os.path.join( | |
43 | os.path.dirname(__file__), |
|
43 | os.path.dirname(__file__), | |
44 | 'running.lock') |
|
44 | 'running.lock') | |
45 | self.callbackfn = callbackfn |
|
45 | self.callbackfn = callbackfn | |
46 | self.desc = desc |
|
46 | self.desc = desc | |
47 | self.debug = debug |
|
47 | self.debug = debug | |
48 | self.held = False |
|
48 | self.held = False | |
49 | #run the lock automatically ! |
|
49 | #run the lock automatically ! | |
50 | self.lock() |
|
50 | self.lock() | |
51 | self._finalize = Finalize(self, DaemonLock._on_finalize, |
|
51 | self._finalize = Finalize(self, DaemonLock._on_finalize, | |
52 | args=(self, debug), exitpriority=10) |
|
52 | args=(self, debug), exitpriority=10) | |
53 |
|
53 | |||
54 | @staticmethod |
|
54 | @staticmethod | |
55 | def _on_finalize(lock, debug): |
|
55 | def _on_finalize(lock, debug): | |
56 | if lock.held: |
|
56 | if lock.held: | |
57 | if debug: |
|
57 | if debug: | |
58 | print 'leck held finilazing and running lock.release()' |
|
58 | print 'leck held finilazing and running lock.release()' | |
59 | lock.release() |
|
59 | lock.release() | |
60 |
|
60 | |||
61 | def lock(self): |
|
61 | def lock(self): | |
62 | """ |
|
62 | """ | |
63 | locking function, if lock is present it |
|
63 | locking function, if lock is present it | |
64 | will raise LockHeld exception |
|
64 | will raise LockHeld exception | |
65 | """ |
|
65 | """ | |
66 | lockname = '%s' % (os.getpid()) |
|
66 | lockname = '%s' % (os.getpid()) | |
67 | if self.debug: |
|
67 | if self.debug: | |
68 | print 'running lock' |
|
68 | print 'running lock' | |
69 | self.trylock() |
|
69 | self.trylock() | |
70 | self.makelock(lockname, self.pidfile) |
|
70 | self.makelock(lockname, self.pidfile) | |
71 | return True |
|
71 | return True | |
72 |
|
72 | |||
73 | def trylock(self): |
|
73 | def trylock(self): | |
74 | running_pid = False |
|
74 | running_pid = False | |
75 | if self.debug: |
|
75 | if self.debug: | |
76 | print 'checking for already running process' |
|
76 | print 'checking for already running process' | |
77 | try: |
|
77 | try: | |
78 | pidfile = open(self.pidfile, "r") |
|
78 | pidfile = open(self.pidfile, "r") | |
79 | pidfile.seek(0) |
|
79 | pidfile.seek(0) | |
80 | running_pid = int(pidfile.readline()) |
|
80 | running_pid = int(pidfile.readline()) | |
81 |
|
81 | |||
82 | pidfile.close() |
|
82 | pidfile.close() | |
83 |
|
83 | |||
84 | if self.debug: |
|
84 | if self.debug: | |
85 | print ('lock file present running_pid: %s, ' |
|
85 | print ('lock file present running_pid: %s, ' | |
86 | 'checking for execution') % running_pid |
|
86 | 'checking for execution') % running_pid | |
87 | # Now we check the PID from lock file matches to the current |
|
87 | # Now we check the PID from lock file matches to the current | |
88 | # process PID |
|
88 | # process PID | |
89 | if running_pid: |
|
89 | if running_pid: | |
90 | try: |
|
90 | try: | |
91 | kill(running_pid, 0) |
|
91 | kill(running_pid, 0) | |
92 | except OSError, exc: |
|
92 | except OSError, exc: | |
93 | if exc.errno in (errno.ESRCH, errno.EPERM): |
|
93 | if exc.errno in (errno.ESRCH, errno.EPERM): | |
94 | print ("Lock File is there but" |
|
94 | print ("Lock File is there but" | |
95 | " the program is not running") |
|
95 | " the program is not running") | |
96 | print "Removing lock file for the: %s" % running_pid |
|
96 | print "Removing lock file for the: %s" % running_pid | |
97 | self.release() |
|
97 | self.release() | |
98 | else: |
|
98 | else: | |
99 | raise |
|
99 | raise | |
100 | else: |
|
100 | else: | |
101 | print "You already have an instance of the program running" |
|
101 | print "You already have an instance of the program running" | |
102 | print "It is running as process %s" % running_pid |
|
102 | print "It is running as process %s" % running_pid | |
103 | raise LockHeld() |
|
103 | raise LockHeld() | |
104 |
|
104 | |||
105 | except IOError, e: |
|
105 | except IOError, e: | |
106 | if e.errno != 2: |
|
106 | if e.errno != 2: | |
107 | raise |
|
107 | raise | |
108 |
|
108 | |||
109 | def release(self): |
|
109 | def release(self): | |
110 | """releases the pid by removing the pidfile |
|
110 | """releases the pid by removing the pidfile | |
111 | """ |
|
111 | """ | |
112 | if self.debug: |
|
112 | if self.debug: | |
113 | print 'trying to release the pidlock' |
|
113 | print 'trying to release the pidlock' | |
114 |
|
114 | |||
115 | if self.callbackfn: |
|
115 | if self.callbackfn: | |
116 | #execute callback function on release |
|
116 | #execute callback function on release | |
117 | if self.debug: |
|
117 | if self.debug: | |
118 | print 'executing callback function %s' % self.callbackfn |
|
118 | print 'executing callback function %s' % self.callbackfn | |
119 | self.callbackfn() |
|
119 | self.callbackfn() | |
120 | try: |
|
120 | try: | |
121 | if self.debug: |
|
121 | if self.debug: | |
122 | print 'removing pidfile %s' % self.pidfile |
|
122 | print 'removing pidfile %s' % self.pidfile | |
123 | os.remove(self.pidfile) |
|
123 | os.remove(self.pidfile) | |
124 | self.held = False |
|
124 | self.held = False | |
125 | except OSError, e: |
|
125 | except OSError, e: | |
126 | if self.debug: |
|
126 | if self.debug: | |
127 | print 'removing pidfile failed %s' % e |
|
127 | print 'removing pidfile failed %s' % e | |
128 | pass |
|
128 | pass | |
129 |
|
129 | |||
130 | def makelock(self, lockname, pidfile): |
|
130 | def makelock(self, lockname, pidfile): | |
131 | """ |
|
131 | """ | |
132 | this function will make an actual lock |
|
132 | this function will make an actual lock | |
133 |
|
133 | |||
134 | :param lockname: acctual pid of file |
|
134 | :param lockname: acctual pid of file | |
135 | :param pidfile: the file to write the pid in |
|
135 | :param pidfile: the file to write the pid in | |
136 | """ |
|
136 | """ | |
137 | if self.debug: |
|
137 | if self.debug: | |
138 | print 'creating a file %s and pid: %s' % (pidfile, lockname) |
|
138 | print 'creating a file %s and pid: %s' % (pidfile, lockname) | |
139 | pidfile = open(self.pidfile, "wb") |
|
139 | pidfile = open(self.pidfile, "wb") | |
140 | pidfile.write(lockname) |
|
140 | pidfile.write(lockname) | |
141 | pidfile.close |
|
141 | pidfile.close | |
142 | self.held = True |
|
142 | self.held = True |
General Comments 0
You need to be logged in to leave comments.
Login now