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