wrapping golock programs

I recently released a small package called golock. golock isn't that interesting on its own, it provides some utiities to establish exclusive file locks on systems that support this feature.

Locking programs with a lock file does present some challenges though - it is possible for the system to enter into a state where either the lockfile remains after the calling program that established the lock has completed, or that process has entered into a zombie state. In either case, we may find it useful to "break" the lock (removing the lockfile) and running our program anyway (in the case of crontab-scheduled programs, etc).

Consider following program, which we can call testlock.go:

gist

It is possible that while this program sleeps for ten seconds, somehow the process was terminated, and the lock left intact (normally you would catch the signal and remove the lock in the signal handler to prevent this). When we go to run testlock again, it will appear to be locked even though it is not executing. This simple wrapper can be used to control execution of the go program and safely break locks where applicable:

gist

This wrapper can be invoked for this example as:

gist

last update 2013-03-03