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