| 1 | package Workflow; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use v5.14.0; # warnings | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 28 | ▶ | use parent qw( Workflow::Base ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Workflow::Context; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use Workflow::Exception qw( workflow_error ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 28 | ▶ | use Exception::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use Workflow::Factory qw( FACTORY ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 28 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 28 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 28 | my @FIELDS = qw( id type description state last_update time_zone | |||||||||||||||||||||||||||
| 15 | history_class last_action_executed ); | ||||||||||||||||||||||||||||
| 16 | 28 | my @INTERNAL = qw( _factory _observers ); | |||||||||||||||||||||||||||
| 17 | 28 | __PACKAGE__->mk_accessors( @FIELDS, @INTERNAL ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 28 | $Workflow::VERSION = '2.12'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 28 | ▶ | use constant NO_CHANGE_VALUE => 'NOCHANGE'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ######################################## | ||||||||||||||||||||||||||||
| 24 | # INTERNAL METHODS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 55 | ▶ | sub add_observer { | ||||||||||||||||||||||||||
|
Subroutine
add_observer: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 55 | my ($self, @observers) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 55 | ▶ | if (not $self->_observers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 36 | $self->_observers( [] ); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 55 | push @{$self->_observers}, @observers; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 55 | return; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 281 | ▶ | sub notify_observers { | ||||||||||||||||||||||||||
|
Subroutine
notify_observers: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 281 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 281 | ▶ | return unless $self->_observers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 253 | $_->($self, @args) for @{$self->_observers}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 253 | return; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 26 | ▶ | sub get_initial_history_data { | ||||||||||||||||||||||||||
|
Subroutine
get_initial_history_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 26 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 48 | return ( | ||||||||||||||||||||||||||||
| 49 | 26 | description => 'Create new workflow', | |||||||||||||||||||||||||||
| 50 | user => 'n/a', | ||||||||||||||||||||||||||||
| 51 | action => 'Create workflow', | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ######################################## | ||||||||||||||||||||||||||||
| 57 | # PUBLIC METHODS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # this is our only read-write property... | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 231 | ▶ | sub context { | ||||||||||||||||||||||||||
|
Subroutine
context: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 231 | my ( $self, $context ) = @_; | |||||||||||||||||||||||||||
| 63 | 231 | ▶ | if ($context) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # We already have a context, merge the new one with ours; (the | ||||||||||||||||||||||||||||
| 66 | # new one wins with dupes) | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 36 | ▶ | if ( $self->{context} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $self->{context}->merge($context); | |||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | 36 | $context->param( workflow_id => $self->id ); | |||||||||||||||||||||||||||
| 72 | 36 | $self->{context} = $context; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 231 | ▶ | unless ( $self->{context} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $self->{context} = Workflow::Context->new(); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 231 | return $self->{context}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 12 | ▶ | sub get_current_actions { | ||||||||||||||||||||||||||
|
Subroutine
get_current_actions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 12 | my ( $self, $group ) = @_; | |||||||||||||||||||||||||||
| 83 | 12 | $self->log->debug( "Getting current actions for wf '" . $self->id . "'" ); | |||||||||||||||||||||||||||
| 84 | 12 | my $wf_state = $self->_get_workflow_state; | |||||||||||||||||||||||||||
| 85 | 12 | return $wf_state->get_available_action_names( $self, $group ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub get_all_actions { | ||||||||||||||||||||||||||
|
Subroutine
get_all_actions: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 90 | 0 | $self->log->debug( "Getting all actions for wf '" . $self->id . "'" ); | |||||||||||||||||||||||||||
| 91 | 0 | my $wf_state = $self->_get_workflow_state; | |||||||||||||||||||||||||||
| 92 | 0 | return $wf_state->get_all_action_names( $self ); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 43 | ▶ | sub get_action { | ||||||||||||||||||||||||||
|
Subroutine
get_action: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 43 | my ( $self, $action_name ) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 43 | my $state = $self->state; | |||||||||||||||||||||||||||
| 99 | 43 | $self->log->debug( | |||||||||||||||||||||||||||
| 100 | "Trying to find action '$action_name' in state '$state'"); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 43 | my $wf_state = $self->_get_workflow_state; | |||||||||||||||||||||||||||
| 103 | 43 | ▶ | unless ( $wf_state->contains_action($action_name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | workflow_error | |||||||||||||||||||||||||||
| 105 | "State '$state' does not contain action '$action_name'"; | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 43 | $self->log->debug("Action '$action_name' exists in state '$state'"); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 43 | my $action = $self->_get_workflow_state()->get_action( $self, $action_name ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # This will throw an exception which we want to bubble up | ||||||||||||||||||||||||||||
| 112 | 43 | $wf_state->evaluate_action( $self, $action_name ); | |||||||||||||||||||||||||||
| 113 | 43 | return $action; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub get_action_fields { | ||||||||||||||||||||||||||
|
Subroutine
get_action_fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 1 | my ( $self, $action_name ) = @_; | |||||||||||||||||||||||||||
| 118 | 1 | my $action = $self->get_action($action_name); | |||||||||||||||||||||||||||
| 119 | 1 | return $action->fields; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 5 | ▶ | sub _get_autorun_action_name { | ||||||||||||||||||||||||||
|
Subroutine
_get_autorun_action_name: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||
| 123 | 5 | my ( $self, $wf_state ) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 5 | my $action_name = ''; | |||||||||||||||||||||||||||
| 126 | 5 | ▶ | $self->log->is_info | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | && $self->log->info( | ||||||||||||||||||||||||||||
| 128 | "State '", $wf_state->state, "' marked to be run ", | ||||||||||||||||||||||||||||
| 129 | "automatically; executing that state/action..." | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 5 | ▶ | if ( $wf_state->may_stop() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | try { | ||||||||||||||||||||||||||||
| 134 | 0 | $action_name = $wf_state->get_autorun_action_name($self); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | catch { } | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||||||||||
| 139 | 5 | $action_name = $wf_state->get_autorun_action_name($self); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 5 | ▶ | if ( $action_name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 5 | ▶ | $self->log->is_debug | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | && $self->log->debug( | ||||||||||||||||||||||||||||
| 145 | "Found action '$action_name' to execute in autorun state ", | ||||||||||||||||||||||||||||
| 146 | $wf_state->state | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 5 | return $action_name; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 59 | ▶ | sub _maybe_autorun_state { | ||||||||||||||||||||||||||
|
Subroutine
_maybe_autorun_state: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 154 | 59 | my ( $self, $wf_state, $running ) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 59 | my $run_notified = $running; | |||||||||||||||||||||||||||
| 157 | 59 | ▶ | while ( $wf_state->autorun | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$wf_state->autorun and my $action_name = $self->_get_autorun_action_name($wf_state)
MC/DC50%1/2
$wf_state->autorun and my $action_name = $self->_get_autorun_action_name($wf_state)
$wf_state->autorun
my $action_name = $self->_get_autorun_action_name($wf_state)
Truth table66%2/3
A and B
A= $wf_state->autorunB= my $action_name = $self->_get_autorun_action_name($wf_state)
| |||||||||||||||||||||||||||||
| 158 | and my $action_name = $self->_get_autorun_action_name( $wf_state ) ) { | ||||||||||||||||||||||||||||
| 159 | 5 | ▶ | $self->notify_observers( 'startup' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | if not $running; | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | $wf_state = $self->_execute_single_action( $action_name, !!1 ); | |||||||||||||||||||||||||||
| 163 | 5 | $run_notified = !!1; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 59 | ▶ | $self->notify_observers( 'finalize' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $running and $run_notified
MC/DC100%2/2
not $running and $run_notified
not $running
$run_notified
Truth table100%3/3
not(A) and B
A= not $runningB= $run_notified
| |||||||||||||||||||||||||||||
| 167 | if not $running and $run_notified; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 59 | return; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 31 | ▶ | sub execute_action { | ||||||||||||||||||||||||||
|
Subroutine
execute_action: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 31 | my ( $self, $action_name, $action_args ) = @_; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 31 | $self->notify_observers( 'startup' ); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 31 | my $wf_state = $self->_execute_single_action( $action_name, !!0, $action_args ); | |||||||||||||||||||||||||||
| 178 | 31 | $self->_maybe_autorun_state( $wf_state, !!1 ); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 31 | $self->notify_observers( 'finalize' ); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 31 | return $self->state; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 38 | ▶ | sub add_history { | ||||||||||||||||||||||||||
|
Subroutine
add_history: called CC=5 SCAR=16.9
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 38 | my ( $self, @items ) = @_; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 38 | my @to_add = (); | |||||||||||||||||||||||||||
| 189 | 38 | my $history = $self->history_class; | |||||||||||||||||||||||||||
| 190 | 38 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 191 | 38 | ▶ | if ( ref $item eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 192 | 37 | $item->{workflow_id} = $self->id; | |||||||||||||||||||||||||||
| 193 | 37 | $item->{time_zone} = $self->time_zone(); | |||||||||||||||||||||||||||
| 194 | 37 | push @to_add, $history->new($item); | |||||||||||||||||||||||||||
| 195 | 37 | $self->log->debug("Adding history from hashref"); | |||||||||||||||||||||||||||
| 196 | ▶ | } elsif ( ref $item and $item->isa($history) ) { | |||||||||||||||||||||||||||
|
Condition33%1/3
ref $item and $item->isa($history)
MC/DC0%0/2
ref $item and $item->isa($history)
ref $item
$item->isa($history)
Truth table33%1/3
A and B
A= ref $itemB= $item->isa($history)
| |||||||||||||||||||||||||||||
| 197 | 0 | $item->workflow_id( $self->id ); | |||||||||||||||||||||||||||
| 198 | 0 | push @to_add, $item; | |||||||||||||||||||||||||||
| 199 | 0 | $self->log->debug("Adding history object directly"); | |||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||
| 201 | 1 | workflow_error "I don't know how to add a history of type '", | |||||||||||||||||||||||||||
| 202 | ref($item), "'"; | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 37 | push @{ $self->{_histories} }, @to_add; | |||||||||||||||||||||||||||
| 207 | 37 | $self->notify_observers( 'add history', \@to_add ); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 6 | ▶ | sub get_history { | ||||||||||||||||||||||||||
|
Subroutine
get_history: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||
| 211 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 212 | 6 | ▶ | $self->{_histories} ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_histories"} ||= []
MC/DC0%0/1
$self->{"_histories"} ||= []
$self->{"_histories"}
Truth table50%1/2
A
A= $self->{"_histories"}
| |||||||||||||||||||||||||||||
| 213 | 6 | my @uniq_history = (); | |||||||||||||||||||||||||||
| 214 | 6 | my %seen_ids = (); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 6 | my $history_class = $self->history_class; | |||||||||||||||||||||||||||
| 217 | 6 | foreach my $history ( | |||||||||||||||||||||||||||
| 218 | $self->_factory()->get_workflow_history($self) | ||||||||||||||||||||||||||||
| 219 | ) { | ||||||||||||||||||||||||||||
| 220 | 5 | my $id = $history->{id}; | |||||||||||||||||||||||||||
| 221 | 5 | ▶ | if (defined $id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 5 | ▶ | unless ( $seen_ids{$id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 5 | $seen_ids{$id}++; | |||||||||||||||||||||||||||
| 224 | 5 | my $hist = $history_class->new($history); | |||||||||||||||||||||||||||
| 225 | 5 | $hist->set_saved; | |||||||||||||||||||||||||||
| 226 | 5 | push @uniq_history, $hist; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } else { | ||||||||||||||||||||||||||||
| 229 | 0 | die "Persister returned history item without 'id' key"; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 6 | foreach my $history (@{ $self->{_histories} }) { | |||||||||||||||||||||||||||
| 233 | 6 | my $id = $history->id; | |||||||||||||||||||||||||||
| 234 | 6 | ▶ | if (defined $id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 6 | ▶ | unless ( $seen_ids{$id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 3 | $seen_ids{$id}++; | |||||||||||||||||||||||||||
| 237 | 3 | push @uniq_history, $history; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } else { | ||||||||||||||||||||||||||||
| 240 | 0 | push @uniq_history, $history; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | 6 | return @uniq_history; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 64 | ▶ | sub get_unsaved_history { | ||||||||||||||||||||||||||
|
Subroutine
get_unsaved_history: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 247 | 64 | my ($self) = @_; | |||||||||||||||||||||||||||
| 248 | 64 | return grep { !$_->is_saved } @{ $self->{_histories} }; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub clear_history { | ||||||||||||||||||||||||||
|
Subroutine
clear_history: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 252 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 253 | 0 | $self->{_histories} = []; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | ######################################## | ||||||||||||||||||||||||||||
| 257 | # PRIVATE METHODS | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 36 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 260 | 36 | my ( $self, $id, $current_state, $config, $wf_state_objects, $factory ) | |||||||||||||||||||||||||||
| 261 | = @_; | ||||||||||||||||||||||||||||
| 262 | 36 | ▶ | $id ||= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$id ||= ""
MC/DC100%1/1
$id ||= ""
$id
Truth table100%2/2
A
A= $id
| |||||||||||||||||||||||||||||
| 263 | 36 | ▶ | $factory ||= FACTORY; | ||||||||||||||||||||||||||
|
Condition33%1/3
$factory ||= FACTORY()
MC/DC0%0/2
$factory ||= FACTORY()
$factory
FACTORY()
Truth table33%1/3
A ||= B
A= $factoryB= FACTORY()
| |||||||||||||||||||||||||||||
| 264 | 36 | $self->log->info( | |||||||||||||||||||||||||||
| 265 | "Instantiating workflow of with ID '$id' and type ", | ||||||||||||||||||||||||||||
| 266 | "'$config->{type}' with current state '$current_state'" | ||||||||||||||||||||||||||||
| 267 | ); | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 36 | ▶ | $self->id($id) if ($id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 36 | $self->_factory($factory); | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 36 | $self->state($current_state); | |||||||||||||||||||||||||||
| 273 | 36 | $self->type( $config->{type} ); | |||||||||||||||||||||||||||
| 274 | 36 | $self->description( $config->{description} ); | |||||||||||||||||||||||||||
| 275 | my $time_zone | ||||||||||||||||||||||||||||
| 276 | 36 | ▶ | = exists $config->{time_zone} ? $config->{time_zone} : 'floating'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 36 | $self->time_zone($time_zone); | |||||||||||||||||||||||||||
| 278 | 36 | $self->history_class( $config->{history_class} ); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # other properties go into 'param'... | ||||||||||||||||||||||||||||
| 281 | 36 | ▶ | while ( my ( $key, $value ) = each %{$config} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 187 | ▶ | next if ( $key =~ /^(type|description|history_class)$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 82 | ▶ | next if ( ref $value ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 41 | $self->log->debug("Assigning parameter '$key' -> '$value'"); | |||||||||||||||||||||||||||
| 285 | 41 | $self->param( $key, $value ); | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # Now set all the Workflow::State objects created and cached by the | ||||||||||||||||||||||||||||
| 289 | # factory | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 36 | foreach my $wf_state ( @{$wf_state_objects} ) { | |||||||||||||||||||||||||||
| 292 | 132 | $self->_set_workflow_state($wf_state); | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Override from Class::Accessor so only certain callers can set | ||||||||||||||||||||||||||||
| 297 | # properties | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 460 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | 460 | my ( $self, $prop, $value ) = @_; | |||||||||||||||||||||||||||
| 301 | 460 | my $calling_pkg = ( caller 1 )[0]; | |||||||||||||||||||||||||||
| 302 | 460 | ▶ | unless ( $calling_pkg =~ /^Workflow/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 0 | carp "Tried to set from: ", join ', ', caller 1; | |||||||||||||||||||||||||||
| 304 | 0 | workflow_error | |||||||||||||||||||||||||||
| 305 | "Don't try to use my private setters from '$calling_pkg'!"; | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | 460 | $self->{$prop} = $value; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 36 | ▶ | sub _execute_single_action { | ||||||||||||||||||||||||||
|
Subroutine
_execute_single_action: called CC=10 SCAR=26.5
| |||||||||||||||||||||||||||||
| 312 | 36 | my ( $self, $action_name, $is_autorun, $action_args ) = @_; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | # This checks the conditions behind the scenes, so there's no | ||||||||||||||||||||||||||||
| 315 | # explicit 'check conditions' step here | ||||||||||||||||||||||||||||
| 316 | 36 | my $action = $self->get_action($action_name); | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # Need this in case we encounter an exception after we store the | ||||||||||||||||||||||||||||
| 319 | # new state | ||||||||||||||||||||||||||||
| 320 | 36 | my $old_state = $self->state; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | try { | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 36 | $self->last_action_executed($action_name); | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 36 | $action->validate($self, $action_args); | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 36 | $self->notify_observers( 'run' ); | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 36 | ▶ | $self->log->is_debug && $self->log->debug("Action validated ok"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 36 | ▶ | if ($action_args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | # Merge the action args into the context | ||||||||||||||||||||||||||||
| 333 | 0 | $self->context->param( $action_args ); | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | 36 | ▶ | my $action_return = $action->execute($self) // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$action->execute($self) // ""
MC/DC100%1/1
$action->execute($self) // ""
$action->execute($self)
Truth table100%2/2
A
A= $action->execute($self)
| |||||||||||||||||||||||||||||
| 336 | 36 | ▶ | $self->log->is_debug && $self->log->debug("Action executed ok"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # In case the state has a map defined, this returns the next state based | ||||||||||||||||||||||||||||
| 339 | # on $action_return. It will throw an error if the value is not part of | ||||||||||||||||||||||||||||
| 340 | # the map and there is no default route defined | ||||||||||||||||||||||||||||
| 341 | 36 | my $new_state = $self->_get_next_state( $action_name, $action_return ); | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 36 | ▶ | if ( $new_state ne NO_CHANGE_VALUE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 36 | ▶ | $self->log->is_info | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | && $self->log->info( | ||||||||||||||||||||||||||||
| 346 | "Set new state '$new_state' after action executed"); | ||||||||||||||||||||||||||||
| 347 | 36 | $self->state($new_state); | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # this will save the workflow histories as well as modify the | ||||||||||||||||||||||||||||
| 351 | # state of the workflow history to reflect the NEW state of | ||||||||||||||||||||||||||||
| 352 | # the workflow; if it fails we should have some means for the | ||||||||||||||||||||||||||||
| 353 | # factory to rollback other transactions... | ||||||||||||||||||||||||||||
| 354 | 36 | $self->_factory()->save_workflow( $self ); | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 36 | ▶ | $self->log->is_info | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | && $self->log->info("Saved workflow with possible new state ok"); | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 36 | $self->notify_observers( | |||||||||||||||||||||||||||
| 360 | 'completed', | ||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||
| 362 | state => $old_state, | ||||||||||||||||||||||||||||
| 363 | action => $action_name, | ||||||||||||||||||||||||||||
| 364 | autorun => $is_autorun | ||||||||||||||||||||||||||||
| 365 | }); | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 36 | ▶ | if ( $old_state ne $new_state ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 36 | $self->notify_observers( | |||||||||||||||||||||||||||
| 369 | 'state change', | ||||||||||||||||||||||||||||
| 370 | { | ||||||||||||||||||||||||||||
| 371 | from => $old_state, | ||||||||||||||||||||||||||||
| 372 | action => $action_name, | ||||||||||||||||||||||||||||
| 373 | to => $new_state | ||||||||||||||||||||||||||||
| 374 | }); | ||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 36 | return $self->_get_workflow_state; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | 36 | catch ($error) { | |||||||||||||||||||||||||||
| 381 | 0 | $self->log->error( | |||||||||||||||||||||||||||
| 382 | "Caught exception from action: $error; reset ", | ||||||||||||||||||||||||||||
| 383 | "workflow to old state '$old_state'" | ||||||||||||||||||||||||||||
| 384 | ); | ||||||||||||||||||||||||||||
| 385 | 0 | $self->state($old_state); | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | $self->notify_observers( 'rollback' ); | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # If it is a validation error we rethrow it so it can be evaluated | ||||||||||||||||||||||||||||
| 390 | # by the caller to provide better feedback to the user | ||||||||||||||||||||||||||||
| 391 | 0 | ▶ | if (Exception::Class->caught('Workflow::Exception::Validation')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 0 | $error->rethrow(); | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # Don't use 'workflow_error' here since $error should already | ||||||||||||||||||||||||||||
| 396 | # be a Workflow::Exception object or subclass | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 0 | croak $error; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 0 | ▶ | sub _get_action { # for backward compatibility with 1.49 and before | ||||||||||||||||||||||||||
|
Subroutine
_get_action: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 404 | 0 | goto &get_action; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 211 | ▶ | sub _get_workflow_state { | ||||||||||||||||||||||||||
|
Subroutine
_get_workflow_state: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 408 | 211 | my ( $self, $state ) = @_; | |||||||||||||||||||||||||||
| 409 | 211 | ▶ | $state ||= ''; # get rid of -w... | ||||||||||||||||||||||||||
|
Condition100%2/2
$state ||= ""
MC/DC100%1/1
$state ||= ""
$state
Truth table100%2/2
A
A= $state
| |||||||||||||||||||||||||||||
| 410 | 211 | ▶ | my $use_state = $state || $self->state; | ||||||||||||||||||||||||||
|
Condition66%2/3
$state || $self->state
MC/DC0%0/2
$state || $self->state
$state
$self->state
Truth table66%2/3
A || B
A= $stateB= $self->state
| |||||||||||||||||||||||||||||
| 411 | 211 | $self->log->debug( | |||||||||||||||||||||||||||
| 412 | "Finding Workflow::State object for state [given: $use_state] ", | ||||||||||||||||||||||||||||
| 413 | "[internal: ", $self->state, "]" ); | ||||||||||||||||||||||||||||
| 414 | 211 | my $wf_state = $self->{_states}{$use_state}; | |||||||||||||||||||||||||||
| 415 | 211 | ▶ | unless ($wf_state) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | 0 | workflow_error "No state '$use_state' exists in workflow '", | |||||||||||||||||||||||||||
| 417 | $self->type, "'"; | ||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | 211 | return $wf_state; | |||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 132 | ▶ | sub _set_workflow_state { | ||||||||||||||||||||||||||
|
Subroutine
_set_workflow_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 423 | 132 | my ( $self, $wf_state ) = @_; | |||||||||||||||||||||||||||
| 424 | 132 | $self->{_states}{ $wf_state->state } = $wf_state; | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 36 | ▶ | sub _get_next_state { | ||||||||||||||||||||||||||
|
Subroutine
_get_next_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 428 | 36 | my ( $self, $action_name, $action_return ) = @_; | |||||||||||||||||||||||||||
| 429 | 36 | my $wf_state = $self->_get_workflow_state; | |||||||||||||||||||||||||||
| 430 | 36 | return $wf_state->get_next_state( $action_name, $action_return ); | |||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 28 | 1; | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | __END__ | ||||||||||||||||||||||||||||