Workflow/Persister/File.pm

filter: Statementstmt 93.9% 124 / 132 Branchbran 50.0% 10 / 20 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 23 / 23 Podpod 100% 8 / 8 totaltotal 86.5% 167 / 193 CC 15 SCAR 9.4
CC
15
Cov
86%
init
6.1
serialize_object
4.5
create_history
3.0
CRAP 2.6
« Workflow/Validator/HasRequiredField.pm ↑ Summary Workflow/History.pm »
1 package Workflow::Persister::File;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use parent qw( Workflow::Persister );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Spec::Functions qw( catdir catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Workflow::Exception qw( configuration_error persist_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Workflow::Persister::RandomId;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use File::Slurp qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 $Workflow::Persister::File::VERSION = '2.12';
16  
17 4 my @FIELDS = qw( path );
18 4 __PACKAGE__->mk_accessors(@FIELDS);
19  
20 3 ▶sub init {
Subroutine init: called CC=5 SCAR=18.0
Pod documented
21 3 my ( $self, $params ) = @_;
22 3 $self->SUPER::init($params);
23 3 ▶ unless ( $self->use_uuid eq 'yes' || $self->use_random eq 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->use_uuid eq "yes" or $self->use_random eq "yes") 3 0 3
Condition33%1/3
$self->use_uuid eq "yes" or $self->use_random eq "yes"
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->use_uuid eq "yes" or $self->use_random eq "yes"
$self->use_uuid eq "yes" $self->use_random eq "yes"
Truth table33%1/3
A or B
A= $self->use_uuid eq "yes"B= $self->use_random eq "yes"
ABresult
1X1
011
000
24 3 $self->use_random('yes');
25 }
26 3 $self->assign_generators($params);
27 3 ▶ unless ( $params->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"path"}) 0 3 3
28 0 configuration_error "The file persister must have the 'path' ",
29 "specified in the configuration";
30 }
31 3 ▶ unless ( -d $params->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $params->{"path"}) 0 3 3
32 0 configuration_error "The file persister must have a valid directory ",
33 "specified in the 'path' key of the configuration ",
34 "(given: '$params->{path}')";
35 }
36 $self->log->info(
37 3 "Using path for workflows and histories '$params->{path}'");
38 3 $self->path( $params->{path} );
39 }
40  
41 6 ▶sub create_workflow {
Subroutine create_workflow: called CC=2 SCAR=6.9
Pod documented
42 6 my ( $self, $wf ) = @_;
43 6 my $generator = $self->workflow_id_generator;
44 6 my $wf_id = $generator->pre_fetch_id();
45 6 $wf->id($wf_id);
46 6 $self->log->debug("Generated workflow ID '$wf_id'");
47 6 $self->_serialize_workflow($wf);
48 6 my $full_history_path = $self->_get_history_path($wf);
49 ## no critic (ProhibitMagicNumbers)
50 6 ▶ mkdir( $full_history_path, 0777 )
Branch: 1 branch
BranchTrueFalseTotal
mkdir $full_history_path, 511 or persist_error("Cannot create history dir '${full_history_path}': $!") 6 0 6
51 || persist_error "Cannot create history dir '$full_history_path': $!";
52  
53 6 return $wf_id;
54 }
55  
56 4 ▶sub fetch_workflow {
Subroutine fetch_workflow: called CC=2 SCAR=7.4
Pod documented
57 4 my ( $self, $wf_id ) = @_;
58 4 my $full_path = $self->_get_workflow_path($wf_id);
59 4 $self->log->debug("Checking to see if workflow exists in '$full_path'");
60 4 ▶ unless ( -f $full_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $full_path) 0 4 4
61 0 $self->log->error("No file at path '$full_path'");
62 0 persist_error "No workflow with ID '$wf_id' is available";
63 }
64 4 $self->log->debug("File exists, reconstituting workflow");
65 4 my $wf_info;
66 try {
67 4 $wf_info = $self->constitute_object($full_path);
68 }
69 4 catch ($error) {
70 0 persist_error "Cannot reconstitute data from file for ",
71 "workflow '$wf_id': $error";
72 }
73 4 return $wf_info;
74 }
75  
76 2 ▶sub update_workflow {
Subroutine update_workflow: called CC=1 SCAR=0.0
Pod documented
77 2 my ( $self, $wf ) = @_;
78 2 $self->_serialize_workflow($wf);
79 }
80  
81 8 ▶sub create_history {
Subroutine create_history: called CC=3 SCAR=11.1
Pod documented
82 8 my ( $self, $wf, @history ) = @_;
83 8 my $generator = $self->history_id_generator;
84 8 my $history_dir = $self->_get_history_path($wf);
85 8 $self->log->info("Will use directory '$history_dir' for history");
86 8 foreach my $history (@history) {
87 7 ▶ if ( $history->is_saved ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($history->is_saved) 0 7 7
88 0 $self->log->debug("History object saved, skipping...");
89 0 next;
90 }
91 7 $self->log->debug("History object unsaved, continuing...");
92 7 my $history_id = $generator->pre_fetch_id();
93 7 $history->id($history_id);
94 7 my $history_file = catfile( $history_dir, $history_id );
95 # Serialize as hash so reconstituting the object returns a hash again
96 # we need to return a list of hashes when returning the history list.
97 7 $self->serialize_object( $history_file, { %$history } );
98 7 $self->log->info("Created history object '$history_id' ok");
99 7 $history->set_saved();
100 }
101 }
102  
103 2 ▶sub fetch_history {
Subroutine fetch_history: called CC=3 SCAR=11.0
Pod documented
104 2 my ( $self, $wf ) = @_;
105 2 my $history_dir = $self->_get_history_path($wf);
106 2 $self->log->debug("Trying to read history files from dir '$history_dir'");
107 2 ▶ opendir( my $hist, $history_dir )
Branch: 1 branch
BranchTrueFalseTotal
opendir my $hist, $history_dir or persist_error("Cannot read history from '${history_dir}': $!") 2 0 2
108 || persist_error "Cannot read history from '$history_dir': $!";
109 7 my @history_files = grep { -f $_ }
110 2 map { catfile( $history_dir, $_ ) } readdir $hist;
111 2 closedir $hist;
112 2 my @histories = ();
113  
114 2 foreach my $history_file (@history_files) {
115 3 $self->log->debug("Reading history from file '$history_file'");
116 3 my $history = $self->constitute_object($history_file);
117 3 push @histories, $history;
118 }
119 2 return @histories;
120 }
121  
122 8 ▶sub _serialize_workflow {
Subroutine _serialize_workflow: called CC=1 SCAR=0.0
123 8 my ( $self, $wf ) = @_;
124 8 local $Data::Dumper::Indent = 1;
125 8 my $full_path = $self->_get_workflow_path( $wf->id );
126 8 $self->log->debug("Trying to write workflow to '$full_path'");
127 my %wf_info = (
128 id => $wf->id,
129 state => $wf->state,
130 last_update => $wf->last_update,
131 type => $wf->type,
132 8 context => { %{$wf->context->{PARAMS} } },
133 );
134 8 $self->serialize_object( $full_path, \%wf_info );
135 8 $self->log->debug("Wrote workflow ok");
136 }
137  
138 17 ▶sub serialize_object {
Subroutine serialize_object: called CC=4 SCAR=15.0
Pod documented
139 17 my ( $self, $path, $object ) = @_;
140 17 $self->log->info( "Trying to save object of type '",
141 ref($object), "' ", "to path '$path'" );
142 17 ▶ open( THINGY, '>', $path )
Branch: 1 branch
BranchTrueFalseTotal
open THINGY, ">", $path or persist_error("Cannot write to '${path}': $!") 17 0 17
143 || persist_error "Cannot write to '$path': $!";
144 17 ▶ print THINGY Dumper($object)
Condition33%1/3
Dumper($object) || persist_error("Error writing to '${path}': $!")
l!l&&r!l&&!r
1700
MC/DC0%0/2
Dumper($object) || persist_error("Error writing to '${path}': $!")
Dumper($object) persist_error("Error writing to '${path}': $!")
Truth table33%1/3
A || B
A= Dumper($object)B= persist_error("Error writing to '${path}': $!")
ABresult
1X1
011
000
145 || persist_error "Error writing to '$path': $!";
146 17 ▶ close(THINGY) || persist_error "Cannot close '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
close THINGY or persist_error("Cannot close '${path}': $!") 17 0 17
147 17 $self->log->debug("Wrote object to file ok");
148 }
149  
150 9 ▶sub constitute_object {
Subroutine constitute_object: called CC=2 SCAR=7.0
Pod documented
151 9 my ( $self, $object_path ) = @_;
152  
153 9 my $content = slurp($object_path);
154  
155 4 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 9 my $object;
157 my $error;
158 9 my $success = do {
159 9 local $@;
160 9 my $rv = eval "\$object = do { $content }; 1;";
161 9 $error = $EVAL_ERROR;
162 9 $rv;
163 };
164 9 ▶ if (not $success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 9 9
165 0 die $error;
166 }
167 9 return $object;
168 }
169  
170 12 ▶sub _get_workflow_path {
Subroutine _get_workflow_path: called CC=1 SCAR=0.0
171 12 my ( $self, $wf_id ) = @_;
172 12 $self->log->info( "Creating workflow file from '",
173 $self->path, "' ", "and ID '$wf_id'" );
174 12 return catfile( $self->path, $wf_id . '_workflow' );
175 }
176  
177 16 ▶sub _get_history_path {
Subroutine _get_history_path: called CC=1 SCAR=0.0
178 16 my ( $self, $wf ) = @_;
179 16 return catdir( $self->path, $wf->id . '_history' );
180 }
181  
182 4 1;
183  
184 __END__
« Workflow/Validator/HasRequiredField.pm ↑ Summary Workflow/History.pm »