blib/lib/Sys/RunAlone/Flexible.pm

filter: Statementstmt 98.3% 60 / 61 Branchbran 90.6% 29 / 32 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 93.3% 112 / 120 CC 21 SCAR 23.4
CC
21
Cov
94%
lock
16.0
import
6.5
BEGIN
1.0
CRAP 10.4
↑ Summary
1 ## no critic: ValuesAndExpressions::ProhibitCommaSeparatedStatements
2  
3 package Sys::RunAlone::Flexible;
4  
5 36 our $DATE = '2019-09-03'; # DATE
6 36 our $VERSION = '0.004'; # VERSION
7  
8 # make sure we're strict and verbose as possible
9 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # make sure we know how to lock
13 36 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # process local storage
16 36 my $silent;
17 my $retry;
18  
19 # this holds the package namespace of the calling script
20 36 our $pkg;
21  
22 # this holds the package namespace of where a tag was found. it will always be
23 # main::DATA for __END__, or the namespace of the calling script if __DATA__
24 36 our $data_pkg = 'main::DATA';
25  
26 34 ▶sub lock {
Subroutine lock: called CC=16 SCAR=27.7
Pod documented
27 36 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # the environment variables have to be checked here since import doesn't
31 # execute when brought in via "require".
32 # NOTE: the evironment variables will override options that were passed in
33 # via "use" if present!
34 34 ▶ $silent = $ENV{SILENT_SYS_RUNALONE} if exists $ENV{SILENT_SYS_RUNALONE};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"SILENT_SYS_RUNALONE"} 5 29 34
35 34 ▶ $retry = $ENV{RETRY_SYS_RUNALONE} if exists $ENV{RETRY_SYS_RUNALONE};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"RETRY_SYS_RUNALONE"} 4 30 34
36  
37 # skipping
38 34 ▶ if ( my $skip= $ENV{SKIP_SYS_RUNALONE} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $skip = $ENV{'SKIP_SYS_RUNALONE'}) { } 6 28 34
elsif (tell main::DATA == -1) { } 6 22 28
39 6 ▶ print STDERR "Skipping " . __PACKAGE__ . " check for '$0'\n"
Branch: 1 branch
BranchTrueFalseTotal
if not $silent and $skip > 1 2 4 6
Condition100%3/3
not $silent and $skip > 1
!ll&&!rl&&r
222
MC/DC100%2/2
not $silent and $skip > 1
not $silent $skip > 1
Truth table100%3/3
not(A) and B
A= not $silentB= $skip > 1
ABresult
0X0
100
111
40 if !$silent and $skip > 1;
41  
42 6 return;
43 }
44 elsif ( tell(*main::DATA) == -1 ) {
45  
46 # if we reach this then the __END__ tag does not exist. swap in the
47 # calling script namespace to see if the __DATA__ tag exists.
48 6 $data_pkg = $pkg . '::DATA';
49 6 ▶ if ( ( tell( *{$data_pkg} ) == -1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tell do { $Sys::RunAlone::Flexible::data_pkg } == -1) 4 2 6
50 4 print STDERR "Add __END__ or __DATA__ to end of script '$0'"
51 . " to be able use the features of Sys::RunALone\n";
52 4 exit 2;
53 }
54 }
55  
56 # are we alone? $data_pkg will be set to wherever an appropriate tag was.
57 24 ▶ if ( !flock *{$data_pkg}, LOCK_EX | LOCK_NB ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock do { $Sys::RunAlone::Flexible::data_pkg }, 6) 13 11 24
58  
59 # need to retry
60 13 ▶ if ($retry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry) 6 7 13
61 6 ▶ print STDERR "Retrying lock attempt ...\n" unless $silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 4 2 6
62 6 my ( $times, $sleep )= split ',', $retry;
63 6 ▶ $sleep ||= 1;
Condition100%2/2
$sleep ||= 1
l!l
24
MC/DC100%1/1
$sleep ||= 1
$sleep
Truth table100%2/2
A
A= $sleep
Aresult
11
00
64 6 ▶ while ( $times-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($times--) 10 4 14
65 10 sleep $sleep;
66  
67 # we're alone!
68 10 ▶ goto ALLOK if flock *{$data_pkg}, LOCK_EX | LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if flock do { $Sys::RunAlone::Flexible::data_pkg }, 6 2 8 10
69 }
70 4 ▶ print STDERR "Retrying lock failed ...\n" unless $silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 4 0 4
71 }
72  
73 # we're done
74 11 ▶ print STDERR "A copy of '$0' is already running\n" if !$silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 8 3 11
75 11 exit 1;
76 }
77  
78 ALLOK:
79 13 return;
80 }
81  
82 #-------------------------------------------------------------------------------
83 #
84 # Standard Perl functionality
85 #
86 #-------------------------------------------------------------------------------
87 # import
88 #
89 # IN: 1 class (not used)
90 # 2 .. N options (default: none)
91  
92 18 ▶sub import {
Subroutine import: called CC=6 SCAR=18.7
93 18 shift;
94  
95 # support obsolete form of silencing
96 18 ▶ $silent= 1, return if @_ == 1 and $_[0] and $_[0] eq 'silent';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and $_[0] and $_[0] eq "silent" 1 17 18
Condition66%4/6
@_ == 1 and $_[0] and $_[0] eq "silent"
!ll&&!rl&&r
1701
@_ == 1 and $_[0]
!ll&&!rl&&r
1701
MC/DC33%1/3
@_ == 1 and $_[0] and $_[0] eq "silent"
@_ == 1 $_[0] $_[0] eq "silent"
Truth table50%2/4
A and B and C
A= @_ == 1B= $_[0]C= $_[0] eq "silent"
ABCresult
0XX0
10X0
1100
1111
97  
98 # huh?
99 17 ▶ die "Must specify even number of parameters" if ( @_ & 1 ) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1) == 1 0 17 17
100  
101 # obtain parameters
102 17 my %args= @_;
103 17 $silent= delete $args{silent};
104 17 $retry= delete $args{retry};
105  
106 # sanity check
107 17 ▶ if ( my @huh= sort keys %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@huh) = sort(keys %args)) 0 17 17
108 0 die "Don't know what to do with: @huh";
109 }
110  
111 17 return;
112 } #import
113  
114 {
115 # it is at this point we can get the correct package namespace of the
116 # calling script
117 36 my @call_info = caller(0);
118 36 $pkg = $call_info[0];
119  
120 # to shut up the 'Too late to run INIT block' warning
121 36 ▶ no warnings 'void';
Subroutine BEGIN: called CC=1 SCAR=0.0
122 18 ▶ INIT {
Subroutine INIT: called CC=1 SCAR=0.0
123 18 lock();
124 }
125 }
126  
127 # satisfy -require-
128 36 1;
129  
130 # ABSTRACT: make sure only one invocation of a script is active at a time
131  
132 __END__
↑ Summary