Zapp/Task.pm

filter: Statementstmt 23.4% 22 / 94 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 31.5% 6 / 19 Podpod 50.0% 5 / 10 totaltotal 18.7% 33 / 176 CC 28 SCAR 35.6
CC
28
Cov
16%
set
110.0
process_input
30.0
process_output
30.0
CRAP 35.1
« Zapp/Controller/Trigger.pm ↑ Summary Zapp/Controller/Run.pm »
1 package Zapp::Task;
2 # ABSTRACT: Base class for individual steps in a job
3  
4 #pod =head1 SYNOPSIS
5 #pod
6 #pod package My::Task::Greet;
7 #pod use Mojo::Base 'Zapp::Task', -signatures;
8 #pod
9 #pod # Perform the task
10 #pod sub run( $self, $input ) {
11 #pod return $self->fail( 'No-one to greet' ) if !$input->{who};
12 #pod return $self->finish({
13 #pod greeting => "Hello, $input->{who}!",
14 #pod });
15 #pod }
16 #pod
17 #pod 1;
18 #pod
19 #pod __DATA__
20 #pod @@ input.html.ep
21 #pod %# Display the form to configure this task
22 #pod %= text_field 'who', value => $input->{who}
23 #pod
24 #pod @@ output.html.ep
25 #pod %# Show the result of this task
26 #pod %# XXX: Switch to $task->{error} if it's an actual error
27 #pod % if ( !ref $task->{output} ) {
28 #pod <p>I couldn't send a greeting: <%= $task->{output} %></p>
29 #pod % }
30 #pod % else {
31 #pod <p>I sent a greeting of <q><%= $task->{output}{greeting} %></q></p>
32 #pod % }
33 #pod
34 #pod =head1 DESCRIPTION
35 #pod
36 #pod L<Zapp::Task> is the base class for all tasks. Tasks are the individual
37 #pod steps of a job. Each task has an input template to configure its input
38 #pod and an output template to display its output. Task input and output are
39 #pod declared with JSON Schema (see the L</schema> method), and input will be
40 #pod processed with L<Zapp::Formula> before being given to L</run>.
41 #pod
42 #pod =head2 Creating a Task
43 #pod
44 #pod To create a task, start by extending this class. Create a C<run> method
45 #pod that takes one argument, C<$input>. Inside this method, do the work you
46 #pod want to do. Then, in the C<DATA> section of your module, create two
47 #pod templates: C<input.html.ep> for the input form, and C<output.html.ep>
48 #pod for the output display.
49 #pod
50 #pod The input form should have a set of fields. Field names will be used as
51 #pod the keys to the C<$input> hash reference given to the C<run> method. You
52 #pod can build complex data structures using C<[\d]> to make arrays and C<.>
53 #pod to nest hashes (see L<Zapp::Util/build_data_from_params>).
54 #pod
55 #pod <!-- { name => 'Planet Express', loc => { city => 'New New York' } } -->
56 #pod <input name="name" value="Planet Express">
57 #pod <input name="loc.city" value="New New York">
58 #pod
59 #pod Input forms can also use special C<data-zapp-*> attributes to add
60 #pod dynamic features without writing the JavaScript yourself. See
61 #pod L<Mojo::Util/parse_zapp_attrs>.
62 #pod
63 #pod Your C<run> method should do the actual work, and then call either
64 #pod C<finish> (for success) or C<fail> (for failure) with the output data.
65 #pod This data will be given to the C<output.html.ep> template for display.
66 #pod
67 #pod =head1 SEE ALSO
68 #pod
69 #pod L<Zapp::Task::Action>, L<Minion::Job>, L<Zapp>
70 #pod
71 #pod =cut
72  
73 13 ▶use Mojo::Base 'Minion::Job', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 13 ▶use List::Util qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
75 13 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 13 ▶use Mojo::JSON qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
77 13 ▶use Zapp::Util qw( get_path_from_data get_path_from_schema );
Subroutine BEGIN: called CC=1 SCAR=0.0
78 13 ▶use Yancy::Util qw( currym );
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 ▶has zapp_task => sub( $self ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my ( $task ) = $self->app->yancy->list( zapp_run_tasks => { job_id => $self->id } );
82 0 return $task;
83 13 };
84  
85 ▶has zapp_run => sub( $self ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 my $task = $self->zapp_task;
87 0 return $self->app->yancy->get( zapp_runs => $task->{run_id} );
88 13 };
89  
90 # Cached lookups of output from run input and output from other tasks in
91 # this run. Run input is added here. Task output is filled-in by context()
92 # XXX: Make a Zapp::Run class to put this code, accessible from
93 # $self->app->zapp->run()
94 ▶has _context => sub( $self ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
95 0 my $run_input = decode_json( $self->zapp_run->{ input } );
96 0 my %context;
97 0 for my $input ( @$run_input ) {
98 my $type = $self->app->zapp->types->{ $input->{type} }
99 0 ▶ or die qq{Could not find type "$input->{type}"};
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->app->zapp->types->{$input->{"type"}} or die qq[Could not find type "$input->{"type"}"] 0 0 0
100 0 $context{ $input->{name} } = $type->task_input( $input->{config}, $input->{value} );
101 }
102 0 return \%context;
103 13 };
104  
105 ▶sub new( $class, @args ) {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
106 0 my $self = $class->SUPER::new( @args );
107 # Process the initial arguments passed-in
108 0 $self->args( $self->args );
109 0 return $self;
110 }
111  
112 ▶sub set( $self, %values ) {
Subroutine set: not called CC=10 SCAR=47.0
Pod undocumented
113 $self->app->yancy->backend->set(
114 zapp_run_tasks => $self->zapp_task->{task_id},
115 0 \%values,
116 );
117 0 ▶ if ( exists $values{state} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $values{"state"}) 0 0 0
118 0 my $run = $self->zapp_run;
119 0 my $run_state = $run->{state};
120 0 ▶ if ( $values{state} =~ /(active|failed|stopped|killed)/ && $run->{state} ne $values{state} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($values{'state'} =~ /(active|failed|stopped|killed)/u and $run->{'state'} ne $values{'state'}) { } 0 0 0
elsif ($values{'state'} =~ /(inactive|finished)/u) { } 0 0 0
Condition0%0/3
$values{"state"} =~ /(active|failed|stopped|killed)/u and $run->{"state"} ne $values{"state"}
!ll&&!rl&&r
000
MC/DC0%0/2
$values{"state"} =~ /(active|failed|stopped|killed)/u and $run->{"state"} ne $values{"state"}
$values{"state"} =~ /(active|failed|stopped|killed)/u $run->{"state"} ne $values{"state"}
Truth table0%0/3
A and B
A= $values{"state"} =~ /(active|failed|stopped|killed)/uB= $run->{"state"} ne $values{"state"}
ABresult
0X0
100
111
121 # One job in these states can change the run state
122 0 $run_state = $values{state};
123 }
124 elsif ( $values{state} =~ /(inactive|finished)/ ) {
125 # All tasks must be in this state to change the run state
126 0 my @task_states = uniq map $_->{state}, $self->app->yancy->list( zapp_run_tasks => { $run->%{'run_id'} } );
127 0 ▶ if ( @task_states == 1 && $task_states[0] eq $values{state} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@task_states == 1 and $task_states[0] eq $values{"state"}) 0 0 0
Condition0%0/3
@task_states == 1 and $task_states[0] eq $values{"state"}
!ll&&!rl&&r
000
MC/DC0%0/2
@task_states == 1 and $task_states[0] eq $values{"state"}
@task_states == 1 $task_states[0] eq $values{"state"}
Truth table0%0/3
A and B
A= @task_states == 1B= $task_states[0] eq $values{"state"}
ABresult
0X0
100
111
128 0 $run_state = $values{state};
129 }
130 }
131  
132 0 ▶ if ( $run_state ne $run->{state} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run_state ne $run->{"state"}) 0 0 0
133 $self->app->yancy->backend->set(
134 zapp_runs => $run->{run_id},
135 {
136 state => $run_state,
137 (
138 $run_state eq 'active' ? ( started => Time::Piece->new( $self->info->{started} )->datetime )
139 0 ▶ : $run_state ne 'inactive' ? ( finished => Time::Piece->new( $self->info->{finished} )->datetime )
Branch: 2 branches
BranchTrueFalseTotal
$run_state eq 'active' ? : 0 0 0
$run_state ne 'inactive' ? : 0 0 0
140 : ()
141 ),
142 },
143 );
144 }
145 }
146 }
147  
148 ▶sub context( $self, $var ) {
Subroutine context: not called CC=4 SCAR=30.0
Pod undocumented
149 0 my $context = $self->_context;
150 0 my ( $name ) = $var =~ m{^([^\[.]+)};
151 0 ▶ if ( !$context->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->{$name}) 0 0 0
152 my ( $task ) = $self->app->yancy->list(
153 zapp_run_tasks => {
154 0 $self->zapp_run->%{'run_id'},
155 name => $name,
156 },
157 );
158 0 ▶ if ( $task && $task->{state} eq 'finished' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task and $task->{"state"} eq "finished") 0 0 0
Condition0%0/3
$task and $task->{"state"} eq "finished"
!ll&&!rl&&r
000
MC/DC0%0/2
$task and $task->{"state"} eq "finished"
$task $task->{"state"} eq "finished"
Truth table0%0/3
A and B
A= $taskB= $task->{"state"} eq "finished"
ABresult
0X0
100
111
159 0 $context->{ $name } = decode_json( $task->{output} );
160 }
161 }
162 0 return get_path_from_data( $var, $context );
163 }
164  
165 0 ▶sub args( $self, $new_args=undef ) {
Subroutine args: not called CC=3 SCAR=24.8
Pod documented
166 0 ▶ if ( $new_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_args) 0 0 0
167 # Process before storing
168 0 my $args = $self->process_input( $new_args );
169 0 return $self->SUPER::args( $args );
170 }
171 0 return $self->SUPER::args;
172 }
173  
174 ▶sub execute( $self, @args ) {
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
175 0 $self->set( state => 'active' );
176 0 return $self->SUPER::execute( @args );
177 }
178  
179 0 ▶sub finish( $self, $output=undef ) {
Subroutine finish: not called CC=3 SCAR=24.8
Pod documented
180 # Minion calls this again while reaping the child process, so bail
181 # out if we're in the parent process after having started a child.
182 # XXX: This may be a problem when trying to run a job synchronously
183 # in a web request...
184 0 ▶ return $self->SUPER::finish if $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} 0 0 0
185  
186 0 my $run_job = $self->zapp_task;
187 0 my ( $run_id, $task_id ) = $run_job->@{qw( run_id task_id )};
188  
189 # XXX: Run output through task_output
190  
191 0 $self->app->yancy->backend->set(
192 zapp_run_tasks => $task_id,
193 { output => encode_json( $output ) },
194 );
195  
196 0 my $ok = $self->SUPER::finish( $output );
197 # Set state after so run `finished` timestamp can be set
198 0 $self->set( state => 'finished' );
199 0 return $ok;
200 }
201  
202 0 ▶sub fail( $self, $output=undef ) {
Subroutine fail: not called CC=2 SCAR=17.9
Pod documented
203 0 $self->set( state => 'failed' );
204 0 my $run_job = $self->zapp_task;
205 0 my ( $run_id, $task_id ) = $run_job->@{qw( run_id task_id )};
206 # XXX: Run output through task_output
207 0 $self->app->yancy->backend->set(
208 zapp_run_tasks => $task_id,
209 { output => encode_json( $output ) },
210 );
211 0 return $self->SUPER::fail( $output );
212 }
213  
214 # XXX: Process input and output are the same subroutines with two small
215 # changes: 1. task_(input|output) 2. input evals formulas
216  
217 ▶sub process_input( $self, $input ) {
Subroutine process_input: not called CC=5 SCAR=34.0
Pod undocumented
218 0 ▶ if ( !ref $input ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $input) { } 0 0 0
elsif (ref $input eq 'ARRAY') { } 0 0 0
elsif (ref $input eq 'HASH') { } 0 0 0
219 0 ▶ if ( $input =~ /^=(?!=)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^=(?!=)/u) 0 0 0
220 0 my $expr = substr $input, 1;
221 0 $input = $self->app->formula->eval( $expr, currym( $self, 'context' ) );
222 }
223 # XXX: Run through task_input
224 0 return $input;
225 }
226 elsif ( ref $input eq 'ARRAY' ) {
227 return [
228 0 map { $self->process_input( $_ ) }
229 $input->@*
230 ];
231 }
232 elsif ( ref $input eq 'HASH' ) {
233 return {
234 0 map { $_ => $self->process_input( $input->{$_} ) }
235 keys $input->%*
236 };
237 }
238 0 die "Unknown ref type for data: " . ref $input;
239 }
240  
241 0 ▶sub process_output( $self, $output, $path='' ) {
Subroutine process_output: not called CC=5 SCAR=34.0
Pod undocumented
242 0 ▶ if ( !ref $output ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $output) { } 0 0 0
elsif (ref $output eq 'ARRAY') { } 0 0 0
elsif (ref $output eq 'HASH') { } 0 0 0
243 # XXX: Find type in schema and run through task_output
244 0 my $schema = get_path_from_schema( $path, $self->schema->{output} );
245 0 return $output;
246 }
247 elsif ( ref $output eq 'ARRAY' ) {
248 return [
249 0 map { $self->process_output( $output->[ $_ ], $path . "[$_]" ) }
250 0..$output->$#*
251 ];
252 }
253 elsif ( ref $output eq 'HASH' ) {
254 return {
255 0 map { $_ => $self->process_output( $output->{$_}, $path . ".$_" ) }
256 keys $output->%*
257 };
258 }
259 0 die "Unknown ref type for data: " . ref $output;
260 }
261  
262 ▶sub schema( $class ) {
Subroutine schema: not called CC=1 SCAR=6.9
Pod undocumented
263 return {
264 0 input => {
265 type => 'array',
266 },
267 output => {
268 type => 'string',
269 },
270 };
271 }
272  
273 13 1;
274  
275 __END__
« Zapp/Controller/Trigger.pm ↑ Summary Zapp/Controller/Run.pm »