| 1 | package Beam::Runnable::Single; | ||||||||||
| 2 | 2 | our $VERSION = '0.016'; | |||||||||
| 3 | # ABSTRACT: Only allow one instance of this command at a time | ||||||||||
| 4 | |||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||
| 6 | #pod | ||||||||||
| 7 | #pod ### In a Runnable module | ||||||||||
| 8 | #pod package My::Runnable::Script; | ||||||||||
| 9 | #pod use Moo; | ||||||||||
| 10 | #pod with 'Beam::Runnable', 'Beam::Runnable::Single'; | ||||||||||
| 11 | #pod has '+pid_file' => ( default => '/var/run/runnable-script.pid' ); | ||||||||||
| 12 | #pod sub run { } | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod ### In a container config file | ||||||||||
| 15 | #pod runnable: | ||||||||||
| 16 | #pod $class: My::Runnable::Script | ||||||||||
| 17 | #pod $with: | ||||||||||
| 18 | #pod - 'Beam::Runnable::Single' | ||||||||||
| 19 | #pod pid_file: /var/run/runnable-script.pid | ||||||||||
| 20 | #pod | ||||||||||
| 21 | #pod =head1 DESCRIPTION | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod This role checks to ensure that only one instance of the command is | ||||||||||
| 24 | #pod running at a time. If another instance tries to run, it dies with an | ||||||||||
| 25 | #pod error instead. | ||||||||||
| 26 | #pod | ||||||||||
| 27 | #pod Users should have access to read/write the path pointed to by | ||||||||||
| 28 | #pod L</pid_file>, and to read/write the directory containing the PID file. | ||||||||||
| 29 | #pod | ||||||||||
| 30 | #pod If the command exits prematurely, the PID file will not be cleaned up. | ||||||||||
| 31 | #pod If this is undesirable, make sure to trap exceptions in your C<run()> | ||||||||||
| 32 | #pod method and return the exit code you want. | ||||||||||
| 33 | #pod | ||||||||||
| 34 | #pod =head1 SEE ALSO | ||||||||||
| 35 | #pod | ||||||||||
| 36 | #pod L<Beam::Runnable> | ||||||||||
| 37 | #pod | ||||||||||
| 38 | #pod =cut | ||||||||||
| 39 | |||||||||||
| 40 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 2 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 2 | ▶ | use Types::Path::Tiny qw( Path ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | #pod =attr pid_file | ||||||||||
| 46 | #pod | ||||||||||
| 47 | #pod The path to a file containing the PID of the currently-running script. | ||||||||||
| 48 | #pod | ||||||||||
| 49 | #pod =cut | ||||||||||
| 50 | |||||||||||
| 51 | 2 | has pid_file => ( | |||||||||
| 52 | is => 'ro', | ||||||||||
| 53 | isa => Path, | ||||||||||
| 54 | required => 1, | ||||||||||
| 55 | coerce => 1, | ||||||||||
| 56 | ); | ||||||||||
| 57 | |||||||||||
| 58 | #pod =method run | ||||||||||
| 59 | #pod | ||||||||||
| 60 | #pod This role wraps the C<run> method of your runnable class to check that | ||||||||||
| 61 | #pod there is no running instance of this task (the PID file does not exist). | ||||||||||
| 62 | #pod | ||||||||||
| 63 | #pod =cut | ||||||||||
| 64 | |||||||||||
| 65 | 2 | ▶ | before run => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 66 | 2 | my ( $self, @args ) = @_; | |||||||||
| 67 | 2 | ▶ | if ( $self->pid_file->exists ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 1 | my $pid = $self->pid_file->slurp; | |||||||||
| 69 | 1 | die "Process already running (PID: $pid)\n"; | |||||||||
| 70 | } | ||||||||||
| 71 | 1 | $self->pid_file->spew( $$ ); | |||||||||
| 72 | 2 | }; | |||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | after run => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 75 | 0 | my ( $self ) = @_; | |||||||||
| 76 | 0 | unlink $self->pid_file; | |||||||||
| 77 | 2 | }; | |||||||||
| 78 | |||||||||||
| 79 | 2 | 1; | |||||||||
| 80 | |||||||||||
| 81 | __END__ | ||||||||||