Conveyor/Mutex.pm

filter: Statementstmt 29.2% 26 / 89 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 50.0% 9 / 18 Podpod 100% 6 / 6 totaltotal 23.9% 41 / 171 CC 24 SCAR 40.1
CC
24
Cov
19%
get_mutex
132.0
mutex_getconf
56.0
mail
56.0
CRAP 55.3
« Conveyor/Lock.pm ↑ Summary Conveyor/Service/Interface/Shell.pm »
1 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Data::Conveyor::Mutex;
6 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 $Data::Conveyor::Mutex::VERSION = '1.103130';
8 }
9 # ABSTRACT: Stage-based conveyor-belt-like ticket handling system
10  
11 2 ▶use Sys::Hostname (); # no import so we don't clash with our hostname()
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Error ':try';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # XXX the whole thing must be recoded with database locks.
15 # Note: one way or the other, this is probably not portable
16 # across databases.
17 2 ▶use parent 'Class::Scaffold::Storable';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 __PACKAGE__->mk_scalar_accessors(
19 qw(
20 application mutex_config_id max_parallel group_exlock program_name
21 pid hostname dbinst
22 )
23 );
24  
25 # FIXME
26 2 ▶use constant ADMINADDR => 'service-admin@domain.univie.ac.at';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use constant DEFAULTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
28 hostname => Sys::Hostname::hostname(),
29 pid => $$,
30 program_name => $0,
31 2 );
32  
33 0 ▶sub mutex_getconf {
Subroutine mutex_getconf: not called CC=7 SCAR=40.3
Pod documented
34 0 my $self = shift;
35 0 $self->dbinst($self->storage->dbname);
36 0 $self->log(
37 sprintf '%s[%08d]@%s (%s) init "%s"' =>
38 map($self->$_, qw/
39 program_name pid hostname dbinst application/)
40 );
41  
42 # get config id and parallelity
43 0 my ($cnf_id, $parallel) = $self->storage->get_mutex_config($self);
44 0 $self->mutex_config_id($cnf_id);
45 0 $self->max_parallel($parallel);
46  
47 # require this to be configured.
48 0 ▶ $self->error(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->mutex_config_id and defined $self->max_parallel 0 0 0
Condition0%0/3
$self->mutex_config_id and defined $self->max_parallel
!ll&&!rl&&r
000
MC/DC0%0/2
$self->mutex_config_id and defined $self->max_parallel
$self->mutex_config_id defined $self->max_parallel
Truth table0%0/3
A and B
A= $self->mutex_config_idB= defined $self->max_parallel
ABresult
0X0
100
111
49 sprintf '%s: mutex configuration: no valid entry found for "%s"' =>
50 __PACKAGE__,
51 $self->application
52 )
53 unless $self->mutex_config_id
54 && defined $self->max_parallel;
55  
56 # now check if we are in a group
57 0 my $tmp = $self->storage->get_mutex_data($self);
58  
59 # test validity of mutex configuration
60 0 ▶ if ($self->group_exlock(scalar(@$tmp) > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->group_exlock(scalar @$tmp > 1)) 0 0 0
61 0 for my $cnf (@$tmp) {
62 0 ▶ $self->error(
Branch: 1 branch
BranchTrueFalseTotal
if $cnf->[2] > 1 0 0 0
63 sprintf "%s: mutex misconfigured for '%s':\nApplication is"
64 . " in a GROUP (%d), but MTXCNF_MAXPARALLEL=%d. Aborting." =>
65 __PACKAGE__,
66 $cnf->[1], $self->mutex_config_id, $cnf->[2]
67 ) if $cnf->[2] > 1;
68 }
69 }
70  
71 # reset the transaction
72 0 $self->storage->dbh->rollback;
73 0 ▶ $self->log(
Branch: 1 branch
BranchTrueFalseTotal
$self->group_exlock ? : 0 0 0
74 sprintf '%s[%08d]@%s (%s) mutex_getconf CNFID=%d MXP=%d GROUP=%s'
75 . ' "%s"' =>
76 map($self->$_, qw/
77 program_name pid hostname dbinst mutex_config_id max_parallel/),
78 ($self->group_exlock ? 'yes' : 'no '),
79 $self->application,
80 );
81 0 $self;
82 }
83  
84 0 ▶sub get_mutex {
Subroutine get_mutex: not called CC=11 SCAR=48.8
Pod documented
85 0 my $self = shift;
86  
87 # XXX: replace this with assert_defined
88 0 ▶ $self->error(sprintf "%s: mutex not initialized. Aborting." => __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa(__PACKAGE__) and $self->application and $self->storage and $self->mutex_config_id and defined $self->max_parallel 0 0 0
Condition0%0/12
$self->isa(__PACKAGE__) and $self->application and $self->storage and $self->mutex_config_id and defined $self->max_parallel
!ll&&!rl&&r
000
$self->isa(__PACKAGE__) and $self->application and $self->storage and $self->mutex_config_id
!ll&&!rl&&r
000
$self->isa(__PACKAGE__) and $self->application and $self->storage
!ll&&!rl&&r
000
$self->isa(__PACKAGE__) and $self->application
!ll&&!rl&&r
000
MC/DC0%0/5
$self->isa(__PACKAGE__) and $self->application and $self->storage and $self->mutex_config_id and defined $self->max_parallel
$self->isa(__PACKAGE__) $self->application $self->storage $self->mutex_config_id defined $self->max_parallel
Truth table0%0/6
A and B and C and D and E
A= $self->isa(__PACKAGE__)B= $self->applicationC= $self->storageD= $self->mutex_config_idE= defined $self->max_parallel
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
89 unless $self->isa(__PACKAGE__)
90 && $self->application
91 && $self->storage
92 && $self->mutex_config_id
93 && defined $self->max_parallel;
94 ## make sure previous transaction is over
95 0 ▶ $self->storage->dbh->rollback
Branch: 1 branch
BranchTrueFalseTotal
$self->storage->dbh->rollback or $self->error("Data::Conveyor::Mutex: critical rollback failed. Aborting.") 0 0 0
96 || $self->error(
97 sprintf "%s: critical rollback failed. Aborting." => __PACKAGE__);
98  
99 # try all slots < parallelity
100 # try locks
101 0 my $LOCKFAIL;
102 my $HAVELOCK;
103 0 ▶ for (my $slot = 0 ; $slot < $self->max_parallel ; $slot++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot < $self->max_parallel) 0 0 0
104 0 $LOCKFAIL = 0;
105 0 $HAVELOCK = undef;
106 0 local $Error::Hierarchy::Internal::DBI::SkipWarning = 1;
107 0 my $slot_info;
108 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 $slot_info = $self->storage->get_mutex_slot($self, $slot);
110 }
111 0 ▶ catch Error with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 $LOCKFAIL++;
113  
114 # XXX: log the exception here
115 0 };
116 0 ▶ $self->log(
Branch: 1 branch
BranchTrueFalseTotal
$LOCKFAIL ? : 0 0 0
117 sprintf
118 '%s[%08d]@%s (%s) get_mutex CNFID=%d SLOT=%d: lock=%s "%s"' =>
119 map($self->$_,
120 qw/program_name pid hostname dbinst mutex_config_id/),
121 $slot,
122 ($LOCKFAIL ? 'no ' : 'yes'),
123 $self->application
124 );
125 ## lock failed...
126 0 ▶ next if $LOCKFAIL;
Branch: 1 branch
BranchTrueFalseTotal
if $LOCKFAIL 0 0 0
127 0 ▶ $self->error(
Branch: 1 branch
BranchTrueFalseTotal
unless $slot_info 0 0 0
128 sprintf "%s: mutex table problem. No mutex"
129 . " row could be found for MTXCNF_ID %d, slot %d" => __PACKAGE__,
130 $self->mutex_config_id, $slot
131 ) unless $slot_info;
132 0 $HAVELOCK = 1;
133 0 last;
134 }
135 0 return $HAVELOCK;
136 }
137  
138 0 ▶sub release_mutex {
Subroutine release_mutex: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 $self->log(
141 sprintf '%s[%08d]@%s (%s) release_mutex "%s"' =>
142 map($self->$_, qw/
143 program_name pid hostname dbinst application/)
144 );
145 0 $self->storage->dbh->rollback;
146 0 $self->storage->dbh->disconnect;
147 }
148 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
149  
150 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
151 0 my ($self, $error) = @_;
152 0 my $fatal = {
153 to => ADMINADDR,
154 subj => sprintf('[%s] MUTEX', $self->hostname),
155 body => sprintf('[%s] %s', $self->hostname, $error),
156 };
157 0 mail($fatal);
158 0 die "FATAL ERROR: $fatal->{body}\n";
159 }
160  
161 # XXX: use Log::Dispatch to log the mutex messages to the mutex log as well
162 0 ▶sub log {
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
163 0 my $self = shift;
164 0 my $message = localtime() . " " . shift(@_);
165 0 ▶ 1 while chomp $message;
Branch: 1 branch
BranchTrueFalseTotal
chomp $message and do { '???' } 0 0 0
166 0 $self->SUPER::log->debug($message);
167 0 open my $log_fh, '>>', '/tmp/mutex.log';
168 0 print $log_fh "$message\n";
169 0 close $log_fh;
170 }
171  
172 0 ▶sub mail {
Subroutine mail: not called CC=7 SCAR=40.3
Pod documented
173 0 ▶ shift if $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 0 0 0
174 0 my $init = shift;
175 0 my %P;
176 0 ▶ if ($init && ref $init eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($init and ref $init eq 'HASH') { } 0 0 0
Condition0%0/3
$init and ref $init eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$init and ref $init eq "HASH"
$init ref $init eq "HASH"
Truth table0%0/3
A and B
A= $initB= ref $init eq "HASH"
ABresult
0X0
100
111
177 0 local $_;
178 0 $P{$_} = $init->{$_} for qw/to subj body/;
179 0 ▶ $P{to} ||= ADMINADDR;
Condition0%0/2
$P{"to"} ||= "service-admin\@domain.univie.ac.at"
l!l
00
MC/DC0%0/1
$P{"to"} ||= "service-admin\@domain.univie.ac.at"
$P{"to"}
Truth table0%0/2
A
A= $P{"to"}
Aresult
11
00
180 } else {
181 0 $P{to} = ADMINADDR;
182 0 $P{subj} = "Mutex Crash";
183 0 ▶ $P{body} = $init ? $init : 'Bitte manuell pruefen.';
Branch: 1 branch
BranchTrueFalseTotal
$init ? : 0 0 0
184 }
185 0 my $exc;
186 0 ($exc = $0) =~ s|.*/||;
187 0 $P{body} .= "\n$exc, " . localtime() . "\n";
188 ## no critic (ProhibitTwoArgOpen)
189 0 open my $mail_fh, qq/| mail -s '$P{subj}' $P{to}/;
190 0 print $mail_fh $P{body};
191 0 close $mail_fh;
192 }
193 2 1;
194  
195  
196 __END__
« Conveyor/Lock.pm ↑ Summary Conveyor/Service/Interface/Shell.pm »