App-OverWatch
view release on metacpan or search on metacpan
bin/servicelock view on Meta::CPAN
my $ServiceLock = $OverWatch->servicelock();
$commands{$cmd}->();
exit(0);
sub do_check {
my $Lock = $ServiceLock->get_lock({
system => $Options->{system},
});
exit(0) if ($Lock->is_locked());
exit(1);
}
sub do_create_lock {
my $system = $Options->{system};
if ($ServiceLock->create_lock({
system => $system,
})) {
print "Created lock '$system'\n";
}
bin/servicelock view on Meta::CPAN
print "Created table for ServiceLock.\n";
}
}
sub do_force_unlock {
my $system = $Options->{system};
my $Lock = $ServiceLock->get_lock({
system => $system,
});
die "Error: Lock '$system' is not currently locked!\n"
if (!$Lock->is_locked());
if ($ServiceLock->force_unlock({
system => $system,
})) {
print "Force unlocked '$system'\n";
}
}
sub do_list {
my @Locks = $ServiceLock->get_all_locks();
foreach my $Lock (@Locks) {
$Lock->print();
print "\n";
}
}
bin/servicelock view on Meta::CPAN
});
$Lock->print();
}
sub do_unlock {
my $system = $Options->{system};
if ($ServiceLock->try_unlock({
system => $system,
worker => $Options->{worker},
})) {
print "Unlocked '$system'\n";
} else {
die "Could not unlock '$system'\n";
}
}
sub do_update {
my $system = $Options->{system};
if ($ServiceLock->try_update({
system => $system,
worker => $Options->{worker},
bin/servicelock view on Meta::CPAN
=head1 EXAMPLE
Create the ServiceLock table 'servicelocks':
# servicelock --create_table
Create a lock named 'global':
# servicelock --create_lock --system global
Check whether 'global' is locked (flagged by exit status):
# servicelock --check --system global
Attempt to lock the 'global' lock:
# servicelock --lock --system global --worker myworkerid --text 'testing lock'
# servicelock --lock --system global --worker myworkerid --text 'testing lock' --expiry 60
Unlock:
lib/App/OverWatch/Lock.pm view on Meta::CPAN
use Moo;
use namespace::clean;
has system => ( is => 'ro' );
has expiry => ( is => 'ro' );
has mtime => ( is => 'ro' );
has worker => ( is => 'ro' );
has text => ( is => 'ro' );
has status => ( is => 'ro' );
sub is_locked {
my $self = shift;
return 1 if ($self->status() eq 'LOCKED');
return 0;
}
sub print {
my $self = shift;
foreach my $attr (qw( System Worker Status Mtime Expiry Text )) {
my $lc_attr = lc($attr);
lib/App/OverWatch/Lock.pm view on Meta::CPAN
=head2 mtime
=head2 worker
=head2 text
=head2 status
=head1 METHODS
=head2 is_locked
Returns 1 if locked, 0 otherwise.
=head2 print
Print to stdout.
=head1 AUTHOR
Chris Hughes <chrisjh@cpan.org>
=head1 COPYRIGHT AND LICENSE
t/servicelock.pl view on Meta::CPAN
## Create second lock
is($ServiceLock->create_lock({ system => $s2 }), 1, "Create $s2 lock");
## Get all locks
@Locks = $ServiceLock->get_all_locks();
is(scalar @Locks, 2, "get_all_locks() returns 2 locks");
## Force unlock
is($ServiceLock->force_unlock({ system => $s }), 1, "Force unlock (succeed)");
## Force unlock an already unlocked lock
is($ServiceLock->force_unlock({ system => $s2 }), 0, "Force unlock (fail as lock not locked)");
## utf8 tests
{
my $char_str = "à á â ã ä å æ ç è é ê ë ì à î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ";
is($ServiceLock->try_lock({ system => $s, worker => $w, text => $char_str }), 1, "Get lock (succeed)");
my $rh_lock = $ServiceLock->get_lock({ system => $s });
isa_ok($rh_lock, 'App::OverWatch::Lock');
is($rh_lock->status, 'LOCKED', 'Lock is LOCKED');
is($rh_lock->text, $char_str, 'Character string is as expected');
}
( run in 0.449 second using v1.01-cache-2.11-cpan-49f99fa48dc )