DotLock - Multi-host advisory queing locking system
see CONSTRUCTOR & METHODS section below
DotLock is a multipurpose queing locking system. Originally designed to take some of the pain away from locking on NFS filesystems.
This module allows script writers to develop on multiple hosts when locking between these hosts is an issue. It allows queing - scary but true. It also provides an atomic method of locking by using the "link" function between files.
The locking/queing method provided is purely on a file manipulation level. Also note that this object does not handle signals. If the program is interrupted, any open lockfiles will be left behind.
- new (OPTIONS) - create a new DotLock object
This is the constructor of the
OPTIONS are passed to the object in a hash-like fashion using keys and values. The keys are the same as the methods provided below, as shown:
$t = new DotLock( path => "/home/ken/perl/locking/", errmode => "die", timeout => 100, maxqueue => 8, retrytime => 1, domain => 'optusnet.com.au', );
So you can define all settings in one go. Besides the "lock" method, this is the heart and soul of the module.
- lock - attempt to obtain a lock
This method will attempt to obtain a lock with the configuration passed to it from the constructor or from the various setup methods.
The process is basically as follows:
1. A temporary lockfile is created which is associated with the script and host.
2. The lockfile directory is listed to find the highest free lock.
3. Depending on the maxqueue setting, the highest lock is obtained.
4. If for some reason the highest lock was nabbed before the current script could get it, the current script will try the next higher up lock que until it obtains a placing.
5. Once a place in the que is obtained, lock will attempt to get the next lock. It will retry every second, or the time defined in the retrytime method.
6. Unless the process has reached the point of obtaining the main lock, the script will return to step 5 to obtain the next que placing.
7. Once the main lock is obtained, the script using this object will do its business.
- unlock - remove all lockfiles
The method removes all open lockfiles, enabling other processes to obtain any lock you have left open. This is also the method called upon object destruction.
- timeout ([TIMEOUT]) - timeout for obtaining a lock
This defaults to 60 seconds.
Set this method to the amount of time you want your script to wait for a lock. The
TIMEOUTvalue is in seconds.
If you fail to obtain the lock with the set time, the object will return an error and react based on the
errmodemethod set when creating the object.
- path ([PATH]) - path of lockfiles
Set the directory for where the lockfiles will be kept. This by default is set to /var/lock/quelock. If the path does not exist, an error will occur.
For two processes to use the same lockfiles, this path must be the same in both processes.
With this method you can either set the path of the lockfiles, or return the previous path.
- errmode ([MODE]) - set how to react to errors
The errmode method can be set to either "die" to die when an error is encountered or "return" to return an error message. The error message can be obtained from the errmsg method.
The default setting for errmode is "die".
- maxqueue ([QUE]) - the highest allowable que placing
The maxqueue data method is used by the
lockmethod. When attempting to find a place in the que, it will look at this figure - if the figure is higher than the highest free que then DotLock will return an error.
By default, queing is turned off.
- retrytime ([TIME]) - the time in seconds to retry the next lock placing
You set this data method to the amount of time in seconds you with the system to retry for the next available lock. The default is 1 second.
- domain ([DOMAIN]) - set the domain of your system
Although not necessary, this allows all hosts entries to be reduced to just their hostname. This is only really usefull if you plan on running scripts with one domain and one domain only.
- errmsg - returns the last error message
This method will return the error message of the last error found. When
errmodeis set to "return" this variable will be loaded with the error message.
Some other good modules for locking:
$t = new DotLock( path => "/home/ken/perl/locking/locktest", errmode => "return", timeout => 100, maxqueue => 8, retrytime => 1, domain => 'optusnet.com.au', );
$t->lock || print $t->errmsg . "\n";
$blah = <STDIN>;
Ken Barber <email@example.com>
Copyright (c) 1999 Ken Barber. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.