This repository was archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.py
More file actions
273 lines (245 loc) · 9.82 KB
/
Copy pathsingleton.py
File metadata and controls
273 lines (245 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#! /usr/bin/env python
"""
Copyright (c) 2017 Andrew Azarov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import sys
import os
import tempfile
import logging
import errno
import unittest
LIN = None
WIN = None
BSD = None
try:
import fcntl
LIN = 1
except ImportError:
# Not *NIX
pass
try:
import msvcrt
WIN = 1
except ImportError:
# Not Windows
pass
BSD = hasattr(os, 'O_EXLOCK')
def pid_exists(pid):
"""Check whether pid exists in the current process table."""
# http://stackoverflow.com/a/23409343/2010538
# http://stackoverflow.com/a/28065945/2010538
if os.name != 'nt':
import errno
if pid <= 0:
return False
try:
os.kill(pid, 0)
except OSError as e:
return e.errno == errno.EPERM
else:
return True
else:
import ctypes
kernel32 = ctypes.windll.kernel32
HANDLE = ctypes.c_void_p
DWORD = ctypes.c_ulong
LPDWORD = ctypes.POINTER(DWORD)
class ExitCodeProcess(ctypes.Structure):
_fields_ = [('hProcess', HANDLE),
('lpExitCode', LPDWORD)]
PROCESS_QUERY_INFORMATION = 0x1000
process = kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
if not process:
return False
ec = ExitCodeProcess()
out = kernel32.GetExitCodeProcess(process, ctypes.byref(ec))
if not out:
err = kernel32.GetLastError()
if err == 5: # Look after this change, maybe previous line was just a skip
# Access is denied.
logger.warning("Access is denied to get pid info.")
kernel32.CloseHandle(process)
return False
elif bool(ec.lpExitCode):
# print ec.lpExitCode.contents
# There is an exit code, it quit
kernel32.CloseHandle(process)
return False
# No exit code, it's running.
kernel32.CloseHandle(process)
return True
class SingletException(BaseException):
pass
class Singlet:
"""
Based on tendo.singleton
This module provides a Singlet() class which atomically creates a lock file
containing PID of the running process to prevent parallel execution of the
same program. In case there is any other instance running already a
`SingletException` will be thrown. The class will throw `IOError` and
`OSError` in case there are hardware or OS level corruption.
>>> from singletony import Singlet
... me = Singlet(filename="test.lock", path="/tmp")
This is helpful for both daemons and simple crontab scripts. Works on *NIX
and Windows OS's.
By default this creates a lock file with a filename based on the
full path to the script file.
"""
def __init__(self, filename=None, path=None):
if not filename:
filename = os.path.basename(sys.argv[0]) + '.lock'
if not path:
path = tempfile.gettempdir()
self.lockfile = os.path.normpath(path + '/' + filename)
self.pid = str(os.getpid())
self.fd = None
logger.info("Singlet lockfile: " + self.lockfile)
try:
# If advance UNIX system with atomic locks on open
if BSD:
self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL |
os.O_RDWR | os.O_EXLOCK | os.O_NONBLOCK)
else:
self.fd = os.open(self.lockfile, os.O_CREAT |
os.O_EXCL | os.O_RDWR)
if WIN:
msvcrt.locking(self.fd, msvcrt.LK_NBRLCK, 65)
if LIN:
fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except (IOError, OSError) as e:
if e.errno in (errno.EPERM, errno.EWOULDBLOCK):
logger.error(
"Another instance is already running, quitting.")
self.fd = None
raise SingletException(
"Another instance is already running, quitting.")
elif e.errno == errno.EEXIST:
# Workaround for Linux/Windows mostly which lacks atomic
# locking on open
self.fd = os.open(self.lockfile, os.O_RDWR)
try:
if WIN:
msvcrt.locking(self.fd, msvcrt.LK_NBRLCK, 65)
if LIN:
fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except (IOError, OSError) as e:
# Some entity has been faster than us if we WOULDBLOCK
if e.errno in (errno.EPERM, errno.EWOULDBLOCK):
logger.error(
"Another instance is already running, quitting.")
self.fd = None
raise SingletException(
"Another instance is already running, quitting.")
else:
logger.exception("Something went wrong")
self.fd = None
raise
else:
logger.exception("Something went wrong")
# Anything else is horribly wrong, we need to raise to the
# upper level so the following code in this try clause
# won't execute.
self.fd = None
raise
# By this moment the file should be locked or we should exit so
# there should realistically be no error here, or it can raise
if self.oldpid_is_running():
try:
os.close(self.fd)
except OSError as e:
# We shouldn't raise here anything. Because we don't
# actually care
logger.exception("Interesting state")
logger.error(
"Another instance is already running, quitting.")
self.fd = None
raise SingletException(
"Another instance is already running, quitting.")
# Barring any OS/Hardware issue this musn't throw anything. But
# even if it throws we should raise it because it means we
# shouldn't run and some serious problem is already happening. So
# no, I'm not going to escape this one in try/except.
if hasattr(os, "ftruncate"):
os.ftruncate(self.fd, 0) # Erase
os.lseek(self.fd, 0, 0) # Rewind
# Write PID with WIN fix
os.write(self.fd, self.pid.rjust(64, "#"))
if hasattr(os, "fsync"):
os.fsync(self.fd)
def oldpid_is_running(self):
# Some OS actually recycle pids within same range so we
# have to check whether oldPid is not the new one so this
# won't fire up (*BSD).
# If not and it is still running we'd rather actually exit
# right here.
oldPid = os.read(self.fd, 64).strip().lstrip("#")
return (oldPid and int(oldPid) > 0 and int(oldPid) != int(self.pid) and pid_exists(int(oldPid)))
def __del__(self):
# If we are not initialized don't run the clause
if self.fd:
try:
os.close(self.fd)
os.unlink(self.lockfile)
except:
logger.exception("Unknown issue on exit")
raise
def f(name):
from time import sleep
tmp = logger.level
logger.setLevel(logging.CRITICAL) # we do not want to see the warning
try:
me2 = Singlet(filename=name) # noqa
sleep(1)
except SingletException:
sys.exit(-1)
logger.setLevel(tmp)
pass
class testSingleton(unittest.TestCase):
def test_1(self):
me = Singlet(filename="test-1")
save = me.lockfile
del me # now the lock should be removed
assert not os.path.isfile(save)
def test_2(self):
p = Process(target=f, args=("test-2",))
p.start()
p.join()
# the called function should succeed
assert p.exitcode == 0, "%s != 0" % p.exitcode
def test_3(self):
me = Singlet(filename="test-3") # noqa -- me should still kept
p = Process(target=f, args=("test-3",))
p.start()
p.join()
# the called function should fail because we already have another
# instance running
assert p.exitcode != 0, "%s != 0 (2nd execution)" % p.exitcode
# note, we return -1 but this translates to 255 meanwhile we'll
# consider that anything different from 0 is good
p = Process(target=f, args=("test-3",))
p.start()
p.join()
# the called function should fail because we already have another
# instance running
assert p.exitcode != 0, "%s != 0 (3rd execution)" % p.exitcode
logger = logging.getLogger("singletony")
logger.addHandler(logging.StreamHandler())
if __name__ == "__main__":
from multiprocessing import Process
logger.setLevel(logging.DEBUG)
unittest.main()