LongSteps/Storage/Memory.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 87.5% 7 / 8 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 93.3% 14 / 15 Podpod 75.0% 3 / 4 totaltotal 96.0% 96 / 100 CC 8 SCAR 8.2
CC
8
Cov
97%
prepare_due_processes
5.0
update
3.0
find_process
2.0
CRAP 2.3
« LongSteps/Storage/DBIxClass.pm ↑ Summary LongSteps/Process.pm »
1 package Schedule::LongSteps::Storage::Memory;
2 14 $Schedule::LongSteps::Storage::Memory::VERSION = '0.023';
3 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 extends qw/Schedule::LongSteps::Storage/;
5  
6 14 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 ▶use Log::Any qw/$log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Schedule::LongSteps::Storage::DBIxClass - DBIx::Class based storage.
13  
14 =head1 SYNOPSIS
15  
16 my $storage = Schedule::LongSteps::Storage::Memory->new();
17  
18 Then build and use a L<Schedule::LongSteps> object:
19  
20 my $long_steps = Schedule::LongSteps->new({ storage => $storage });
21  
22 ...
23  
24  
25 =cut
26  
27 13 ▶has 'processes' => ( is => 'ro', isa => 'ArrayRef[Schedule::LongSteps::Storage::Memory::Process]', default => sub{ []; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 =head2 prepare_due_processes
30  
31 See L<Schedule::LongSteps::Storage>
32  
33 =cut
34  
35 42 ▶sub prepare_due_processes{
Subroutine prepare_due_processes: called CC=5 SCAR=16.1
Pod documented
36 42 my ($self) = @_;
37  
38 42 my $now = DateTime->now();
39 42 my $uuid = $self->uuid()->create_str();
40  
41 42 my @to_run = ();
42 42 foreach my $process ( @{ $self->processes() } ){
43 56 ▶ if( $process->run_at()
Branch: 1 branch
BranchTrueFalseTotal
if ($process->run_at and not $process->run_id and "DateTime"->compare($process->run_at, $now) <= 0) 35 21 56
Condition100%6/6
$process->run_at and not $process->run_id and "DateTime"->compare($process->run_at, $now) <= 0
!ll&&!rl&&r
20135
$process->run_at and not $process->run_id
!ll&&!rl&&r
13736
MC/DC100%3/3
$process->run_at and not $process->run_id and "DateTime"->compare($process->run_at, $now) <= 0
$process->run_at not $process->run_id "DateTime"->compare($process->run_at, $now) <= 0
Truth table100%4/4
A and not(B) and C
A= $process->run_atB= not $process->run_idC= "DateTime"->compare($process->run_at, $now) <= 0
ABCresult
0XX0
10X0
1100
1111
44 && !$process->run_id()
45 && ( DateTime->compare( $process->run_at(), $now ) <= 0 ) ){
46 35 $process->update({
47 run_id => $uuid,
48 status => 'running'
49 });
50 35 push @to_run , $process;
51 }
52 }
53 42 return @to_run;
54 }
55  
56 =head2 find_process
57  
58 See L<Schedule::LongSteps::Storage>
59  
60 =cut
61  
62 19 ▶sub find_process{
Subroutine find_process: called CC=2 SCAR=6.9
Pod documented
63 19 my ($self, $pid) = @_;
64 19 $log->trace("Looking up process ID=$pid");
65 19 my ( $match ) = grep{ $_->id() == $pid } @{$self->processes()};
66 19 ▶ my $log_message = $match ? "Found: $match" : "Could not find a process for $pid";
Branch: 1 branch
BranchTrueFalseTotal
$match ? : 18 1 19
67 19 $log->trace($log_message);
68 19 return $match;
69 }
70  
71  
72 =head2 create_process
73  
74 See L<Schedule::LongSteps::Storage>
75  
76 =cut
77  
78 19 ▶sub create_process{
Subroutine create_process: called CC=1 SCAR=0.0
Pod documented
79 19 my ($self, $process_properties) = @_;
80 19 my $process = Schedule::LongSteps::Storage::Memory::Process->new($process_properties);
81 19 push @{$self->processes()} , $process;
82 19 return $process;
83 }
84  
85 14 __PACKAGE__->meta->make_immutable();
86  
87 package Schedule::LongSteps::Storage::Memory::Process;
88 14 $Schedule::LongSteps::Storage::Memory::Process::VERSION = '0.023';
89 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 14 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 14 my $IDSEQUENCE = 0;
94  
95 19 ▶has 'id' => ( is => 'ro', isa => 'Int', default => sub{ ++$IDSEQUENCE ; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
96 14 has 'process_class' => ( is => 'rw', isa => 'Str', required => 1); # rw only for test. Should not changed ever.
97 14 has 'status' => ( is => 'rw', isa => 'Str', default => 'pending' );
98 14 has 'what' => ( is => 'rw' , isa => 'Str', required => 1);
99 0 ▶has 'run_at' => ( is => 'rw', isa => 'Maybe[DateTime]', default => sub{ undef; } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
100 19 ▶has 'run_id' => ( is => 'rw', isa => 'Maybe[Str]', default => sub{ undef; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
101 3 ▶has 'state' => ( is => 'rw', default => sub{ {}; });
Subroutine __ANON__: called CC=1 SCAR=0.0
102 19 ▶has 'error' => ( is => 'rw', isa => 'Maybe[Str]', default => sub{ undef; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
103  
104 72 ▶sub update{
Subroutine update: called CC=3 SCAR=11.0
Pod undocumented
105 72 my ($self, $update_properties) = @_;
106 72 ▶ defined($update_properties) or ( $update_properties = {} );
Branch: 1 branch
BranchTrueFalseTotal
defined $update_properties or $update_properties = {} 72 0 72
107  
108 # use Data::Dumper;
109 # warn "Updating with ".Dumper($update_properties);
110  
111 72 ▶ while( my ( $key, $value ) = each %{$update_properties} ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$update_properties;}) 219 72 291
112 219 $self->$key( $value );
113 }
114 }
115  
116 14 __PACKAGE__->meta->make_immutable();
« LongSteps/Storage/DBIxClass.pm ↑ Summary LongSteps/Process.pm »