Reputation: 17970
I want to make sure only one process at a time runs. So I want to make sure jobB doesn't run unless jobA is not running.
It would be great if it had some ability to retry the lock so I don't have to code that. Something vaguely like this:
LockFileModule->lock(
lockfile => '/fabulous/pants',
retries => 12,
timeout => 25,
timebetweenretries => 30,
) or die "the other job is still running";
Whats the best way to do this in Perl? I'm hoping there is a good CPAN module for this.
Upvotes: 1
Views: 527
Reputation: 160
#!/usr/bin/perl
unless (flock(DATA, LOCK_EX|LOCK_NB)) {
$logger->info("$0 is already running. Exiting.\n" );
exit(1);
} else {
$logger->info("$0 not already running, so starting instance now." );
}
__DATA__
Do not delete this. Used for flock code above
This will lock the DATA section of the program itself. I use this technique, and it works very well.
You can expand it to enable retries pretty easily.
Upvotes: 2