Pidfile.jl
Documentation for Pidfile.jl:
A simple utility tool for creating advisory pidfiles (lock files).
Primary Functions
Pidfile.mkpidlock
— Functionmkpidlock([f::Function], at::String, [pid::Cint, proc::Process]; kwopts...)
Create a pidfile lock for the path "at" for the current process or the process identified by pid or proc. Can take a function to execute once locked, for usage in do
blocks, after which the lock will be automatically closed. If the lock fails and wait
is false, then an error is thrown.
The lock will be released by either close
, a finalizer
, or shortly after proc
exits. Make sure the return value is live through the end of the critical section of your program, so the finalizer
does not reclaim it early.
Optional keyword arguments:
mode
: file access mode (modified by the process umask). Defaults to world-readable.poll_interval
: Specify the maximum time to between attempts (ifwatch_file
doesn't work)stale_age
: Delete an existing pidfile (ignoring the lock) if its mtime is older than this. The file won't be deleted until 25x longer than this if the pid in the file appears that it may be valid. By default this is disabled (stale_age
= 0), but a typical recommended value would be about 3-5x an estimated normal completion time.refresh
: Keeps a lock from becoming stale by updating the mtime every interval of time that passes. By default, this is set tostale_age/2
, which is the recommended value.wait
: If true, block until we get the lock, if false, raise error if lock fails.
Base.close
— Functionclose(lock::LockMonitor)
Release a pidfile lock.
Helper Functions
Pidfile.open_exclusive
— Functionopen_exclusive(path::String; mode, poll_interval, stale_age) :: File
Create a new a file for read-write advisory-exclusive access. If wait
is false
then error out if the lock files exist otherwise block until we get the lock.
For a description of the keyword arguments, see mkpidlock
.
Pidfile.tryopen_exclusive
— Functiontryopen_exclusive(path::String, mode::Integer = 0o444) :: Union{Void, File}
Try to create a new file for read-write advisory-exclusive access, return nothing if it already exists.
Pidfile.write_pidfile
— Functionwrite_pidfile(io, pid)
Write our pidfile format to an open IO descriptor.
Pidfile.parse_pidfile
— Functionparse_pidfile(file::Union{IO, String}) => (pid, hostname, age)
Attempt to parse our pidfile format, replaced an element with (0, "", 0.0), respectively, for any read that failed.
Pidfile.stale_pidfile
— Functionstale_pidfile(path::String, stale_age::Real) :: Bool
Helper function for open_exclusive
for deciding if a pidfile is stale.
Pidfile.isvalidpid
— Functionisvalidpid(hostname::String, pid::Cuint) :: Bool
Attempt to conservatively estimate whether pid is a valid process id.
Base.Filesystem.touch
— MethodBase.touch(::Pidfile.LockMonitor)
Update the mtime
on the lock, to indicate it is still fresh.
See also the refresh
keyword in the mkpidlock
constructor.