line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Minion::Backend::API; |
2
|
1
|
|
|
1
|
|
6513
|
use Mojo::Base 'Minion::Backend'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
1609
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
12
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 0.01; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'ua'; |
9
|
|
|
|
|
|
|
has 'url'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub broadcast { |
12
|
1
|
|
50
|
1
|
1
|
976
|
my ($self, $command, $args, $ids) = (shift, shift, shift || [], shift || []); |
|
|
|
50
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
|
|
3
|
my $res = $self->ua->put( |
15
|
|
|
|
|
|
|
$self->url . '/broadcast' |
16
|
|
|
|
|
|
|
=> json => { |
17
|
|
|
|
|
|
|
command => $command, |
18
|
|
|
|
|
|
|
args => $args, |
19
|
|
|
|
|
|
|
ids => $ids |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
)->result; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
|
|
20920
|
$self->_success($res); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub dequeue { |
27
|
1
|
|
|
1
|
1
|
4
|
my ($self, $id, $wait, $options) = @_; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# slows down |
30
|
1
|
|
|
|
|
500599
|
select(undef, undef, undef, 0.5); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $res = $self->ua->post( |
33
|
|
|
|
|
|
|
$self->url . '/dequeue' |
34
|
|
|
|
|
|
|
=> json => { |
35
|
|
|
|
|
|
|
id => $id, |
36
|
|
|
|
|
|
|
wait => $wait, |
37
|
|
|
|
|
|
|
options => $options, |
38
|
1
|
|
|
|
|
26
|
tasks => [keys %{$self->minion->tasks}] |
|
1
|
|
|
|
|
55
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
)->result; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
$self->_success($res); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub enqueue { |
46
|
1
|
|
50
|
1
|
1
|
723
|
my ($self, $task, $args, $options) = (shift, shift, shift || [], shift || {}); |
|
|
|
50
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
7
|
my $res = $self->ua->post( |
49
|
|
|
|
|
|
|
$self->url . '/enqueue' |
50
|
|
|
|
|
|
|
=> json => { |
51
|
|
|
|
|
|
|
task => $task, |
52
|
|
|
|
|
|
|
args => $args, |
53
|
|
|
|
|
|
|
options => $options |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
)->result; |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
9682
|
$self->_success($res); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub fail_job { |
61
|
1
|
|
|
1
|
1
|
4
|
my ($self, $id, $retries, $result) = @_; |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
5
|
my $res = $self->ua->patch( |
64
|
|
|
|
|
|
|
$self->url . '/fail-job' |
65
|
|
|
|
|
|
|
=> json => { |
66
|
|
|
|
|
|
|
id => $id, |
67
|
|
|
|
|
|
|
retries => $retries, |
68
|
|
|
|
|
|
|
result => $result |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
)->result; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
8449
|
$self->_success($res); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub finish_job { |
76
|
1
|
|
|
1
|
1
|
3
|
my ($self, $id, $retries, $result) = @_; |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
5
|
my $res = $self->ua->patch( |
79
|
|
|
|
|
|
|
$self->url . '/finish-job' |
80
|
|
|
|
|
|
|
=> json => { |
81
|
|
|
|
|
|
|
id => $id, |
82
|
|
|
|
|
|
|
retries => $retries, |
83
|
|
|
|
|
|
|
result => $result |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
)->result; |
86
|
|
|
|
|
|
|
|
87
|
1
|
|
|
|
|
7859
|
$self->_success($res); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub history { |
91
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
4
|
my $res = $self->ua->get($self->url . '/history')->result; |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
9267
|
$self->_success($res); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub list_jobs { |
99
|
1
|
|
|
1
|
1
|
4
|
my ($self, $offset, $limit, $options) = @_; |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
|
|
6
|
my $res = $self->ua->get( |
102
|
|
|
|
|
|
|
$self->url . '/list-jobs' |
103
|
|
|
|
|
|
|
=> json => { |
104
|
|
|
|
|
|
|
offset => $offset, |
105
|
|
|
|
|
|
|
limit => $limit, |
106
|
|
|
|
|
|
|
options => $options |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
)->result; |
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
8538
|
$self->_success($res); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub list_locks { |
114
|
1
|
|
|
1
|
1
|
5
|
my ($self, $offset, $limit, $options) = @_; |
115
|
|
|
|
|
|
|
|
116
|
1
|
|
|
|
|
3
|
my $res = $self->ua->get( |
117
|
|
|
|
|
|
|
$self->url . '/list-locks' |
118
|
|
|
|
|
|
|
=> json => { |
119
|
|
|
|
|
|
|
offset => $offset, |
120
|
|
|
|
|
|
|
limit => $limit, |
121
|
|
|
|
|
|
|
options => $options |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
)->result; |
124
|
|
|
|
|
|
|
|
125
|
1
|
|
|
|
|
8463
|
$self->_success($res); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub list_workers { |
129
|
1
|
|
|
1
|
1
|
4
|
my ($self, $offset, $limit, $options) = @_; |
130
|
|
|
|
|
|
|
|
131
|
1
|
|
|
|
|
5
|
my $res = $self->ua->get( |
132
|
|
|
|
|
|
|
$self->url . '/list-workers' |
133
|
|
|
|
|
|
|
=> json => { |
134
|
|
|
|
|
|
|
offset => $offset, |
135
|
|
|
|
|
|
|
limit => $limit, |
136
|
|
|
|
|
|
|
options => $options |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
)->result; |
139
|
|
|
|
|
|
|
|
140
|
1
|
|
|
|
|
8874
|
$self->_success($res); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub lock { |
144
|
1
|
|
50
|
1
|
1
|
12
|
my ($self, $name, $duration, $options) = (shift, shift, shift, shift // {}); |
145
|
|
|
|
|
|
|
|
146
|
1
|
|
|
|
|
5
|
my $res = $self->ua->get( |
147
|
|
|
|
|
|
|
$self->url . '/lock' |
148
|
|
|
|
|
|
|
=> json => { |
149
|
|
|
|
|
|
|
name => $name, |
150
|
|
|
|
|
|
|
duration => $duration, |
151
|
|
|
|
|
|
|
options => $options |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
)->result; |
154
|
|
|
|
|
|
|
|
155
|
1
|
|
|
|
|
8392
|
$self->_success($res); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub new { |
159
|
1
|
|
|
1
|
1
|
589
|
my ($self, @args) = @_; |
160
|
|
|
|
|
|
|
|
161
|
1
|
50
|
33
|
|
|
13
|
my $ua = $args[1] && $args[1]->isa('Mojo::UserAgent') |
162
|
|
|
|
|
|
|
? $args[1] |
163
|
|
|
|
|
|
|
: Mojo::UserAgent->new; |
164
|
|
|
|
|
|
|
|
165
|
1
|
|
|
|
|
8
|
my $url = $args[0]; |
166
|
1
|
|
|
|
|
3
|
$url =~ s/\/$//; |
167
|
|
|
|
|
|
|
|
168
|
1
|
|
|
|
|
5
|
return $self->SUPER::new(ua => $ua, url => $url); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub note { |
172
|
1
|
|
|
1
|
1
|
4
|
my ($self, $id, $merge) = @_; |
173
|
|
|
|
|
|
|
|
174
|
1
|
|
|
|
|
4
|
my $res = $self->ua->patch( |
175
|
|
|
|
|
|
|
$self->url . '/note' |
176
|
|
|
|
|
|
|
=> json => { |
177
|
|
|
|
|
|
|
id => $id, |
178
|
|
|
|
|
|
|
merge => $merge |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
)->result; |
181
|
|
|
|
|
|
|
|
182
|
1
|
|
|
|
|
8808
|
$self->_success($res); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub receive { |
186
|
1
|
|
|
1
|
1
|
4
|
my ($self, $id) = @_; |
187
|
|
|
|
|
|
|
|
188
|
1
|
|
|
|
|
5
|
my $res = $self->ua->patch( |
189
|
|
|
|
|
|
|
$self->url . '/receive' |
190
|
|
|
|
|
|
|
=> json => { |
191
|
|
|
|
|
|
|
id => $id |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
)->result; |
194
|
|
|
|
|
|
|
|
195
|
1
|
|
|
|
|
8365
|
$self->_success($res); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub register_worker { |
199
|
1
|
|
50
|
1
|
1
|
9
|
my ($self, $id, $options) = (shift, shift, shift || {}); |
200
|
|
|
|
|
|
|
|
201
|
1
|
|
|
|
|
6
|
my $res = $self->ua->post( |
202
|
|
|
|
|
|
|
$self->url . '/register-worker' |
203
|
|
|
|
|
|
|
=> json => { |
204
|
|
|
|
|
|
|
id => $id, |
205
|
|
|
|
|
|
|
options => $options |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
)->result; |
208
|
|
|
|
|
|
|
|
209
|
1
|
|
|
|
|
10216
|
$self->_success($res); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub remove_job { |
213
|
1
|
|
|
1
|
1
|
4
|
my ($self, $id) = @_; |
214
|
|
|
|
|
|
|
|
215
|
1
|
|
|
|
|
5
|
my $res = $self->ua->delete( |
216
|
|
|
|
|
|
|
$self->url . '/remove-job' |
217
|
|
|
|
|
|
|
=> json => { |
218
|
|
|
|
|
|
|
id => $id |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
)->result; |
221
|
|
|
|
|
|
|
|
222
|
1
|
|
|
|
|
10820
|
$self->_success($res); |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub repair { |
226
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
227
|
|
|
|
|
|
|
|
228
|
1
|
|
|
|
|
6
|
my $res = $self->ua->post( |
229
|
|
|
|
|
|
|
$self->url . '/repair' |
230
|
|
|
|
|
|
|
)->result; |
231
|
|
|
|
|
|
|
|
232
|
1
|
|
|
|
|
9227
|
$self->_success($res); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub reset { |
236
|
1
|
|
50
|
1
|
1
|
9
|
my ($self, $options) = (shift, shift // {}); |
237
|
|
|
|
|
|
|
|
238
|
1
|
|
|
|
|
4
|
my $res = $self->ua->post( |
239
|
|
|
|
|
|
|
$self->url . '/reset' |
240
|
|
|
|
|
|
|
=> json => { |
241
|
|
|
|
|
|
|
options => $options |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
)->result; |
244
|
|
|
|
|
|
|
|
245
|
1
|
|
|
|
|
8062
|
$self->_success($res); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub retry_job { |
249
|
1
|
|
50
|
1
|
1
|
10
|
my ($self, $id, $retries, $options) = (shift, shift, shift, shift || {}); |
250
|
|
|
|
|
|
|
|
251
|
1
|
|
|
|
|
5
|
my $res = $self->ua->put( |
252
|
|
|
|
|
|
|
$self->url . '/retry-job' |
253
|
|
|
|
|
|
|
=> json => { |
254
|
|
|
|
|
|
|
id => $id, |
255
|
|
|
|
|
|
|
retries => $retries, |
256
|
|
|
|
|
|
|
options => $options |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
)->result; |
259
|
|
|
|
|
|
|
|
260
|
1
|
|
|
|
|
8935
|
$self->_success($res); |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub stats { |
264
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
265
|
|
|
|
|
|
|
|
266
|
1
|
|
|
|
|
5
|
my $res = $self->ua->get($self->url . '/stats')->result; |
267
|
|
|
|
|
|
|
|
268
|
1
|
|
|
|
|
10540
|
$self->_success($res); |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
sub unlock { |
272
|
1
|
|
|
1
|
1
|
4
|
my ($self, $name) = @_; |
273
|
|
|
|
|
|
|
|
274
|
1
|
|
|
|
|
7
|
my $res = $self->ua->delete( |
275
|
|
|
|
|
|
|
$self->url . '/unlock' |
276
|
|
|
|
|
|
|
=> json => { |
277
|
|
|
|
|
|
|
name => $name |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
)->result; |
280
|
|
|
|
|
|
|
|
281
|
1
|
|
|
|
|
10559
|
$self->_success($res); |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
sub unregister_worker { |
285
|
1
|
|
|
1
|
1
|
3
|
my ($self, $id) = @_; |
286
|
|
|
|
|
|
|
|
287
|
1
|
|
|
|
|
5
|
my $res = $self->ua->delete( |
288
|
|
|
|
|
|
|
$self->url . '/unregister-worker' |
289
|
|
|
|
|
|
|
=> json => { |
290
|
|
|
|
|
|
|
id => $id |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
)->result; |
293
|
|
|
|
|
|
|
|
294
|
1
|
|
|
|
|
8530
|
$self->_success($res); |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
sub _success { |
298
|
19
|
|
|
19
|
|
62
|
my ($self, $res) = @_; |
299
|
|
|
|
|
|
|
|
300
|
19
|
50
|
|
|
|
68
|
if ($res->is_success) { |
301
|
19
|
|
|
|
|
366
|
my $data = $res->json; |
302
|
|
|
|
|
|
|
|
303
|
19
|
50
|
50
|
|
|
3509
|
return $data->{result} || undef if $data->{success}; |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
0
|
|
|
|
|
|
return; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
1; |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=encoding utf8 |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=head1 NAME |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
Minion::Backend::API - API Rest backend |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=head1 SYNOPSIS |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
# simple |
320
|
|
|
|
|
|
|
use Minion::Backend::API; |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
my $backend = Minion::Backend::API->new('https://my-api.com'); |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
# using with your own Mojo::UserAgent |
325
|
|
|
|
|
|
|
use Mojo::UserAgent; |
326
|
|
|
|
|
|
|
use Minion::Backend::API; |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
my $ua = Mojo::UserAgent->new; |
329
|
|
|
|
|
|
|
my $backend = Minion::Backend::API->new('https://my-api.com', $ua); |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=head1 DESCRIPTION |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
L is a backend for L based on L. |
334
|
|
|
|
|
|
|
This module need be used together with the module L, |
335
|
|
|
|
|
|
|
access it to see manual. |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
L inherits all attributes from L and |
340
|
|
|
|
|
|
|
implements the following new ones. |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=head2 url |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
my $url = $backend->url; |
345
|
|
|
|
|
|
|
$backend = $backend->url('https://my-api.com'); |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
=head2 ua |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
my $ua = $backend->ua; |
350
|
|
|
|
|
|
|
$backend = $backend->ua(Mojo::UserAgent->new); |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
=head1 SEE MORE OPTIONS |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
L |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
=head1 SEE ALSO |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
L, L, L, L, L. |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
=head1 AUTHOR |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
Lucas Tiago de Moraes C |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
This software is copyright (c) 2020 by Lucas Tiago de Moraes. |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=cut |