line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Minion::Task; |
2
|
1
|
|
|
1
|
|
69052
|
use Mojo::Base -base; |
|
1
|
|
|
|
|
195465
|
|
|
1
|
|
|
|
|
7
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
700
|
use Mojo::Loader qw(load_class); |
|
1
|
|
|
|
|
36043
|
|
|
1
|
|
|
|
|
63
|
|
5
|
1
|
|
|
1
|
|
501
|
use Mojo::Server; |
|
1
|
|
|
|
|
9802
|
|
|
1
|
|
|
|
|
10
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.0.3'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has [qw/id guard parent_id user/]; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'app' => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') }, weak => 1; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'args' => sub { {} }; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has 'chain' => sub { shift->args->{ chain } || [] }; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has 'children' => sub { |
18
|
|
|
|
|
|
|
my $self = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my @children = @{ $self->subtasks }; |
21
|
|
|
|
|
|
|
push(@children, @{ $self->chain }); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
return \@children; |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has 'error' => 'Failed.'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has 'failed' => sub { 0 }; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has 'finish' => 'Action complete.'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has 'options' => sub { |
33
|
|
|
|
|
|
|
my $self = shift; |
34
|
|
|
|
|
|
|
my $options = { |
35
|
|
|
|
|
|
|
queue => 'default', |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
if ($self->parent_id) { |
39
|
|
|
|
|
|
|
$options->{ parents } = [$self->parent_id]; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
return $options; |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has 'subtasks' => sub { [] }; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has 'tags' => sub { |
48
|
|
|
|
|
|
|
my $self = shift; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my @tags = $self->tagify($self->args); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
return \@tags; |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 dispatch |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Dispatch the task |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub dispatch { |
63
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
return $self->start; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 processChain |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Process the chain of tasks |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub processChain { |
75
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my @children = @{ $self->children }; |
|
0
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
my $task = shift(@children); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $e = load_class($task); |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
$self->app->log->fatal("Loading '$task' failed: $e") if ($e); |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
$task->new(app => $self->app, args => $self->args, parent_id => $self->id) |
85
|
|
|
|
|
|
|
->withChain(\@children) |
86
|
|
|
|
|
|
|
->dispatch; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 start |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Start the task |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub start { |
96
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $ok = $self->run(@_); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# When current task was successful, |
101
|
|
|
|
|
|
|
# and there are children tasks defined, |
102
|
|
|
|
|
|
|
# try to process them |
103
|
0
|
0
|
0
|
|
|
|
if ($ok && scalar(@{ $self->children })) { |
|
0
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
$self->processChain; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
return $ok; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 tag |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Convert name and value to a tag |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub tag { |
117
|
0
|
|
|
0
|
1
|
|
my ($self, $name, $value) = @_; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
return sprintf("%s %s", $name, $value); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 tagify |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Convert a hash to an array of tags |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub tagify { |
129
|
0
|
|
|
0
|
1
|
|
my ($self, $args, $prefix) = @_; |
130
|
0
|
|
0
|
|
|
|
$prefix ||= ''; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my @tags; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
for my $key (sort(keys(%{ $args }))) { |
|
0
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
if (ref($args->{ $key }) eq 'HASH') { |
|
|
0
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
push(@tags, $self->tagify($args->{ $key }, $prefix . $key . ' ')); |
137
|
|
|
|
|
|
|
} elsif (ref($args->{ $key }) eq 'ARRAY') { |
138
|
0
|
|
|
|
|
|
push(@tags, $self->tag($prefix . $key, join(', ', @{ $args->{ $key } }))); |
|
0
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
} else { |
140
|
0
|
|
|
|
|
|
push(@tags, $self->tag($prefix . $key, $args->{ $key })); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return @tags; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=encoding utf8 |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 NAME |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Minion::Task - A task boilerplate for Minion |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 SYNOPSIS |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
package MyApp::Tasks::HelloWorld; |
158
|
|
|
|
|
|
|
use Mojo::Base 'Minion::Task'; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
... defined your own logic here ... |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 DESCRIPTION |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
L is a package that provides a way of handling minion tasks |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
L inherits all attributes from L. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 METHODS |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
L inherits all methods from L and implements |
173
|
|
|
|
|
|
|
the following new ones. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 dispatch |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
$task->dispatch; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Is basically a link to start method. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 processChain |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
$task->processChain; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
If current task has subtasks or if there's something defined in the args->{ chain }, |
186
|
|
|
|
|
|
|
those tasks will be dispatched. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 start |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
$task->start; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Start processing the task. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 SEE ALSO |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
L, L, L, L. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |