Mojolicious::Plugin::PgLock - postgres advisory locks for Mojolicious application
my $pg = Mojo::Pg->new('postgresql://...'); $app->plugin( PgLock => { pg => $pg } ); if ( my $lock = $app->get_lock ) { # make something exclusively }
Mojolicious::Plugin::PgLock implements get_lock helper. It is a shugar for postgres advisory lock functions.
Mojolicious::Plugin::PgLock implements the following helper.
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
pg_try_advisory_lock
pg_advisory_lock
pg_advisory_lock_shared
get_lock helper returns a Mojolicious::Plugin::PgLock::Sentinel object which holds the lock while it is alive.
id parameter is used as integer key argument in postgres advisory lock function call. Default value for id is CRC32 hash of name parameter.
id
name
name is used for id calculation only if id is not set. Default value for name is (caller(2))[0]. It allows to use get_helper without parameters in Mojolicious::Commands modules
(caller(2))[0]
get_helper
shared parameter chose shared or exclusive lock. Default is false.
shared
If wait is true then function will wait until a lock is granted.
wait
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.
To install Mojolicious::Plugin::PgLock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::PgLock
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::PgLock
For more information on module installation, please visit the detailed CPAN module installation guide.