File Coverage

blib/lib/HPC/Runner/Command/execute_job/Logger/Lock.pm
Criterion Covered Total %
statement 18 35 51.4
branch 0 4 0.0
condition n/a
subroutine 6 10 60.0
pod 1 2 50.0
total 25 51 49.0


line stmt bran cond sub pod time code
1             package HPC::Runner::Command::execute_job::Logger::Lock;
2              
3 1     1   559 use Moose::Role;
  1         3  
  1         6  
4 1     1   4573 use namespace::autoclean;
  1         4  
  1         9  
5              
6 1     1   62 use Try::Tiny;
  1         1  
  1         66  
7 1     1   7 use Time::HiRes;
  1         2  
  1         10  
8 1     1   78 use File::Spec;
  1         2  
  1         28  
9 1     1   6 use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/;
  1         2  
  1         14  
10              
11             has 'lock_file' => (
12             is => 'rw',
13             isa => Path,
14             lazy => 1,
15             coerce => 1,
16             required => 1,
17             default => sub {
18             my $self = shift;
19             my $file = File::Spec->catdir( $self->data_dir, '.lock' );
20             return $file;
21             },
22             );
23              
24             has 'logger' => (
25             is => 'rw',
26             isa => 'Str',
27             default => 'command_log',
28             );
29              
30             =head3 check_lock
31              
32             Check to see if the lock exists
33              
34             Have a max retry count to avoid infinite loops
35              
36             =cut
37              
38             sub check_lock {
39 0     0 1   my $self = shift;
40              
41 0           my $max_retries = 500;
42 0           my $x = 0;
43              
44 0           my $ret = 1;
45 0           while ( $self->lock_file->exists ) {
46 0           Time::HiRes::sleep(0.2);
47 0           $x++;
48 0 0         if($x >= $max_retries){
49 0           $ret = 0;
50 0           last;
51             }
52             }
53 0 0         if ( $x >= $max_retries ) {
54 0           $self->{$self->logger}->warn('Logger::JSON Error: We exited the lock!');
55             }
56              
57 0           return $ret;
58             }
59              
60             sub write_lock {
61 0     0 0   my $self = shift;
62              
63             try {
64 0     0     $self->lock_file->touchpath;
65             }
66             catch {
67 0     0     $self->{$self->logger}->warn(
68             'Logger::JSON Error: We were not able to write '
69             . $self->lock_file->stringify );
70 0           };
71             }
72              
73             1;