@@ -33,7 +33,8 @@ Optional keyword arguments:
3333 - `mode`: file access mode (modified by the process umask). Defaults to world-readable.
3434 - `poll_interval`: Specify the maximum time to between attempts (if `watch_file` doesn't work)
3535 - `stale_age`: Delete an existing pidfile (ignoring the lock) if it is older than this many seconds, based on its mtime.
36- The file won't be deleted until 25x longer than this if the pid in the file appears that it may be valid.
36+ The file won't be deleted until 5x longer than this if the pid in the file appears that it may be valid.
37+ Or 25x longer if `refresh` is overridden to 0 to disable lock refreshing.
3738 By default this is disabled (`stale_age` = 0), but a typical recommended value would be about 3-5x an
3839 estimated normal completion time.
3940 - `refresh`: Keeps a lock from becoming stale by updating the mtime every interval of time that passes.
@@ -64,7 +65,7 @@ mutable struct LockMonitor
6465 atdir, atname = splitdir (at)
6566 isempty (atdir) && (atdir = pwd ())
6667 at = realpath (atdir) * path_separator * atname
67- fd = open_exclusive (at; stale_age= stale_age , kwopts... )
68+ fd = open_exclusive (at; stale_age, refresh , kwopts... )
6869 update = nothing
6970 try
7071 write_pidfile (fd, pid)
@@ -185,15 +186,16 @@ function isvalidpid(hostname::AbstractString, pid::Cuint)
185186end
186187
187188"""
188- stale_pidfile(path::String, stale_age::Real) :: Bool
189+ stale_pidfile(path::String, stale_age::Real, refresh::Real ) :: Bool
189190
190191Helper function for `open_exclusive` for deciding if a pidfile is stale.
191192"""
192- function stale_pidfile (path:: String , stale_age:: Real )
193+ function stale_pidfile (path:: String , stale_age:: Real , refresh :: Real )
193194 pid, hostname, age = parse_pidfile (path)
194195 age < - stale_age && @warn " filesystem time skew detected" path= path
196+ longer_factor = refresh == 0 ? 25 : 5
195197 if age > stale_age
196- if (age > stale_age * 25 ) || ! isvalidpid (hostname, pid)
198+ if (age > stale_age * longer_factor ) || ! isvalidpid (hostname, pid)
197199 return true
198200 end
199201 end
@@ -220,7 +222,7 @@ struct PidlockedError <: Exception
220222end
221223
222224"""
223- open_exclusive(path::String; mode, poll_interval, wait, stale_age) :: File
225+ open_exclusive(path::String; mode, poll_interval, wait, stale_age, refresh ) :: File
224226
225227Create a new a file for read-write advisory-exclusive access.
226228If `wait` is `false` then error out if the lock files exist
@@ -232,13 +234,14 @@ function open_exclusive(path::String;
232234 mode:: Integer = 0o444 #= read-only =# ,
233235 poll_interval:: Real = 10 #= seconds =# ,
234236 wait:: Bool = true #= return on failure if false =# ,
235- stale_age:: Real = 0 #= disabled =# )
237+ stale_age:: Real = 0 #= disabled =# ,
238+ refresh:: Real = stale_age/ 2 )
236239 # fast-path: just try to open it
237240 file = tryopen_exclusive (path, mode)
238241 file === nothing || return file
239242 if ! wait
240243 if file === nothing && stale_age > 0
241- if stale_age > 0 && stale_pidfile (path, stale_age)
244+ if stale_age > 0 && stale_pidfile (path, stale_age, refresh )
242245 @warn " attempting to remove probably stale pidfile" path= path
243246 tryrmopenfile (path)
244247 end
@@ -264,7 +267,7 @@ function open_exclusive(path::String;
264267 file = tryopen_exclusive (path, mode)
265268 file === nothing || return file
266269 Base. wait (t) # sleep for a bit before trying again
267- if stale_age > 0 && stale_pidfile (path, stale_age)
270+ if stale_age > 0 && stale_pidfile (path, stale_age, refresh )
268271 # if the file seems stale, try to remove it before attempting again
269272 # set stale_age to zero so we won't attempt again, even if the attempt fails
270273 stale_age -= stale_age
0 commit comments