blib/lib/Role/RunAlone.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 93.7% 30 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 96.5% 112 / 116 CC 15 SCAR 21.7
CC
15
Cov
96%
runalone_lock
15.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.8
↑ Summary
1 package Role::RunAlone;
2  
3 21 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 our $VERSION = 'v0.1.0';
8  
9 21 ▶use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 21 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 my %default_lock_args = (
15 noexit => 0,
16 attempts => 1,
17 interval => 1,
18 verbose => 0,
19 );
20  
21 12 my $data_pkg = 'main::DATA';
22 12 my $caller_cnt = 0;
23  
24 # use a block because the pragmas have lexical scope and we need
25 # to stop warnings/errors from the call to "tell()"
26 {
27 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 21 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 12 ▶ if ( tell( *{$data_pkg} ) == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tell do { $data_pkg } == -1) 3 9 12
31  
32 # if we reach this then the __END__ tag does not exist, and the
33 # __DATA__ tag is not in the "main" namespace. swap in the
34 # calling script namespace to see if the __DATA__ tag is in there.
35 3 ▶ while ( ++$caller_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$caller_cnt) 20 0 20
36 20 my @call_info = caller($caller_cnt);
37 20 ▶ last if !@call_info;
Branch: 1 branch
BranchTrueFalseTotal
unless @call_info 3 17 20
38 17 $data_pkg = $call_info[0] . '::DATA';
39 }
40  
41 3 ▶ if ( ( tell( *{$data_pkg} ) == -1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tell do { $data_pkg } == -1) 0 3 3
42 0 warn "FATAL: No __DATA__ or __END__ tag found\n";
43 0 __PACKAGE__->_runalone_exit(2);
44 }
45 }
46 }
47  
48 # maybe the script wants to control this
49 12 ▶__PACKAGE__->runalone_lock unless !!$ENV{RUNALONE_DEFER_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if not $ENV{"RUNALONE_DEFER_LOCK"} 4 8 12
50  
51 # is the argument validation over-engineered? maybe, but I'm paranoid.
52 45 ▶sub runalone_lock {
Subroutine runalone_lock: called CC=15 SCAR=27.1
Pod documented
53 45 my $proto = shift;
54 45 my %args = @_;
55  
56 # set defaults as needed
57 45 for ( keys(%default_lock_args) ) {
58 180 ▶ $args{$_} = $default_lock_args{$_} unless defined( $args{$_} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$_} 117 63 180
59 }
60  
61 45 ▶ croak 'ERROR: unknown argument present'
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != scalar keys %default_lock_args 1 44 45
62 if scalar( keys(%args) ) != scalar( keys(%default_lock_args) );
63  
64 # validate integer args
65 44 for (qw( attempts interval )) {
66 83 ▶ croak "$_: invalid value" unless $args{$_} =~ /^[1-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$_} =~ /^[1-9]$/ 10 73 83
67 }
68  
69 # coerce Boolean args
70 34 for (qw( noexit verbose )) {
71 68 $args{$_} = !!$args{$_};
72 }
73  
74 34 my $ret;
75 34 ▶ while ( $args{attempts}-- > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"attempts"}-- > 0) 85 28 113
76 85 ▶ warn "Attempting to lock $data_pkg ...\n" if $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"verbose"} 2 83 85
77 85 ▶ last if $ret = $proto->_runalone_lock;
Branch: 1 branch
BranchTrueFalseTotal
if $ret = $proto->_runalone_lock 6 79 85
78 warn "Failed, retrying $args{attempts} more time(s)\n"
79 79 ▶ if $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"verbose"} 1 78 79
80  
81 79 ▶ sleep( $args{interval} ) if $args{attempts};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"attempts"} 51 28 79
82 }
83  
84 34 ▶ if ($ret) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret) { } 6 28 34
elsif (not $args{'noexit'}) { } 9 19 28
85 6 ▶ warn "SUCCESS\n" if $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"verbose"} 1 5 6
86 }
87 elsif ( !$args{noexit} ) {
88 9 warn "FATAL: A copy of '$0' is already running\n";
89 9 __PACKAGE__->_runalone_exit(1);
90 }
91  
92 25 return $ret;
93 }
94  
95 # broken out for easier retry testing
96 8 ▶sub _runalone_lock {
Subroutine _runalone_lock: called CC=1 SCAR=0.0
97 8 my $proto = shift;
98  
99 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 8 return flock( *{$data_pkg}, LOCK_EX | LOCK_NB );
101 }
102  
103 # mock this out in tests
104 9 ▶sub _runalone_exit {
Subroutine _runalone_exit: called CC=1 SCAR=0.0
105 9 my $proto = shift;
106  
107 9 exit( shift );
108 }
109  
110 # helper for test scripts
111 8 ▶sub _runalone_tag_pkg {
Subroutine _runalone_tag_pkg: called CC=1 SCAR=0.0
112 8 $data_pkg =~ /^(.+)::DATA$/;
113  
114 8 return { package => $1, caller => $caller_cnt };
115 }
116  
117 12 1;
118 __END__
↑ Summary