| 1 | ## no critic: ValuesAndExpressions::ProhibitCommaSeparatedStatements | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Sys::RunAlone::Flexible2; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | our $DATE = '2019-09-03'; # DATE | |||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.001'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Fcntl ':flock'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # process local storage | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | my $silent; | |||||||||||||||||||||||||||||||||||||||||
| 15 | my $retry; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub lock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lock: called CC=17 SCAR=43.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | no warnings; # to shut up "tell() on unopened filehandle" | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # the environment variables have to be checked here since import doesn't | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # execute when brought in via "require". | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # NOTE: the evironment variables will override options that were passed in | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # via "use" if present! | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | $silent = $ENV{SILENT_SYS_RUNALONE} if exists $ENV{SILENT_SYS_RUNALONE}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | $retry = $ENV{RETRY_SYS_RUNALONE} if exists $ENV{RETRY_SYS_RUNALONE}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # skipping | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | if ( my $skip= $ENV{SKIP_SYS_RUNALONE} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | print STDERR "Skipping " . __PACKAGE__ . " check for '$0'\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $silent and $skip > 1
MC/DC0%0/2
not $silent and $skip > 1
not $silent
$skip > 1
Truth table0%0/3
not(A) and B
A= not $silentB= $skip > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | if !$silent and $skip > 1; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | my $data_handle = \*main::DATA; | |||||||||||||||||||||||||||||||||||||||||
| 36 | GET_ALT_HANDLE: | ||||||||||||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | last unless tell($data_handle) == -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | # script does not contain __DATA__ or __END__, try to open a filehandle | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # to ourself. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | if (open $data_handle, "<", $0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # cannot open filehandle to the script ($0), probably one-liner. try to | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # open a filehandle to this module instead. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if (open $data_handle, "<", $INC{"Sys/RunAlone/Flexible2.pm"}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | print STDERR "Cannot get a handle to ourself: $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | exit 2; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # are we alone? | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if ( !flock $data_handle, LOCK_EX | LOCK_NB ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # need to retry | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ($retry) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | print STDERR "Retrying lock attempt ...\n" unless $silent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my ( $times, $sleep )= split ',', $retry; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $sleep ||= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$sleep ||= 1
MC/DC0%0/1
$sleep ||= 1
$sleep
Truth table0%0/2
A
A= $sleep
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | while ( $times-- ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | sleep $sleep; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # we're alone! | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | goto ALLOK if flock $data_handle, LOCK_EX | LOCK_NB; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | print STDERR "Retrying lock failed ...\n" unless $silent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # we're done | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | print STDERR "A copy of '$0' is already running\n" if !$silent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | ALLOK: | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | shift; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | # support obsolete form of silencing | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | $silent= 1, return if @_ == 1 and $_[0] and $_[0] eq 'silent'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@_ == 1 and $_[0] and $_[0] eq "silent"
@_ == 1 and $_[0]
MC/DC0%0/3
@_ == 1 and $_[0] and $_[0] eq "silent"
@_ == 1
$_[0]
$_[0] eq "silent"
Truth table25%1/4
A and B and C
A= @_ == 1B= $_[0]C= $_[0] eq "silent"
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # huh? | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | die "Must specify even number of parameters" if ( @_ & 1 ) == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # obtain parameters | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | my %args= @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | $silent= delete $args{silent}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | $retry= delete $args{retry}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # sanity check | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | if ( my @huh= sort keys %args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | die "Don't know what to do with: @huh"; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } #import | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # to shut up the 'Too late to run INIT block' warning | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | no warnings 'void'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | INIT { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
INIT: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | lock(); | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # ABSTRACT: Make sure only one invocation of a script is active at a time | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||||||||||||