Zapp.pm

filter: Statementstmt 13.7% 34 / 247 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 42.8% 9 / 21 Podpod 87.5% 7 / 8 totaltotal 13.6% 50 / 367 CC 67 SCAR 46.6
CC
67
Cov
11%
enqueue_run
306.0
enqueue_tasks
132.0
enqueue_plan
110.0
CRAP 105.4
« Zapp/Task/Script.pm ↑ Summary Zapp/Task/Test.pm »
1 package Zapp;
2 18 our $VERSION = '0.004';
3 # ABSTRACT: Plan building, job creating web app
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # Start the web application
8 #pod zapp daemon
9 #pod
10 #pod # Start the task runner
11 #pod zapp minion worker
12 #pod
13 #pod =head1 DESCRIPTION
14 #pod
15 #pod Zapp is a graphical workflow builder that provides a UI to build and
16 #pod execute jobs.
17 #pod
18 #pod For documentation on running and using Zapp, see L<Zapp::Guides>.
19 #pod
20 #pod This file documents the main application class, L<Zapp>. This class can
21 #pod be used to L<embed Zapp into an existing Mojolicious application|https://docs.mojolicious.org/Mojolicious/Guides/Routing#Embed-applications>, or
22 #pod can be extended to add customizations.
23 #pod
24 #pod =head1 SEE ALSO
25 #pod
26 #pod L<Yancy>, L<Mojolicious>
27 #pod
28 #pod =cut
29  
30 18 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 18 ▶use Mojo::Base 'Mojolicious', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 18 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
33 18 ▶use Yancy::Util qw( load_backend );
Subroutine BEGIN: called CC=1 SCAR=0.0
34 18 ▶use Mojo::JSON qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 18 ▶use Mojo::Loader qw( find_modules load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
36 18 ▶use Mojo::File qw( curfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
37 18 ▶use Zapp::Formula;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 #pod =attr formula
40 #pod
41 #pod The formula interpreter. Usually a L<Zapp::Formula> object.
42 #pod
43 #pod =cut
44  
45 0 ▶has formula => sub { Zapp::Formula->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
46  
47 #pod =method startup
48 #pod
49 #pod Initialize the application. Called automatically by L<Mojolicious>.
50 #pod
51 #pod =cut
52  
53 ▶sub startup( $self ) {
Subroutine startup: called CC=6 SCAR=34.8
Pod documented
54  
55 16 push @{ $self->renderer->paths }, curfile->sibling( 'Zapp', 'resources', 'templates' );
56 16 push @{ $self->static->paths }, curfile->sibling( 'Zapp', 'resources', 'public' );
57  
58 # XXX: Allow configurable backends, like Minion
59 16 $self->plugin( Config => { default => {
60 backend => 'sqlite:' . $self->home->child( 'zapp.db' ),
61 minion => { SQLite => 'sqlite:' . $self->home->child( 'zapp.db' ) },
62 } } );
63  
64 # XXX: Add migrate() method to Yancy app base class, varying by
65 # backend type. Should try to read migrations from each class in
66 # $self->isa
67 # XXX: Create this migrate() method in a role so it can also be used
68 # by Yancy::Plugins or other plugins
69 16 my $backend = load_backend( $self->config->{backend} );
70 16 my ( $db_type ) = blessed( $backend ) =~ m/([^:]+)$/;
71 16 $backend->mojodb->migrations
72 ->name( 'zapp' )
73 ->from_data( __PACKAGE__, 'migrations.' . lc $db_type . '.sql' )
74 ->migrate;
75  
76 0 $self->plugin( Minion => $self->config->{ minion }->%* );
77  
78 # XXX: Allow additional task namespaces
79 0 for my $class ( find_modules 'Zapp::Task', { recursive => 1 } ) {
80 0 ▶ next if $class eq 'Zapp::Task';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Zapp::Task" 0 0 0
81 0 ▶ if ( my $e = load_class( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($class)) 0 0 0
82 0 $self->log->error( sprintf "Could not load task class %s: %s", $class, $e );
83 0 next;
84 }
85 #; say "Adding task class: $class";
86 0 $self->minion->add_task( $class, $class );
87 }
88  
89 $self->plugin( Yancy =>
90 0 $self->config->%{qw( backend )},
91 schema => {
92 zapp_plan_inputs => {
93 # XXX: Fix read_schema to detect compound primary keys
94 'x-id-field' => [qw( plan_id name )],
95 },
96 zapp_plan_task_parents => {
97 # XXX: Fix read_schema to detect compound primary keys
98 'x-id-field' => [qw( task_id parent_task_id )],
99 },
100 },
101 );
102  
103 # Add basic types
104 0 my %base_types = (
105 string => 'Zapp::Type::Text',
106 textarea => 'Zapp::Type::Textarea',
107 number => 'Zapp::Type::Text',
108 integer => 'Zapp::Type::Text',
109 boolean => 'Zapp::Type::Text',
110 file => 'Zapp::Type::File',
111 selectbox => 'Zapp::Type::SelectBox',
112 );
113 0 ▶ $self->helper( 'zapp.types' => sub( $c ) { state %types; \%types } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 ▶ $self->helper( 'zapp.add_type' => sub( $c, $name, $type ) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
115 0 ▶ my $obj = blessed( $type ) ? $type : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($type) ? : 0 0 0
116 0 ▶ if ( !defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $obj) { } 0 0 0
117 0 ▶ if ( my $e = load_class( $type ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($type)) 0 0 0
118 0 die "Could not load type class $type: $e\n";
119 }
120 0 $obj = $type->new( app => $c->app );
121 }
122 else {
123 0 $obj->app( $c->app );
124 }
125 0 $c->zapp->types->{ $name } = $obj;
126 0 });
127 0 for my $type_name ( keys %base_types ) {
128 0 $self->zapp->add_type( $type_name, $base_types{ $type_name } );
129 }
130  
131 # XXX: Add config file for adding types
132  
133 # Add basic triggers
134 0 my %base_triggers = (
135 Webhook => 'Zapp::Trigger::Webhook',
136 );
137 0 ▶ $self->helper( 'zapp.triggers' => sub( $c ) { state %triggers; \%triggers } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 ▶ $self->helper( 'zapp.add_trigger' => sub( $c, $name, $trigger ) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
139 0 ▶ my $obj = blessed( $trigger ) ? $trigger : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($trigger) ? : 0 0 0
140 0 ▶ if ( !defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj) 0 0 0
141 0 ▶ if ( my $e = load_class( $trigger ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($trigger)) 0 0 0
142 0 die "Could not load trigger class $trigger $e\n";
143 }
144 0 $obj = $trigger->new( moniker => $name );
145 }
146 0 $obj->install( $self );
147 0 $c->zapp->triggers->{ $name } = $obj;
148 0 });
149 0 for my $trigger_name ( keys %base_triggers ) {
150 0 $self->zapp->add_trigger( $trigger_name, $base_triggers{ $trigger_name } );
151 }
152  
153 # Create/edit plans
154 # XXX: Make Yancy support this basic CRUD with relationships?
155 # XXX: Otherwise, add custom JSON API
156 0 $self->routes->get( '/plan/create' )
157 ->to( 'plan#edit_plan' )->name( 'zapp.create_plan' );
158 0 $self->routes->post( '/plan/create' )->to( 'plan#save_plan' );
159 0 $self->routes->get( '/plan/:plan_id' )
160 ->to( 'plan#get_plan' )->name( 'zapp.get_plan' );
161 0 $self->routes->get( '/plan/:plan_id/edit' )
162 ->to( 'plan#edit_plan' )->name( 'zapp.edit_plan' );
163 0 $self->routes->post( '/plan/:plan_id/edit' )->to( 'plan#save_plan' );
164 0 $self->routes->get( '/plan/:plan_id/delete' )
165 ->to( 'plan#delete_plan' )->name( 'zapp.delete_plan' );
166 0 $self->routes->post( '/plan/:plan_id/delete' )
167 ->to( 'plan#delete_plan' )->name( 'zapp.delete_plan_confirm' );
168 0 $self->routes->get( '/' )
169 ->to( 'plan#list_plans' )->name( 'zapp.list_plans' );
170  
171 # Create/view runs
172 0 $self->routes->get( '/plan/:plan_id/run', { run_id => undef } )
173 ->to( 'run#create_run' )->name( 'zapp.create_run' );
174 0 $self->routes->get( '/run/:run_id/replay' )
175 ->to( 'run#create_run' )->name( 'zapp.replay_run' );
176 0 $self->routes->post( '/run' )
177 ->to( 'run#save_run' )->name( 'zapp.save_run' );
178 0 $self->routes->get( '/run' )
179 ->to( 'run#list_runs' )->name( 'zapp.list_runs' );
180 0 $self->routes->get( '/run/:run_id' )
181 ->to( 'run#get_run' )->name( 'zapp.get_run' );
182 0 $self->routes->get( '/run/:run_id/task/:task_id' )
183 ->to( 'run#get_run_task' )->name( 'zapp.get_run_task' );
184 0 $self->routes->post( '/run/:run_id/task/:task_id/action' )
185 ->to( 'run#save_task_action' )->name( 'zapp.save_task_action' );
186 # $self->routes->get( '/run/:run_id/edit' )
187 # ->to( 'run#edit_run' )->name( 'zapp.edit_run' );
188 # $self->routes->post( '/run/:run_id/edit' )
189 # ->to( 'run#save_run' )->name( 'zapp.save_run' );
190 0 $self->routes->get( '/run/:run_id/stop' )
191 ->to( 'run#stop_run' )->name( 'zapp.stop_run' );
192 0 $self->routes->post( '/run/:run_id/stop' )
193 ->to( 'run#stop_run' )->name( 'zapp.stop_run_confirm' );
194 0 $self->routes->post( '/run/:run_id/start' )
195 ->to( 'run#start_run' )->name( 'zapp.start_run_confirm' );
196 0 $self->routes->get( '/run/:run_id/kill' )
197 ->to( 'run#kill_run' )->name( 'zapp.kill_run' );
198 0 $self->routes->post( '/run/:run_id/kill' )
199 ->to( 'run#kill_run' )->name( 'zapp.kill_run_confirm' );
200 0 $self->routes->websocket( '/run/:run_id/feed' )
201 ->to( 'run#feed_run' )->name( 'zapp.feed_run' );
202  
203 0 $self->routes->any( [qw( GET POST )], '/plan/:plan_id/trigger/:trigger_id', { trigger_id => undef } )
204 ->to( 'trigger#edit' )->name( 'zapp.edit_trigger' );
205 }
206  
207 #pod =method create_plan
208 #pod
209 #pod Create a new plan and all related data.
210 #pod
211 #pod =cut
212  
213 # XXX: Make Yancy automatically handle relationships like this
214 ▶sub create_plan( $self, $plan ) {
Subroutine create_plan: not called CC=6 SCAR=37.4
Pod documented
215 0 ▶ my @inputs = @{ delete $plan->{inputs} // [] };
Condition0%0/2
delete $plan->{"inputs"} // []
l!l
00
MC/DC0%0/1
delete $plan->{"inputs"} // []
delete $plan->{"inputs"}
Truth table0%0/2
A
A= delete $plan->{"inputs"}
Aresult
11
00
216 0 ▶ my @tasks = @{ delete $plan->{tasks} // [] };
Condition0%0/2
delete $plan->{"tasks"} // []
l!l
00
MC/DC0%0/1
delete $plan->{"tasks"} // []
delete $plan->{"tasks"}
Truth table0%0/2
A
A= delete $plan->{"tasks"}
Aresult
11
00
217 0 my $plan_id = $self->yancy->create( zapp_plans => $plan );
218  
219 0 for my $i ( 0..$#inputs ) {
220 0 $inputs[$i]{plan_id} = $plan_id;
221 0 my $input = { %{ $inputs[$i] }, rank => $i };
222 0 $self->yancy->create( zapp_plan_inputs => $input );
223 }
224  
225 0 my $prev_task_id;
226 0 for my $task ( @tasks ) {
227 0 $task->{plan_id} = $plan_id;
228 0 my $task_id = $self->yancy->create( zapp_plan_tasks => $task );
229 0 ▶ if ( $prev_task_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_task_id) 0 0 0
230 0 $self->yancy->create( zapp_plan_task_parents => {
231 task_id => $task_id,
232 parent_task_id => $prev_task_id,
233 });
234 }
235 0 $prev_task_id = $task_id;
236 0 $task->{ task_id } = $task_id;
237 }
238  
239 0 $plan->{plan_id} = $plan_id;
240 0 $plan->{tasks} = \@tasks;
241 0 $plan->{inputs} = \@inputs;
242  
243 0 return $plan;
244 }
245  
246 #pod =method get_plan
247 #pod
248 #pod Get a plan and all related data (tasks, inputs).
249 #pod
250 #pod =cut
251  
252 ▶sub get_plan( $self, $plan_id ) {
Subroutine get_plan: not called CC=7 SCAR=40.3
Pod documented
253 0 ▶ my $plan = $self->yancy->get( zapp_plans => $plan_id ) || {};
Condition0%0/2
$self->yancy->get('zapp_plans', $plan_id) || {}
l!l
00
MC/DC0%0/1
$self->yancy->get('zapp_plans', $plan_id) || {}
$self->yancy->get('zapp_plans', $plan_id)
Truth table0%0/2
A
A= $self->yancy->get('zapp_plans', $plan_id)
Aresult
11
00
254 0 ▶ if ( my $plan_id = $plan->{plan_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plan_id = $plan->{"plan_id"}) 0 0 0
255 my $tasks = $plan->{tasks} = [
256 0 $self->yancy->list( zapp_plan_tasks => { plan_id => $plan_id }, { order_by => 'task_id' } ),
257 ];
258 0 for my $task ( @$tasks ) {
259 0 $task->{input} = decode_json( $task->{input} );
260 }
261  
262 my $inputs = $plan->{inputs} = [
263 0 $self->yancy->list( zapp_plan_inputs => { plan_id => $plan_id }, { order_by => 'rank' } ),
264 ];
265 0 for my $input ( @$inputs ) {
266 0 ▶ if ( my $config = $input->{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config = $input->{"config"}) 0 0 0
267 0 $input->{config} = decode_json( $config );
268 }
269 0 ▶ if ( my $value = $input->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $input->{"value"}) 0 0 0
270 0 $input->{value} = decode_json( $value );
271 }
272 }
273 }
274 0 return $plan;
275 }
276  
277 #pod =method enqueue_plan
278 #pod
279 #pod Enqueue a plan.
280 #pod
281 #pod =cut
282  
283 0 ▶sub enqueue_plan( $self, $plan_id, $input={}, %opt ) {
Subroutine enqueue_plan: not called CC=10 SCAR=47.0
Pod documented
284 0 ▶ $opt{queue} ||= 'zapp';
Condition0%0/2
$opt{"queue"} ||= "zapp"
l!l
00
MC/DC0%0/1
$opt{"queue"} ||= "zapp"
$opt{"queue"}
Truth table0%0/2
A
A= $opt{"queue"}
Aresult
11
00
285  
286 # Create the run in the database by copying the plan
287 0 my $plan = $self->yancy->get( zapp_plans => $plan_id );
288 # XXX: Run inputs and plan inputs should either both be tables or
289 # both be JSON serialized
290 0 my @inputs = $self->yancy->list( zapp_plan_inputs => { plan_id => $plan_id }, { order_by => 'rank' } );
291 0 delete $plan->{created};
292 my $run = {
293 %$plan,
294 # XXX: Auto-encode/-decode JSON fields in Yancy schema
295 input => encode_json([
296 map +{
297 $_->%{qw( name label type description )},
298 config => decode_json( $_->{config} // 'null' ),
299 value => $input->{ $_->{name} },
300 },
301 0 ▶ @inputs,
Condition0%0/2
$_->{"config"} // "null"
l!l
00
MC/DC0%0/1
$_->{"config"} // "null"
$_->{"config"}
Truth table0%0/2
A
A= $_->{"config"}
Aresult
11
00
302 ]),
303 };
304 0 my $run_id = $run->{run_id} = $self->yancy->create( zapp_runs => $run );
305  
306 0 my @tasks = $self->get_tasks( zapp_plan_tasks => { plan_id => $plan_id } );
307  
308 # Create the new task rows, mapping new task IDs from the old task
309 # IDs for parent/child relationships.
310 0 my %task_id_map;
311 0 for my $task ( @tasks ) {
312 0 delete $task->{ $_ } for qw( plan_id );
313 0 $task->{run_id} = $run_id;
314 0 ▶ my $parents = $task->{parents} ? delete $task->{parents} : [];
Branch: 1 branch
BranchTrueFalseTotal
$task->{'parents'} ? : 0 0 0
315 0 my $old_task_id = $task->{plan_task_id} = delete $task->{task_id};
316 0 my $new_task_id = $self->yancy->backend->create( zapp_run_tasks => $task );
317 0 $task->{task_id} = $task_id_map{ $old_task_id } = $new_task_id;
318 #$task->{task_id} = $new_task_id;
319 0 $task->{parents} = [ map { $task_id_map{ $_ } } @$parents ];
320 0 for my $parent_task_id ( @{ $task->{parents} } ) {
321 $self->yancy->backend->create( zapp_run_task_parents => {
322 0 $task->%{'task_id'},
323 parent_task_id => $parent_task_id,
324 } );
325 }
326 }
327 0 $run->{tasks} = \@tasks;
328  
329 0 my $jobs = $self->enqueue_tasks( $input, @tasks );
330 0 for my $i ( 0..$#$jobs ) {
331 0 my $job = $jobs->[$i];
332  
333 0 my ( $task ) = grep { $_->{task_id} eq $job->{task_id} } $run->{tasks}->@*;
334 0 $task->{$_} = $job->{$_} for keys %$job;
335  
336 0 $self->yancy->backend->set( zapp_run_tasks => $job->{task_id}, $job );
337 }
338  
339 0 return $run;
340 }
341  
342 #pod =method get_tasks
343 #pod
344 #pod Get the tasks for a plan/run from the given table.
345 #pod
346 #pod =cut
347  
348 ▶sub get_tasks( $self, $table, $search ) {
Subroutine get_tasks: not called CC=6 SCAR=37.4
Pod documented
349 0 my $parents_table = $table =~ s/s$/_parents/r;
350 0 my @tasks = $self->yancy->list( $table => $search );
351  
352 0 for my $task ( @tasks ) {
353 $task->{parents} = [
354 0 map { $_->{parent_task_id} }
355 0 $self->yancy->list( $parents_table => { $task->%{'task_id'} } )
356 ];
357 #; $self->log->debug( 'Got parents for task ' . $task->{task_id} . ': ' . join ', ', @{ $task->{parents} } );
358 }
359  
360 # Put the tasks in an order they can be created so all parent tasks
361 # are before any dependent child tasks
362 0 my @ordered_tasks;
363 0 ▶ TASK: while ( @tasks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tasks) 0 0 0
364 0 my $task = shift @tasks;
365 0 ▶ for my $parent_task_id ( @{ $task->{parents} // [] } ) {
Condition0%0/2
$task->{"parents"} // []
l!l
00
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
Aresult
11
00
366 # If there's a parent task we haven't seen yet, try again later
367 0 ▶ if ( grep { $_->{task_id} eq $parent_task_id } @tasks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->{"task_id"} eq $parent_task_id;} @tasks) 0 0 0
368 0 push @tasks, $task;
369 0 next TASK;
370 }
371 }
372 0 push @ordered_tasks, $task;
373 }
374  
375 0 return @ordered_tasks;
376 }
377  
378 #pod =method enqueue_run
379 #pod
380 #pod Re-enqueue a run.
381 #pod
382 #pod =cut
383  
384 0 ▶sub enqueue_run( $self, $old_run_id, $input=[], %opt ) {
Subroutine enqueue_run: not called CC=17 SCAR=57.2
Pod documented
385 0 ▶ $opt{queue} ||= 'zapp';
Condition0%0/2
$opt{"queue"} ||= "zapp"
l!l
00
MC/DC0%0/1
$opt{"queue"} ||= "zapp"
$opt{"queue"}
Truth table0%0/2
A
A= $opt{"queue"}
Aresult
11
00
386  
387 # Create the new run in the database by copying the old run
388 0 my $old_run = $self->yancy->get( zapp_runs => $old_run_id );
389 # XXX: Delete more from the old run
390 0 delete $old_run->{ $_ } for qw( run_id created started finished state );
391 0 my $new_run = {
392 %$old_run,
393 state => 'inactive',
394 # XXX: Auto-encode/-decode JSON fields in Yancy schema
395 input => encode_json( $input ),
396 };
397 0 my $new_run_id = $new_run->{run_id} = $self->yancy->create( zapp_runs => $new_run );
398  
399 0 my @tasks = $self->get_tasks( zapp_run_tasks => { run_id => $old_run_id } );
400 0 for my $task ( @tasks ) {
401 0 delete $task->{ $_ } for qw( job_id started finished );
402 0 $task->{ run_id } = $new_run_id;
403 0 $task->{ state } = 'inactive';
404 }
405  
406 0 ▶ if ( my $start_task_id = $opt{task_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $start_task_id = $opt{"task_id"}) 0 0 0
407 #; $self->log->debug( "Starting from task: $start_task_id" );
408 # Mark which jobs should be re-run and which should be copied.
409 # Since we know @tasks is ordered with parents before children,
410 # we can reverse it to make sure we hit children before their
411 # parents.
412 # Start with the parents of the starting task
413 my %to_copy = (
414 0 map { $_ => 1 } map { $_->{parents}->@* }
415 0 grep { $_->{task_id} eq $start_task_id }
416 @tasks
417 );
418 #; $self->log->debug( "Copying " . %to_copy );
419 0 for my $task ( reverse @tasks ) {
420 # Remove parents that we aren't creating from the list we give
421 # to Minion
422 0 ▶ $task->{parents} = [ grep { !$to_copy{ $_ } } @{ $task->{parents} // [] } ];
Condition0%0/2
$task->{"parents"} // []
l!l
00
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
Aresult
11
00
423  
424 0 ▶ next unless $to_copy{ $task->{task_id} };
Branch: 1 branch
BranchTrueFalseTotal
unless $to_copy{$task->{"task_id"}} 0 0 0
425 #; $self->log->debug( "Copying $task->{task_id}" );
426 0 $task->{state} = 'copied';
427 0 ▶ $to_copy{ $_ }++ for @{ $task->{parents} // [] };
Condition0%0/2
$task->{"parents"} // []
l!l
00
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
Aresult
11
00
428 }
429 }
430  
431 # Create the new task rows, mapping new task IDs from the old task
432 # IDs for parent/child relationships.
433 0 my %task_id_map;
434 0 for my $task ( @tasks ) {
435 0 ▶ my $parents = $task->{parents} ? delete $task->{parents} : [];
Branch: 1 branch
BranchTrueFalseTotal
$task->{'parents'} ? : 0 0 0
436 0 my $old_task_id = delete $task->{task_id};
437 0 my $new_task_id = $self->yancy->backend->create( zapp_run_tasks => $task );
438 0 $task->{task_id} = $task_id_map{ $old_task_id } = $new_task_id;
439 #$task->{task_id} = $new_task_id;
440 0 $task->{parents} = [ map { $task_id_map{ $_ } } @$parents ];
441 0 for my $parent_task_id ( @{ $task->{parents} } ) {
442 $self->yancy->backend->create( zapp_run_task_parents => {
443 0 $task->%{'task_id'},
444 parent_task_id => $parent_task_id,
445 } );
446 }
447 }
448 0 $new_run->{tasks} = \@tasks;
449  
450 # Enqueue any tasks we are not copying
451 0 my $jobs = $self->enqueue_tasks( $input, grep $_->{state} eq 'inactive', @tasks );
452 0 for my $i ( 0..$#$jobs ) {
453 0 my $job = $jobs->[$i];
454  
455 0 my ( $task ) = grep { $_->{task_id} eq $job->{task_id} } $new_run->{tasks}->@*;
456 0 $task->{$_} = $job->{$_} for keys %$job;
457  
458 #; $self->log->debug( "Setting job id for task $job->{task_id} -> $job->{job_id}" );
459 0 $self->yancy->backend->set( zapp_run_tasks => $job->{task_id}, $job );
460 }
461  
462 0 return $new_run;
463 }
464  
465 #pod =method
466 #pod
467 #pod Create L<Minion> jobs for a run using L<Minion/enqueue>.
468 #pod
469 #pod =cut
470  
471 ▶sub enqueue_tasks( $self, $input, @tasks ) {
Subroutine enqueue_tasks: not called CC=11 SCAR=48.8
Pod undocumented
472 0 my @jobs;
473 # Create Minion jobs for this run
474 my %task_jobs;
475 # Loop over tasks, making the job if the task's parents are made.
476 # Stop the loop once all tasks have jobs.
477 0 my $loops = @tasks * @tasks;
478 0 ▶ while ( @tasks != keys %task_jobs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tasks != keys %task_jobs) 0 0 0
479 # Loop over any tasks that aren't made yet
480 0 for my $task ( grep !$task_jobs{ $_->{task_id} }, @tasks ) {
481 0 my $task_id = $task->{task_id};
482 # Skip if we haven't created all parents
483 #; $self->log->debug( "Task $task_id has parents @{ $task->{parents} // [] }" );
484 0 ▶ next if @{ $task->{parents} // [] } && grep { !$task_jobs{ $_ } } $task->{parents}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$task->{"parents"} // [];} and grep {not $task_jobs{$_};} @{$$task{"parents"}} 0 0 0
Condition0%0/5
@{$task->{"parents"} // [];} and grep {not $task_jobs{$_};} @{$$task{"parents"}}
!ll&&!rl&&r
000
$task->{"parents"} // []
l!l
00
MC/DC0%0/2
@{$task->{"parents"} // [];} and grep {not $task_jobs{$_};} @{$$task{"parents"}}
@{$task->{"parents"} // [];} grep {not $task_jobs{$_};} @{$$task{"parents"}}
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/3
A and B
A= @{$task->{"parents"} // [];}B= grep {not $task_jobs{$_};} @{$$task{"parents"}}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $task->{"parents"}
Aresult
11
00
485  
486 # XXX: Expose more Minion job configuration
487 0 my %job_opts;
488 0 ▶ if ( my @parents = @{ $task->{parents} // [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parents) = @{$task->{"parents"} // [];}) 0 0 0
Condition0%0/2
$task->{"parents"} // []
l!l
00
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
Aresult
11
00
489 $job_opts{ parents } = [
490 0 map $task_jobs{ $_ }, @parents
491 ];
492 }
493  
494 0 my $args = decode_json( $task->{input} );
495 0 ▶ if ( ref $args ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args ne "ARRAY") 0 0 0
496 0 $args = [ $args ];
497 }
498  
499 0 $self->log->debug( sprintf 'Enqueuing task %s', $task->{class} );
500 my $job_id = $self->minion->enqueue(
501 0 $task->{class} => $args,
502 \%job_opts,
503 );
504 0 $task_jobs{ $task_id } = $job_id;
505  
506 0 push @jobs, {
507 task_id => $task_id,
508 job_id => $job_id,
509 };
510 }
511 0 ▶ last if !$loops--;
Branch: 1 branch
BranchTrueFalseTotal
unless $loops-- 0 0 0
512 }
513 0 ▶ if ( @tasks != keys %task_jobs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tasks != keys %task_jobs) 0 0 0
514 0 $self->log->error( 'Could not create jobs: Infinite loop' );
515 0 return undef;
516 }
517  
518 0 return \@jobs;
519 }
520  
521 #pod =method list_tasks
522 #pod
523 #pod List tasks for a run.
524 #pod
525 #pod =cut
526  
527 0 ▶sub list_tasks( $self, $run_id, $opt={} ) {
Subroutine list_tasks: not called CC=5 SCAR=34.0
Pod documented
528 0 my @tasks = $self->yancy->list(
529 zapp_run_tasks => { run_id => $run_id }, $opt,
530 );
531 0 for my $task ( @tasks ) {
532 0 for my $field ( qw( input output ) ) {
533 0 ▶ $task->{ $field } &&= decode_json( $task->{ $field } );
Condition0%0/3
$task->{$field} &&= decode_json($task->{$field})
!ll&&!rl&&r
000
MC/DC0%0/2
$task->{$field} &&= decode_json($task->{$field})
$task->{$field} decode_json($task->{$field})
Truth table0%0/3
A &&= B
A= $task->{$field}B= decode_json($task->{$field})
ABresult
0X0
100
111
534 }
535 }
536 0 return @tasks;
537 }
538  
539 18 1;
540  
541 =pod
542  
543 =head1 NAME
544  
545 Zapp - Plan building, job creating web app
546  
547 =head1 VERSION
548  
549 version 0.004
550  
551 =head1 SYNOPSIS
552  
553 # Start the web application
554 zapp daemon
555  
556 # Start the task runner
557 zapp minion worker
558  
559 =head1 DESCRIPTION
560  
561 Zapp is a graphical workflow builder that provides a UI to build and
562 execute jobs.
563  
564 For documentation on running and using Zapp, see L<Zapp::Guides>.
565  
566 This file documents the main application class, L<Zapp>. This class can
567 be used to L<embed Zapp into an existing Mojolicious application|https://docs.mojolicious.org/Mojolicious/Guides/Routing#Embed-applications>, or
568 can be extended to add customizations.
569  
570 =head1 ATTRIBUTES
571  
572 =head2 formula
573  
574 The formula interpreter. Usually a L<Zapp::Formula> object.
575  
576 =head1 METHODS
577  
578 =head2 startup
579  
580 Initialize the application. Called automatically by L<Mojolicious>.
581  
582 =head2 create_plan
583  
584 Create a new plan and all related data.
585  
586 =head2 get_plan
587  
588 Get a plan and all related data (tasks, inputs).
589  
590 =head2 enqueue_plan
591  
592 Enqueue a plan.
593  
594 =head2 get_tasks
595  
596 Get the tasks for a plan/run from the given table.
597  
598 =head2 enqueue_run
599  
600 Re-enqueue a run.
601  
602 =head2
603  
604 Create L<Minion> jobs for a run using L<Minion/enqueue>.
605  
606 =head2 list_tasks
607  
608 List tasks for a run.
609  
610 =head1 SEE ALSO
611  
612 L<Yancy>, L<Mojolicious>
613  
614 =head1 AUTHOR
615  
616 Doug Bell <preaction@cpan.org>
617  
618 =head1 COPYRIGHT AND LICENSE
619  
620 This software is copyright (c) 2021 by Doug Bell.
621  
622 This is free software; you can redistribute it and/or modify it under
623 the same terms as the Perl 5 programming language system itself.
624  
625 =cut
626  
627 __DATA__
« Zapp/Task/Script.pm ↑ Summary Zapp/Task/Test.pm »