NAME
Mojolicious::Plugin::PgLock - postgres advisory locks for Mojolicious application
SYNOPSIS
my $pg = Mojo::Pg->new('postgresql://...');
$app->plugin( PgLock => { pg => $pg } );
if ( my $lock = $app->get_lock ) {
# make something exclusively
}
DESCRIPTION
Mojolicious::Plugin::PgLock implements get_lock helper. It is a shugar for postgres advisory lock functions.
HELPERS
Mojolicious::Plugin::PgLock implements the following helper.
get_lock
my $lock = $app->get_lock
or die "another process is running";
# use a name and try to get a shared lock
my $shared_lock = $app->get_lock( name => 'mySharedLock', shared => 1 );
# use explicit id and wait until a lock is granted
my $lock = $app->get_lock( id => 9874738, wait => 1 );
get_lock helper uses one of postgres advisory lock function pg_try_advisory_lock, pg_advisory_lock, pg_advisory_lock_shared, pg_advisory_lock_shared to get an exclusive or shared lock depending on parameters.
get_lock helper returns a Mojolicious::Plugin::PgLock::Sentinel object which holds the lock while it is alive.
- id
-
idparameter is used as integer key argument in postgres advisory lock function call. Default value foridis CRC32 hash ofnameparameter. - name
-
nameis used foridcalculation only ifidis not set. Default value fornameis(caller(2))[0]. It allows to useget_helperwithout parameters in Mojolicious::Commands modules -
sharedparameter chose shared or exclusive lock. Default is false. - wait
-
If
waitis true then function will wait until a lock is granted.
LICENSE
Copyright (C) Alexander Onokhov <onokhov@cpan.org>.
This library is free software; you can redistribute it and/or modify it under the MIT license terms.
AUTHOR
Copyright (C) Alexander Onokhov <onokhov@cpan.org>.