| 1 | package Zapp; | ||||||||||||||||||||||||||||||||||||||
| 2 | 18 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( my $e = load_class( $class ) ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( !defined $obj ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( my $e = load_class( $type ) ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ( !defined $obj ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ( my $e = load_class( $trigger ) ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
delete $plan->{"inputs"} // []
delete $plan->{"inputs"}
Truth table0%0/2
A
A= delete $plan->{"inputs"}
| |||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | my @tasks = @{ delete $plan->{tasks} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $plan->{"tasks"} // []
MC/DC0%0/1
delete $plan->{"tasks"} // []
delete $plan->{"tasks"}
Truth table0%0/2
A
A= delete $plan->{"tasks"}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | my $plan = $self->yancy->get( zapp_plans => $plan_id ) || {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->yancy->get('zapp_plans', $plan_id) || {}
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)
| |||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | if ( my $plan_id = $plan->{plan_id} ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $input->{config} = decode_json( $config ); | |||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||
| 271 | 0 | return $plan; | |||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||
| 274 | #pod =method enqueue_plan | ||||||||||||||||||||||||||||||||||||||
| 275 | #pod | ||||||||||||||||||||||||||||||||||||||
| 276 | #pod Enqueue a plan. | ||||||||||||||||||||||||||||||||||||||
| 277 | #pod | ||||||||||||||||||||||||||||||||||||||
| 278 | #pod =cut | ||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | sub enqueue_plan( $self, $plan_id, $input={}, %opt ) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
enqueue_plan: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | $opt{queue} ||= 'zapp'; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"queue"} ||= "zapp"
MC/DC0%0/1
$opt{"queue"} ||= "zapp"
$opt{"queue"}
Truth table0%0/2
A
A= $opt{"queue"}
| |||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | # Create the run in the database by copying the plan | ||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my $plan = $self->yancy->get( zapp_plans => $plan_id ); | |||||||||||||||||||||||||||||||||||||
| 285 | # XXX: Run inputs and plan inputs should either both be tables or | ||||||||||||||||||||||||||||||||||||||
| 286 | # both be JSON serialized | ||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my @inputs = $self->yancy->list( zapp_plan_inputs => { plan_id => $plan_id }, { order_by => 'rank' } ); | |||||||||||||||||||||||||||||||||||||
| 288 | 0 | delete $plan->{created}; | |||||||||||||||||||||||||||||||||||||
| 289 | my $run = { | ||||||||||||||||||||||||||||||||||||||
| 290 | %$plan, | ||||||||||||||||||||||||||||||||||||||
| 291 | # XXX: Auto-encode/-decode JSON fields in Yancy schema | ||||||||||||||||||||||||||||||||||||||
| 292 | input => encode_json([ | ||||||||||||||||||||||||||||||||||||||
| 293 | map +{ | ||||||||||||||||||||||||||||||||||||||
| 294 | $_->%{qw( name label type description )}, | ||||||||||||||||||||||||||||||||||||||
| 295 | config => decode_json( $_->{config} // 'null' ), | ||||||||||||||||||||||||||||||||||||||
| 296 | value => $input->{ $_->{name} }, | ||||||||||||||||||||||||||||||||||||||
| 297 | }, | ||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | @inputs, | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_->{"config"} // "null"
MC/DC0%0/1
$_->{"config"} // "null"
$_->{"config"}
Truth table0%0/2
A
A= $_->{"config"}
| |||||||||||||||||||||||||||||||||||||||
| 299 | ]), | ||||||||||||||||||||||||||||||||||||||
| 300 | }; | ||||||||||||||||||||||||||||||||||||||
| 301 | 0 | my $run_id = $run->{run_id} = $self->yancy->create( zapp_runs => $run ); | |||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||
| 303 | 0 | my @tasks = $self->get_tasks( zapp_plan_tasks => { plan_id => $plan_id } ); | |||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||
| 305 | # Create the new task rows, mapping new task IDs from the old task | ||||||||||||||||||||||||||||||||||||||
| 306 | # IDs for parent/child relationships. | ||||||||||||||||||||||||||||||||||||||
| 307 | 0 | my %task_id_map; | |||||||||||||||||||||||||||||||||||||
| 308 | 0 | for my $task ( @tasks ) { | |||||||||||||||||||||||||||||||||||||
| 309 | 0 | delete $task->{ $_ } for qw( plan_id ); | |||||||||||||||||||||||||||||||||||||
| 310 | 0 | $task->{run_id} = $run_id; | |||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | my $parents = $task->{parents} ? delete $task->{parents} : []; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 312 | 0 | my $old_task_id = $task->{plan_task_id} = delete $task->{task_id}; | |||||||||||||||||||||||||||||||||||||
| 313 | 0 | my $new_task_id = $self->yancy->backend->create( zapp_run_tasks => $task ); | |||||||||||||||||||||||||||||||||||||
| 314 | 0 | $task->{task_id} = $task_id_map{ $old_task_id } = $new_task_id; | |||||||||||||||||||||||||||||||||||||
| 315 | #$task->{task_id} = $new_task_id; | ||||||||||||||||||||||||||||||||||||||
| 316 | 0 | $task->{parents} = [ map { $task_id_map{ $_ } } @$parents ]; | |||||||||||||||||||||||||||||||||||||
| 317 | 0 | for my $parent_task_id ( @{ $task->{parents} } ) { | |||||||||||||||||||||||||||||||||||||
| 318 | $self->yancy->backend->create( zapp_run_task_parents => { | ||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $task->%{'task_id'}, | |||||||||||||||||||||||||||||||||||||
| 320 | parent_task_id => $parent_task_id, | ||||||||||||||||||||||||||||||||||||||
| 321 | } ); | ||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||
| 324 | 0 | $run->{tasks} = \@tasks; | |||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $jobs = $self->enqueue_tasks( $input, @tasks ); | |||||||||||||||||||||||||||||||||||||
| 327 | 0 | for my $i ( 0..$#$jobs ) { | |||||||||||||||||||||||||||||||||||||
| 328 | 0 | my $job = $jobs->[$i]; | |||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||
| 330 | 0 | my ( $task ) = grep { $_->{task_id} eq $job->{task_id} } $run->{tasks}->@*; | |||||||||||||||||||||||||||||||||||||
| 331 | 0 | $task->{$_} = $job->{$_} for keys %$job; | |||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||
| 333 | 0 | $self->yancy->backend->set( zapp_run_tasks => $job->{task_id}, $job ); | |||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||
| 336 | 0 | return $run; | |||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||
| 339 | #pod =method get_tasks | ||||||||||||||||||||||||||||||||||||||
| 340 | #pod | ||||||||||||||||||||||||||||||||||||||
| 341 | #pod Get the tasks for a plan/run from the given table. | ||||||||||||||||||||||||||||||||||||||
| 342 | #pod | ||||||||||||||||||||||||||||||||||||||
| 343 | #pod =cut | ||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||
| 345 | ▶ | sub get_tasks( $self, $table, $search ) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
get_tasks: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my $parents_table = $table =~ s/s$/_parents/r; | |||||||||||||||||||||||||||||||||||||
| 347 | 0 | my @tasks = $self->yancy->list( $table => $search ); | |||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||
| 349 | 0 | for my $task ( @tasks ) { | |||||||||||||||||||||||||||||||||||||
| 350 | $task->{parents} = [ | ||||||||||||||||||||||||||||||||||||||
| 351 | 0 | map { $_->{parent_task_id} } | |||||||||||||||||||||||||||||||||||||
| 352 | 0 | $self->yancy->list( $parents_table => { $task->%{'task_id'} } ) | |||||||||||||||||||||||||||||||||||||
| 353 | ]; | ||||||||||||||||||||||||||||||||||||||
| 354 | #; $self->log->debug( 'Got parents for task ' . $task->{task_id} . ': ' . join ', ', @{ $task->{parents} } ); | ||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | # Put the tasks in an order they can be created so all parent tasks | ||||||||||||||||||||||||||||||||||||||
| 358 | # are before any dependent child tasks | ||||||||||||||||||||||||||||||||||||||
| 359 | 0 | my @ordered_tasks; | |||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | TASK: while ( @tasks ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my $task = shift @tasks; | |||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | for my $parent_task_id ( @{ $task->{parents} // [] } ) { | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$task->{"parents"} // []
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
| |||||||||||||||||||||||||||||||||||||||
| 363 | # If there's a parent task we haven't seen yet, try again later | ||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | if ( grep { $_->{task_id} eq $parent_task_id } @tasks ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 365 | 0 | push @tasks, $task; | |||||||||||||||||||||||||||||||||||||
| 366 | 0 | next TASK; | |||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||
| 369 | 0 | push @ordered_tasks, $task; | |||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||
| 372 | 0 | return @ordered_tasks; | |||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||
| 375 | #pod =method enqueue_run | ||||||||||||||||||||||||||||||||||||||
| 376 | #pod | ||||||||||||||||||||||||||||||||||||||
| 377 | #pod Re-enqueue a run. | ||||||||||||||||||||||||||||||||||||||
| 378 | #pod | ||||||||||||||||||||||||||||||||||||||
| 379 | #pod =cut | ||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | sub enqueue_run( $self, $old_run_id, $input=[], %opt ) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
enqueue_run: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | $opt{queue} ||= 'zapp'; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"queue"} ||= "zapp"
MC/DC0%0/1
$opt{"queue"} ||= "zapp"
$opt{"queue"}
Truth table0%0/2
A
A= $opt{"queue"}
| |||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||
| 384 | # Create the new run in the database by copying the old run | ||||||||||||||||||||||||||||||||||||||
| 385 | 0 | my $old_run = $self->yancy->get( zapp_runs => $old_run_id ); | |||||||||||||||||||||||||||||||||||||
| 386 | # XXX: Delete more from the old run | ||||||||||||||||||||||||||||||||||||||
| 387 | 0 | delete $old_run->{ $_ } for qw( run_id created started finished state ); | |||||||||||||||||||||||||||||||||||||
| 388 | 0 | my $new_run = { | |||||||||||||||||||||||||||||||||||||
| 389 | %$old_run, | ||||||||||||||||||||||||||||||||||||||
| 390 | state => 'inactive', | ||||||||||||||||||||||||||||||||||||||
| 391 | # XXX: Auto-encode/-decode JSON fields in Yancy schema | ||||||||||||||||||||||||||||||||||||||
| 392 | input => encode_json( $input ), | ||||||||||||||||||||||||||||||||||||||
| 393 | }; | ||||||||||||||||||||||||||||||||||||||
| 394 | 0 | my $new_run_id = $new_run->{run_id} = $self->yancy->create( zapp_runs => $new_run ); | |||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my @tasks = $self->get_tasks( zapp_run_tasks => { run_id => $old_run_id } ); | |||||||||||||||||||||||||||||||||||||
| 397 | 0 | for my $task ( @tasks ) { | |||||||||||||||||||||||||||||||||||||
| 398 | 0 | delete $task->{ $_ } for qw( job_id started finished ); | |||||||||||||||||||||||||||||||||||||
| 399 | 0 | $task->{ run_id } = $new_run_id; | |||||||||||||||||||||||||||||||||||||
| 400 | 0 | $task->{ state } = 'inactive'; | |||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | if ( my $start_task_id = $opt{task_id} ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 404 | #; $self->log->debug( "Starting from task: $start_task_id" ); | ||||||||||||||||||||||||||||||||||||||
| 405 | # Mark which jobs should be re-run and which should be copied. | ||||||||||||||||||||||||||||||||||||||
| 406 | # Since we know @tasks is ordered with parents before children, | ||||||||||||||||||||||||||||||||||||||
| 407 | # we can reverse it to make sure we hit children before their | ||||||||||||||||||||||||||||||||||||||
| 408 | # parents. | ||||||||||||||||||||||||||||||||||||||
| 409 | # Start with the parents of the starting task | ||||||||||||||||||||||||||||||||||||||
| 410 | my %to_copy = ( | ||||||||||||||||||||||||||||||||||||||
| 411 | 0 | map { $_ => 1 } map { $_->{parents}->@* } | |||||||||||||||||||||||||||||||||||||
| 412 | 0 | grep { $_->{task_id} eq $start_task_id } | |||||||||||||||||||||||||||||||||||||
| 413 | @tasks | ||||||||||||||||||||||||||||||||||||||
| 414 | ); | ||||||||||||||||||||||||||||||||||||||
| 415 | #; $self->log->debug( "Copying " . %to_copy ); | ||||||||||||||||||||||||||||||||||||||
| 416 | 0 | for my $task ( reverse @tasks ) { | |||||||||||||||||||||||||||||||||||||
| 417 | # Remove parents that we aren't creating from the list we give | ||||||||||||||||||||||||||||||||||||||
| 418 | # to Minion | ||||||||||||||||||||||||||||||||||||||
| 419 | 0 | ▶ | $task->{parents} = [ grep { !$to_copy{ $_ } } @{ $task->{parents} // [] } ]; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$task->{"parents"} // []
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
| |||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||
| 421 | 0 | ▶ | next unless $to_copy{ $task->{task_id} }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 422 | #; $self->log->debug( "Copying $task->{task_id}" ); | ||||||||||||||||||||||||||||||||||||||
| 423 | 0 | $task->{state} = 'copied'; | |||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | $to_copy{ $_ }++ for @{ $task->{parents} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$task->{"parents"} // []
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
| |||||||||||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||
| 428 | # Create the new task rows, mapping new task IDs from the old task | ||||||||||||||||||||||||||||||||||||||
| 429 | # IDs for parent/child relationships. | ||||||||||||||||||||||||||||||||||||||
| 430 | 0 | my %task_id_map; | |||||||||||||||||||||||||||||||||||||
| 431 | 0 | for my $task ( @tasks ) { | |||||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | my $parents = $task->{parents} ? delete $task->{parents} : []; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 433 | 0 | my $old_task_id = delete $task->{task_id}; | |||||||||||||||||||||||||||||||||||||
| 434 | 0 | my $new_task_id = $self->yancy->backend->create( zapp_run_tasks => $task ); | |||||||||||||||||||||||||||||||||||||
| 435 | 0 | $task->{task_id} = $task_id_map{ $old_task_id } = $new_task_id; | |||||||||||||||||||||||||||||||||||||
| 436 | #$task->{task_id} = $new_task_id; | ||||||||||||||||||||||||||||||||||||||
| 437 | 0 | $task->{parents} = [ map { $task_id_map{ $_ } } @$parents ]; | |||||||||||||||||||||||||||||||||||||
| 438 | 0 | for my $parent_task_id ( @{ $task->{parents} } ) { | |||||||||||||||||||||||||||||||||||||
| 439 | $self->yancy->backend->create( zapp_run_task_parents => { | ||||||||||||||||||||||||||||||||||||||
| 440 | 0 | $task->%{'task_id'}, | |||||||||||||||||||||||||||||||||||||
| 441 | parent_task_id => $parent_task_id, | ||||||||||||||||||||||||||||||||||||||
| 442 | } ); | ||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||
| 445 | 0 | $new_run->{tasks} = \@tasks; | |||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||
| 447 | # Enqueue any tasks we are not copying | ||||||||||||||||||||||||||||||||||||||
| 448 | 0 | my $jobs = $self->enqueue_tasks( $input, grep $_->{state} eq 'inactive', @tasks ); | |||||||||||||||||||||||||||||||||||||
| 449 | 0 | for my $i ( 0..$#$jobs ) { | |||||||||||||||||||||||||||||||||||||
| 450 | 0 | my $job = $jobs->[$i]; | |||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||
| 452 | 0 | my ( $task ) = grep { $_->{task_id} eq $job->{task_id} } $new_run->{tasks}->@*; | |||||||||||||||||||||||||||||||||||||
| 453 | 0 | $task->{$_} = $job->{$_} for keys %$job; | |||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||
| 455 | #; $self->log->debug( "Setting job id for task $job->{task_id} -> $job->{job_id}" ); | ||||||||||||||||||||||||||||||||||||||
| 456 | 0 | $self->yancy->backend->set( zapp_run_tasks => $job->{task_id}, $job ); | |||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||
| 459 | 0 | return $new_run; | |||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||
| 462 | #pod =method | ||||||||||||||||||||||||||||||||||||||
| 463 | #pod | ||||||||||||||||||||||||||||||||||||||
| 464 | #pod Create L<Minion> jobs for a run using L<Minion/enqueue>. | ||||||||||||||||||||||||||||||||||||||
| 465 | #pod | ||||||||||||||||||||||||||||||||||||||
| 466 | #pod =cut | ||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||
| 468 | ▶ | sub enqueue_tasks( $self, $input, @tasks ) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
enqueue_tasks: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 469 | 0 | my @jobs; | |||||||||||||||||||||||||||||||||||||
| 470 | # Create Minion jobs for this run | ||||||||||||||||||||||||||||||||||||||
| 471 | my %task_jobs; | ||||||||||||||||||||||||||||||||||||||
| 472 | # Loop over tasks, making the job if the task's parents are made. | ||||||||||||||||||||||||||||||||||||||
| 473 | # Stop the loop once all tasks have jobs. | ||||||||||||||||||||||||||||||||||||||
| 474 | 0 | my $loops = @tasks * @tasks; | |||||||||||||||||||||||||||||||||||||
| 475 | 0 | ▶ | while ( @tasks != keys %task_jobs ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 476 | # Loop over any tasks that aren't made yet | ||||||||||||||||||||||||||||||||||||||
| 477 | 0 | for my $task ( grep !$task_jobs{ $_->{task_id} }, @tasks ) { | |||||||||||||||||||||||||||||||||||||
| 478 | 0 | my $task_id = $task->{task_id}; | |||||||||||||||||||||||||||||||||||||
| 479 | # Skip if we haven't created all parents | ||||||||||||||||||||||||||||||||||||||
| 480 | #; $self->log->debug( "Task $task_id has parents @{ $task->{parents} // [] }" ); | ||||||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | next if @{ $task->{parents} // [] } && grep { !$task_jobs{ $_ } } $task->{parents}->@*; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
@{$task->{"parents"} // [];} and grep {not $task_jobs{$_};} @{$$task{"parents"}}
$task->{"parents"} // []
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"}}
Truth table0%0/2
A
A= $task->{"parents"}
| |||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||
| 483 | # XXX: Expose more Minion job configuration | ||||||||||||||||||||||||||||||||||||||
| 484 | 0 | my %job_opts; | |||||||||||||||||||||||||||||||||||||
| 485 | 0 | ▶ | if ( my @parents = @{ $task->{parents} // [] } ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$task->{"parents"} // []
MC/DC0%0/1
$task->{"parents"} // []
$task->{"parents"}
Truth table0%0/2
A
A= $task->{"parents"}
| |||||||||||||||||||||||||||||||||||||||
| 486 | $job_opts{ parents } = [ | ||||||||||||||||||||||||||||||||||||||
| 487 | 0 | map $task_jobs{ $_ }, @parents | |||||||||||||||||||||||||||||||||||||
| 488 | ]; | ||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||
| 491 | 0 | my $args = decode_json( $task->{input} ); | |||||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | if ( ref $args ne 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 493 | 0 | $args = [ $args ]; | |||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||
| 496 | 0 | $self->log->debug( sprintf 'Enqueuing task %s', $task->{class} ); | |||||||||||||||||||||||||||||||||||||
| 497 | my $job_id = $self->minion->enqueue( | ||||||||||||||||||||||||||||||||||||||
| 498 | 0 | $task->{class} => $args, | |||||||||||||||||||||||||||||||||||||
| 499 | \%job_opts, | ||||||||||||||||||||||||||||||||||||||
| 500 | ); | ||||||||||||||||||||||||||||||||||||||
| 501 | 0 | $task_jobs{ $task_id } = $job_id; | |||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||
| 503 | 0 | push @jobs, { | |||||||||||||||||||||||||||||||||||||
| 504 | task_id => $task_id, | ||||||||||||||||||||||||||||||||||||||
| 505 | job_id => $job_id, | ||||||||||||||||||||||||||||||||||||||
| 506 | }; | ||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||
| 508 | 0 | ▶ | last if !$loops--; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||
| 510 | 0 | ▶ | if ( @tasks != keys %task_jobs ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 511 | 0 | $self->log->error( 'Could not create jobs: Infinite loop' ); | |||||||||||||||||||||||||||||||||||||
| 512 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||
| 515 | 0 | return \@jobs; | |||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||
| 518 | #pod =method list_tasks | ||||||||||||||||||||||||||||||||||||||
| 519 | #pod | ||||||||||||||||||||||||||||||||||||||
| 520 | #pod List tasks for a run. | ||||||||||||||||||||||||||||||||||||||
| 521 | #pod | ||||||||||||||||||||||||||||||||||||||
| 522 | #pod =cut | ||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||
| 524 | 0 | ▶ | sub list_tasks( $self, $run_id, $opt={} ) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
list_tasks: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 525 | 0 | my @tasks = $self->yancy->list( | |||||||||||||||||||||||||||||||||||||
| 526 | zapp_run_tasks => { run_id => $run_id }, $opt, | ||||||||||||||||||||||||||||||||||||||
| 527 | ); | ||||||||||||||||||||||||||||||||||||||
| 528 | 0 | for my $task ( @tasks ) { | |||||||||||||||||||||||||||||||||||||
| 529 | 0 | for my $field ( qw( input output ) ) { | |||||||||||||||||||||||||||||||||||||
| 530 | 0 | ▶ | $task->{ $field } &&= decode_json( $task->{ $field } ); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$task->{$field} &&= decode_json($task->{$field})
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})
| |||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||
| 533 | 0 | return @tasks; | |||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||
| 536 | 18 | 1; | |||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||
| 538 | =pod | ||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||
| 540 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||
| 542 | Zapp - Plan building, job creating web app | ||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||
| 544 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||
| 546 | version 0.001 | ||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||
| 548 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||
| 550 | # Start the web application | ||||||||||||||||||||||||||||||||||||||
| 551 | zapp daemon | ||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||
| 553 | # Start the task runner | ||||||||||||||||||||||||||||||||||||||
| 554 | zapp minion worker | ||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||
| 556 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||
| 558 | Zapp is a graphical workflow builder that provides a UI to build and | ||||||||||||||||||||||||||||||||||||||
| 559 | execute jobs. | ||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||
| 561 | For documentation on running and using Zapp, see L<Zapp::Guides>. | ||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||
| 563 | This file documents the main application class, L<Zapp>. This class can | ||||||||||||||||||||||||||||||||||||||
| 564 | be used to L<embed Zapp into an existing Mojolicious application|https://docs.mojolicious.org/Mojolicious/Guides/Routing#Embed-applications>, or | ||||||||||||||||||||||||||||||||||||||
| 565 | can be extended to add customizations. | ||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||
| 567 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||
| 569 | =head2 formula | ||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||
| 571 | The formula interpreter. Usually a L<Zapp::Formula> object. | ||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||
| 573 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||
| 575 | =head2 startup | ||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||
| 577 | Initialize the application. Called automatically by L<Mojolicious>. | ||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||
| 579 | =head2 create_plan | ||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||
| 581 | Create a new plan and all related data. | ||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||
| 583 | =head2 get_plan | ||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||
| 585 | Get a plan and all related data (tasks, inputs). | ||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||
| 587 | =head2 enqueue_plan | ||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||
| 589 | Enqueue a plan. | ||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||
| 591 | =head2 get_tasks | ||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||
| 593 | Get the tasks for a plan/run from the given table. | ||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||
| 595 | =head2 enqueue_run | ||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||
| 597 | Re-enqueue a run. | ||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||
| 599 | =head2 | ||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||
| 601 | Create L<Minion> jobs for a run using L<Minion/enqueue>. | ||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||
| 603 | =head2 list_tasks | ||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||
| 605 | List tasks for a run. | ||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||
| 607 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||
| 609 | L<Yancy>, L<Mojolicious> | ||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||
| 611 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||
| 613 | Doug Bell <preaction@cpan.org> | ||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||
| 615 | =head1 CONTRIBUTOR | ||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||
| 617 | =for stopwords Doug Bell | ||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||
| 619 | Doug Bell <doug.bell@grantstreet.com> | ||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||
| 621 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||
| 623 | This software is copyright (c) 2020 by Doug Bell. | ||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||
| 625 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 626 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||
| 628 | =cut | ||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||
| 630 | __DATA__ | ||||||||||||||||||||||||||||||||||||||