Skip to content

Commit

Permalink
Pass action arguments to 'execute_action'
Browse files Browse the repository at this point in the history
By passing the arguments to 'execute_action', there's no need to
directly manipulate the workflow context. This way, the context
is treated as workflow internal state.
  • Loading branch information
ehuelsmann committed Jun 14, 2022
1 parent 58c2148 commit a29aecd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
26 changes: 19 additions & 7 deletions lib/Workflow.pm
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ sub get_action_fields {
}

sub execute_action {
my ( $self, $action_name ) = @_;
my ( $self, $action_name, $action_args ) = @_;

while ( $action_name ) {
my $wf_state =
$self->_execute_single_action( $action_name );
$self->_execute_single_action( $action_name, $action_args );
$action_args = undef;

if ( not $wf_state->autorun ) {
last;
Expand Down Expand Up @@ -279,7 +280,7 @@ sub set {


sub _execute_single_action {
my ( $self, $action_name ) = @_;
my ( $self, $action_name, $action_args ) = @_;

# This checks the conditions behind the scenes, so there's no
# explicit 'check conditions' step here
Expand All @@ -291,8 +292,15 @@ sub _execute_single_action {
my ( $new_state, $action_return );

try {
$action->validate($self);
$action->validate($self, $action_args);
$self->log->is_debug && $self->log->debug("Action validated ok");
if ($action_args) {
# Merge the action args into the context
my $ctx = $self->context;
while (my ($k, $v) = each %{$action_args}) {
$ctx->param( $k, $v );
}
}
$action_return = $action->execute($self);
$self->log->is_debug && $self->log->debug("Action executed ok");

Expand Down Expand Up @@ -517,10 +525,10 @@ This documentation describes version 1.57 of Workflow
my $context = $workflow->context;
$context->param( current_user => $user );
$context->param( sections => \@sections );
$context->param( path => $path_to_file );
# Execute one of them
$workflow->execute_action( 'upload file' );
$workflow->execute_action( 'upload file',
{ path => $path_to_file });
print "New state: ", $workflow->state, "\n";
Expand Down Expand Up @@ -931,13 +939,17 @@ than the entire system.
=head2 Object Methods
=head3 execute_action( $action_name )
=head3 execute_action( $action_name, $args )
Execute the action C<$action_name>. Typically this changes the state
of the workflow. If C<$action_name> is not in the current state, fails
one of the conditions on the action, or fails one of the validators on
the action an exception is thrown.
The C<$args> provided, are checked against the validators to ensure the
context remains in a valid state; upon successful validation, the C<$args>
are merged into the context and the action is executed as described above.
After the action has been successfully executed and the workflow saved
we issue a 'execute' observation with the old state, action name and
an autorun flag as additional parameters.
Expand Down
15 changes: 7 additions & 8 deletions lib/Workflow/Action.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ sub get_validators {
}

sub validate {
my ( $self, $wf ) = @_;
my ( $self, $wf, $action_args ) = @_;
my @validators = $self->get_validators;
return unless ( scalar @validators );

Expand All @@ -75,17 +75,15 @@ sub validate {
my $validator = $validator_info->{validator};
my $args = $validator_info->{args};

# TODO: Revisit this statement it does not look right
# runtime_args becomes the WF object??
my @runtime_args = ($wf);
my @runtime_args = ();
foreach my $arg ( @{$args} ) {
if ( $arg =~ /^\$(.*)$/ ) {
push @runtime_args, $context->param($1);
push @runtime_args, $action_args->{$1};
} else {
push @runtime_args, $arg;
}
}
$validator->validate(@runtime_args);
$validator->validate($wf, @runtime_args);
}
}

Expand Down Expand Up @@ -460,9 +458,10 @@ L<Workflow::Validator> object, while 'args' contains an arrayref of
arguments to pass to the validator, some of which may need to be
evaluated at runtime.
=head3 validate( $workflow )
=head3 validate( $workflow, $action_args )
Run through all validators for this action. If any fail they will
Run through all validators for this action, using the arguments
provided with the C<execute_action> call. If any fail they will
throw a L<Workflow::Exception>, the validation subclass.
=head3 execute( $workflow )
Expand Down

0 comments on commit a29aecd

Please sign in to comment.