line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Minion::Tasks; |
2
|
1
|
|
|
1
|
|
66835
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
190236
|
|
|
1
|
|
|
|
|
8
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
1409
|
use Minion::Task; |
|
1
|
|
|
|
|
47606
|
|
|
1
|
|
|
|
|
11
|
|
5
|
1
|
|
|
1
|
|
578
|
use Module::Loader; |
|
1
|
|
|
|
|
15201
|
|
|
1
|
|
|
|
|
32
|
|
6
|
1
|
|
|
1
|
|
8
|
use Try::Tiny; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
402
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.0.1'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head2 register |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Register the plugin |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
0
|
|
|
0
|
1
|
|
my ($self, $app, $config) = @_; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Make sure "tasks" namespace is defined |
20
|
|
|
|
|
|
|
die __PACKAGE__, ": missing 'namespace' in config.\n" |
21
|
0
|
0
|
|
|
|
|
if (!$config->{ namespace }); |
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
|
|
|
|
|
|
|
$app->minion->add_task($module => sub { |
31
|
0
|
|
|
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
|
|
|
|
|
|
|
sub handle { |
50
|
0
|
|
|
0
|
1
|
|
my ($self, $job, $task) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
try { |
53
|
0
|
|
|
0
|
|
|
my $ok = $task->start; |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
|
if ($ok == 1) { |
56
|
|
|
|
|
|
|
# Mark as finished |
57
|
0
|
|
|
|
|
|
$job->finish($task->finish); |
58
|
|
|
|
|
|
|
} else { |
59
|
|
|
|
|
|
|
# Mark as failed |
60
|
0
|
|
|
|
|
|
$job->fail($task->error); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} catch { |
63
|
0
|
|
|
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
|
|
|
|
|
|
|
1; |