forked from rlowrance/re
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLock.lua
More file actions
100 lines (91 loc) · 2.7 KB
/
Copy pathLock.lua
File metadata and controls
100 lines (91 loc) · 2.7 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
-- One.lua
-- prevent multiple executions using a lock file
-- ref: http://stackoverflow.com/questions/14204108/preventing-multiple-executions-of-a-lua-script
-- ref: http://stackoverflow.com/questions/1034334/easiest-way-to-make-lua-script-wait-pause-sleep-block-for-a-few-seconds
require 'makeVp'
require 'validateAttributes'
-- API overview
if false then
lock = Lock('path/to/lockfile')
if lock:acquire() then -- return true if lock acquired, otherwise false
-- do protected work
lock:release()
else
-- could not acquire lock
end
-- attempt maxTries times to acquire the lock, waiting nSeconds between
-- each try
if not lock:acquireOrWait(nSeconds, maxTries) then
error('could not acquire lock')
end
-- do protected work
lock:release()
end
local Lock = torch.class('Lock')
-- constructor
function Lock:__init(pathToLockFile)
local vp = makeVp(1, 'Lock:__init')
validateAttributes(pathToLockFile, 'string')
self.pathToLockFile = pathToLockFile
vp(1, 'self', self)
end
-- attempt to acquire the lock
-- RETURNS
-- boolean : true, if the lock was acquired
-- false, if the lock was not acquired
function Lock:acquire()
local vp = makeVp(0, 'Lock:acquire')
local cmd = 'mkdir ' .. self.pathToLockFile .. ' >/dev/null 2>&1'
vp(2, 'cmd', cmd)
local ok, s, n = os.execute(cmd)
vp(2, 'ok', ok, 's', s, 'n', n)
if ok == 0 then
-- created the directory (aka, the lockfile)
return true
else
return false
end
end
-- release the lock, if it is held, and return true
-- if lock is not held, error
-- RETURNS true
function Lock:release()
local vp = makeVp(0, 'Lock:release')
local cmd = 'rmdir ' .. self.pathToLockFile
vp(2, 'cmd', cmd)
local ok, s, n = os.execute(cmd)
vp(2, 'ok', ok, 's', s, 'n', n)
if ok == 0 then
return true
else
error('failed to remove lock directory')
end
end
-- acquire the lock and return true
-- if lock cannot be acquired immediately, wait for nSeconds, then retry
-- ARGS
-- nSeconds : number of seconds to wait between tries
-- maxTries : number of tries before giving up
-- RETURNS
-- boolean : true, if lock acquired
-- false, if lock not acquired
function Lock:acquireOrWait(nSeconds, maxTries)
local vp = makeVp(0, 'Lock:acquireOrWait')
validateAttributes(nSeconds, 'number', '>', 0)
validateAttributes(maxTries, 'number', '>', 0)
local n = 0
local tries = 0
repeat
cmd = 'sleep ' .. tostring(n)
vp(2, 'cmd', cmd)
os.execute(cmd)
local attemptOk = self:acquire()
trieds = tries + 1
n = nSeconds
until attemptOk or tries > maxTries
if attemptOk then
return true
else
return false
end
end