문제

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.

도움이 되었습니까?

해결책

#!/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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top