| 1 | package Mojolicious::Plugin::Minion::Tasks; | ||||||||||
| 2 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use Minion::Task; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Module::Loader; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | our $VERSION = '0.0.1'; | |||||||||
| 9 | |||||||||||
| 10 | =head2 register | ||||||||||
| 11 | |||||||||||
| 12 | Register the plugin | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | sub register { | ||||||||
|
Subroutine
register: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 17 | 0 | my ($self, $app, $config) = @_; | |||||||||
| 18 | |||||||||||
| 19 | # Make sure "tasks" namespace is defined | ||||||||||
| 20 | die __PACKAGE__, ": missing 'namespace' in config.\n" | ||||||||||
| 21 | 0 | ▶ | if (!$config->{ namespace }); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | |||||||||||
| 23 | 0 | my $loader = Module::Loader->new; | |||||||||
| 24 | # Find all the modules in the given namespace | ||||||||||
| 25 | 0 | my @modules = $loader->find_modules($config->{ namespace }); | |||||||||
| 26 | |||||||||||
| 27 | 0 | foreach my $module (@modules) { | |||||||||
| 28 | # And add the module to the list of tasks | ||||||||||
| 29 | # e.g.: "MyApp::Tasks::SayHello" | ||||||||||
| 30 | 0 | ▶ | $app->minion->add_task($module => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 31 | 0 | my ($job, $args) = @_; | |||||||||
| 32 | |||||||||||
| 33 | 0 | $loader->load($module); | |||||||||
| 34 | |||||||||||
| 35 | 0 | my $task = $module->new({ id => $job->id, app => $app, args => $args }); | |||||||||
| 36 | |||||||||||
| 37 | # Handle the task | ||||||||||
| 38 | 0 | $self->handle($job, $task); | |||||||||
| 39 | 0 | }); | |||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | =head2 handle | ||||||||||
| 44 | |||||||||||
| 45 | Handle the given task | ||||||||||
| 46 | |||||||||||
| 47 | =cut | ||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub handle { | ||||||||
|
Subroutine
handle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 50 | 0 | my ($self, $job, $task) = @_; | |||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 53 | 0 | my $ok = $task->start; | |||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | if ($ok == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | # Mark as finished | ||||||||||
| 57 | 0 | $job->finish($task->finish); | |||||||||
| 58 | } else { | ||||||||||
| 59 | # Mark as failed | ||||||||||
| 60 | 0 | $job->fail($task->error); | |||||||||
| 61 | } | ||||||||||
| 62 | 0 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 0 | $job->app->log->warn($_); | |||||||||
| 64 | 0 | $job->fail($_); | |||||||||
| 65 | 0 | }; | |||||||||
| 66 | |||||||||||
| 67 | # Update the tags for the current job | ||||||||||
| 68 | # See Mojolicious::Plugin::Minion::Overview for more details | ||||||||||
| 69 | 0 | $job->note(tags => $task->tags); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 2 | 1; | |||||||||