blib/lib/HPCI/SystemLogger.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 129 CC 6 SCAR 16.9
CC
6
Cov
0%
write_system_log
12.0
_getlock
12.0
_log_stages
6.0
CRAP 5.4
1 package HPCI::SystemLogger;
2  
3 use namespace::autoclean;
4  
5 use Moose::Role;
6 use Moose::Util::TypeConstraints;
7  
8 use autodie;
9 use Carp;
10  
11 use feature qw(say);
12  
13 use MooseX::Types::Path::Class qw(Dir File);
14 use Fcntl qw(:flock);
15 use FindBin;
16 use DateTime;
17  
18 =head1 NAME
19  
20 HPCI::SystemLogger - This role provides an optional system logger
21  
22 =head1 DESCRIPTION
23  
24 Defines a systemlogger attribute which logs the return status of
25 the group exwecution.
26  
27 Not provided by default, but it can usefully included with a HPCI::LocalConfig
28 to set a local system logging facility. The saved logs can be examined for
29 such purposes as scanning for recurring errors coming from individual hardware
30 components of the cluster.
31  
32 =head1 Attributes
33  
34 =over 4
35  
36 =item * systemlogger - a code reference
37  
38 This code reference (if provided) is called at the completion of
39 group execution. It is called with two parameters: the group object,
40 and the return_status hash.
41  
42 The provider of this attribute (usually the HPCI::LocalConfig module)
43 is responsible to providing additional code to determine the location
44 and format of the logged data, although the B<write_system_log> method
45 provided below can be used to write info in a useable format to a
46 provided file handle.
47  
48 =item * write_system_log - method to write a result description to a file handle
49  
50 Can be used by the systemlogger to do the actual log writing, after systemlogger
51 has opened a file handle to the proper destination.
52  
53 =back
54  
55 =cut
56  
57 has 'systemlogger' => (
58 is => 'ro',
59 isa => 'CodeRef',
60 predicate => '_has_systemlogger'
61 );
62  
63 sub write_system_log {
64 my $self = shift;
65 my $path = shift;
66 my $fh = shift;
67 my $ret = shift;
68 my $lock = shift;
69 my $unlock = shift;
70 _getlock( $self, $path, $fh ) if $lock;
71 _log_ret( $self, $path, $fh, $ret );
72 _freelock( $self, $path, $fh ) if $unlock;
73 }
74  
75 sub _getlock {
76 my $self = shift;
77 my $path = shift // 'UNKNOWN PATH';
78 my $fh = shift;
79 my $lock_cnt = 0;
80 while (1) {
81 flock $fh, LOCK_EX and last;
82 $self->croak( "$0 [$$]: flock failed on $path: $!" ) if $lock_cnt > 30;
83 $self->info( "Waiting for lock on $path" ) unless $lock_cnt++;
84 sleep(2);
85 }
86 $self->info( "Acquired lock on $path" );
87 seek $fh, 2, 0; # make sure we're still at the end now that it is locked
88 }
89  
90 sub _freelock {
91 my $self = shift;
92 my $path = shift;
93 my $fh = shift;
94 flock $fh, LOCK_UN;
95 $self->info( "Released lock on $path" );
96 }
97  
98 my $groupcnt = 0;
99 my $start = DateTime->from_epoch(epoch => time);
100  
101 sub _log_ret {
102 my $self = shift;
103 my $path = shift;
104 my $fh = shift;
105 my $ret = shift;
106 say $fh "";
107 say $fh "*"x40 for 1..2;
108 say $fh "Program\t$FindBin::Bin/$FindBin::Script";
109 say $fh "ProcessID\t$$";
110 say $fh "StartTime\t", $start;
111 say $fh "User\t", scalar(getpwuid $<);
112 my $gname = $self->name;
113 say $fh "GroupName\t$gname";
114 say $fh "GroupCount\t", ++$groupcnt;
115 say $fh "GroupEndTime\t", DateTime->from_epoch(epoch => time);
116 $self->_log_stages( $fh, $gname, $ret );
117 }
118  
119 sub _log_stages {
120 my ($self, $fh, $gname, $ret, @parents) = @_;
121  
122 my @stages;
123 my @subgroups;
124  
125 map {
126 my $val = $ret->{$_};
127 push @{ ref($val) eq 'HASH' ? \@subgroups : \@stages }, [ $_, $val ];
128 } sort keys %$ret;
129  
130 for my $stage_pair (@stages) {
131 my ($stage, $runs) = @$stage_pair;
132 $stage = join( '__', @parents, $stage );
133 say $fh "StageName\t$gname\t$stage";
134 say $fh "StageAttempts\t$gname\t$stage\t", scalar( @$runs );
135 for my $i (0..$#$runs) {
136 my $pre = "\t$gname\t$stage\tRun$i\t";
137 my $run = $runs->[$i];
138 for my $k (sort keys %$run) {
139 my $v = $run->{$k};
140 say $fh "Res$pre$k\t$v";
141 }
142 }
143 }
144 for my $subgroup_pair (@subgroups) {
145 my ($subgroup, $val) = @$subgroup_pair;
146 $self->_log_stages( $fh, $gname, $val, @parents, $self->_subgroups->{$subgroup}->name );
147 }
148 }
149  
150 1;