Mercurial > kallithea
comparison rhodecode/lib/pidlock.py @ 1194:07963dd1f0f1 beta
fixes for issue #133
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sat, 02 Apr 2011 00:42:17 +0200 |
parents | 5cc96df705b9 |
children | c1516b35f91d |
comparison
equal
deleted
inserted
replaced
1193:523382549c45 | 1194:07963dd1f0f1 |
---|---|
1 import os, time | 1 import os, time |
2 import sys | 2 import sys |
3 from warnings import warn | 3 from warnings import warn |
4 from multiprocessing.util import Finalize | 4 from multiprocessing.util import Finalize |
5 import errno | 5 import errno |
6 | |
7 from rhodecode import __platform__, PLATFORM_WIN | |
8 | |
9 if __platform__ in PLATFORM_WIN: | |
10 import ctypes | |
11 def kill(pid): | |
12 """kill function for Win32""" | |
13 kernel32 = ctypes.windll.kernel32 | |
14 handle = kernel32.OpenProcess(1, 0, pid) | |
15 return (0 != kernel32.TerminateProcess(handle, 0)) | |
16 | |
17 else: | |
18 kill = os.kill | |
6 | 19 |
7 class LockHeld(Exception):pass | 20 class LockHeld(Exception):pass |
8 | 21 |
9 | 22 |
10 class DaemonLock(object): | 23 class DaemonLock(object): |
56 print 'checking for already running process' | 69 print 'checking for already running process' |
57 try: | 70 try: |
58 pidfile = open(self.pidfile, "r") | 71 pidfile = open(self.pidfile, "r") |
59 pidfile.seek(0) | 72 pidfile.seek(0) |
60 running_pid = int(pidfile.readline()) | 73 running_pid = int(pidfile.readline()) |
61 | 74 |
62 pidfile.close() | 75 pidfile.close() |
63 | 76 |
64 if self.debug: | 77 if self.debug: |
65 print 'lock file present running_pid: %s, checking for execution'\ | 78 print 'lock file present running_pid: %s, checking for execution'\ |
66 % running_pid | 79 % running_pid |
67 # Now we check the PID from lock file matches to the current | 80 # Now we check the PID from lock file matches to the current |
68 # process PID | 81 # process PID |
69 if running_pid: | 82 if running_pid: |
70 try: | 83 try: |
71 os.kill(running_pid, 0) | 84 kill(running_pid, 0) |
72 except OSError, exc: | 85 except OSError, exc: |
73 if exc.errno in (errno.ESRCH, errno.EPERM): | 86 if exc.errno in (errno.ESRCH, errno.EPERM): |
74 print "Lock File is there but the program is not running" | 87 print "Lock File is there but the program is not running" |
75 print "Removing lock file for the: %s" % running_pid | 88 print "Removing lock file for the: %s" % running_pid |
76 self.release() | 89 self.release() |
77 else: | 90 else: |
78 raise | 91 raise |
79 else: | 92 else: |
80 print "You already have an instance of the program running" | 93 print "You already have an instance of the program running" |
81 print "It is running as process %s" % running_pid | 94 print "It is running as process %s" % running_pid |
82 raise LockHeld() | 95 raise LockHeld() |
83 | 96 |
84 except IOError, e: | 97 except IOError, e: |
85 if e.errno != 2: | 98 if e.errno != 2: |
86 raise | 99 raise |
87 | 100 |
88 def release(self): | 101 def release(self): |
89 """releases the pid by removing the pidfile | 102 """releases the pid by removing the pidfile |
90 """ | 103 """ |
91 if self.debug: | 104 if self.debug: |
92 print 'trying to release the pidlock' | 105 print 'trying to release the pidlock' |
93 | 106 |
94 if self.callbackfn: | 107 if self.callbackfn: |
95 #execute callback function on release | 108 #execute callback function on release |
96 if self.debug: | 109 if self.debug: |
97 print 'executing callback function %s' % self.callbackfn | 110 print 'executing callback function %s' % self.callbackfn |
98 self.callbackfn() | 111 self.callbackfn() |