NAME
Zapp::Task - Base class for individual steps in a job
VERSION
version 0.005
SYNOPSIS
package My::Task::Greet;
use Mojo::Base 'Zapp::Task', -signatures;
# Perform the task
sub run( $self, $input ) {
return $self->fail( 'No-one to greet' ) if !$input->{who};
return $self->finish({
greeting => "Hello, $input->{who}!",
});
}
1;
__DATA__
@@ input.html.ep
%# Display the form to configure this task
%= text_field 'who', value => $input->{who}
@@ output.html.ep
%# Show the result of this task
%# XXX: Switch to $task->{error} if it's an actual error
% if ( !ref $task->{output} ) {
<p>I couldn't send a greeting: <%= $task->{output} %></p>
% }
% else {
<p>I sent a greeting of <q><%= $task->{output}{greeting} %></q></p>
% }
DESCRIPTION
Zapp::Task is the base class for all tasks. Tasks are the individual steps of a job. Each task has an input template to configure its input and an output template to display its output. Task input and output are declared with JSON Schema (see the "schema" method), and input will be processed with Zapp::Formula before being given to "run".
Creating a Task
To create a task, start by extending this class. Create a run
method that takes one argument, $input
. Inside this method, do the work you want to do. Then, in the DATA
section of your module, create two templates: input.html.ep
for the input form, and output.html.ep
for the output display.
The input form should have a set of fields. Field names will be used as the keys to the $input
hash reference given to the run
method. You can build complex data structures using [\d]
to make arrays and .
to nest hashes (see "build_data_from_params" in Zapp::Util).
<!-- { name => 'Planet Express', loc => { city => 'New New York' } } -->
<input name="name" value="Planet Express">
<input name="loc.city" value="New New York">
Input forms can also use special data-zapp-*
attributes to add dynamic features without writing the JavaScript yourself. See "parse_zapp_attrs" in Mojo::Util.
Your run
method should do the actual work, and then call either finish
(for success) or fail
(for failure) with the output data. This data will be given to the output.html.ep
template for display.
SEE ALSO
Zapp::Task::Action, Minion::Job, Zapp
AUTHOR
Doug Bell <preaction@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2021 by Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.