The fussy.cronlock module provides a simple mechanism to prevent runaway cron jobs. A lock can be used to protect a piece of code that should only ever have a single running instance:
from fussy import cronlock
lock = cronlock.Lock( 'test.lock' )
with lock:
do_something()
It can also be used to prevent a piece of code from running for too long (note: the lock does not need to be held for this to work):
from fussy import cronlock
lock = cronlock.Lock( 'test.lock' )
lock.set_timeout( 20 ) # from this moment
do_something_that_might_take_a_while()
A decorator is provided to create and hold the lock for a given function, normally the main function of your cron-lock:
from fussy import with_lock
@with_lock( 'test.lock', timeout=20 )
def main():
"""Your cron job main-loop"""
Provide a cron lock for preventing cron-bombs and the like
Raised if the lock is held by another cronlock
list of weak references to the object (if defined)
A context manager that just flocks a file
list of weak references to the object (if defined)
A Context manager that provides cron-style locking
Create a lock file
name – used to construct the lock-file name directory – directory in which to construct the lock-file
list of weak references to the object (if defined)
Raised if the timeout signal triggers
list of weak references to the object (if defined)
Decorator that runs a function with Lock instance acquired
name – basename of the file to create
defaults to tempfile.gettempdir()
a Timeout error