line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IO::Iron::IronWorker::Task; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
## no critic (Documentation::RequirePodAtEnd) |
4
|
|
|
|
|
|
|
## no critic (Documentation::RequirePodSections) |
5
|
|
|
|
|
|
|
## no critic (ControlStructures::ProhibitPostfixControls) |
6
|
|
|
|
|
|
|
## no critic (Subroutines::RequireArgUnpacking) |
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
68
|
use 5.010_000; |
|
4
|
|
|
|
|
21
|
|
9
|
4
|
|
|
4
|
|
20
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
79
|
|
10
|
4
|
|
|
4
|
|
21
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
95
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Global creator |
13
|
|
|
|
4
|
|
|
BEGIN { |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Global destructor |
17
|
|
|
|
4
|
|
|
END { |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# ABSTRACT: IronWorker (Online Worker Platform) Client (Task). |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = '0.12_01'; # TRIAL VERSION: generated by DZP::OurPkgVersion |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
4
|
|
|
4
|
|
46
|
use Log::Any qw($log); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
27
|
|
28
|
4
|
|
|
4
|
|
928
|
use Hash::Util 0.06 qw{lock_keys unlock_keys}; |
|
4
|
|
|
|
|
79
|
|
|
4
|
|
|
|
|
22
|
|
29
|
4
|
|
|
4
|
|
311
|
use Carp::Assert::More; |
|
4
|
|
|
|
|
26
|
|
|
4
|
|
|
|
|
674
|
|
30
|
4
|
|
|
4
|
|
27
|
use English '-no_match_vars'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
29
|
|
31
|
4
|
|
|
4
|
|
1478
|
use Params::Validate qw(:all); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
617
|
|
32
|
|
|
|
|
|
|
|
33
|
4
|
|
|
4
|
|
32
|
use IO::Iron::IronWorker::Api (); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
7582
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# CONSTANTS for this module |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# DEFAULTS |
38
|
|
|
|
|
|
|
#my $DEFAULT_DELAY = 0; |
39
|
|
|
|
|
|
|
#my $DEFAULT_TIMEOUT = 3600; |
40
|
|
|
|
|
|
|
#my $DEFAULT_PRIORITY = 0; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub new { ## no critic (Subroutines::ProhibitExcessComplexity) |
44
|
0
|
|
|
0
|
1
|
|
my ($class, $params) = @_; |
45
|
0
|
|
|
|
|
|
$log->tracef('Entering new(%s, %s)', $class, $params); |
46
|
0
|
|
|
|
|
|
my $self; |
47
|
0
|
|
|
|
|
|
my @self_keys = ( ## no critic (CodeLayout::ProhibitQuotedWordLists) |
48
|
|
|
|
|
|
|
'ironworker_client', # Reference to IronWorker client |
49
|
|
|
|
|
|
|
'connection', # Reference to REST client |
50
|
|
|
|
|
|
|
'last_http_status_code', # After successfull network operation, the return value is here. |
51
|
|
|
|
|
|
|
# Can be given when queueing a new task: |
52
|
|
|
|
|
|
|
'code_name', # The name of the code package to execute for this task (mandatory). |
53
|
|
|
|
|
|
|
'payload', # A string of data to be passed to the worker (usually JSON), can be empty (mandatory). |
54
|
|
|
|
|
|
|
'priority', # The priority queue to run the task in. Valid values are 0, 1, and 2. 0 is the default. |
55
|
|
|
|
|
|
|
'timeout', # The maximum runtime of your task in seconds. |
56
|
|
|
|
|
|
|
'delay', # The number of seconds to delay before actually queuing the task. Default is 0. |
57
|
|
|
|
|
|
|
'name', # Name of task or scheduled task. |
58
|
|
|
|
|
|
|
# These are for scheduled task: |
59
|
|
|
|
|
|
|
'run_every', # The amount of time, in seconds, between runs |
60
|
|
|
|
|
|
|
'end_at', # The time tasks will stop being queued. Should be a time or datetime. |
61
|
|
|
|
|
|
|
'run_times', # The number of times a task will run. |
62
|
|
|
|
|
|
|
'start_at', # The time the scheduled task should first be run. |
63
|
|
|
|
|
|
|
# Returned when queried a queued task: |
64
|
|
|
|
|
|
|
'id', # Task or Scheduled task id. |
65
|
|
|
|
|
|
|
'project_id', # Iron.io project ID. |
66
|
|
|
|
|
|
|
'code_id', # The code package id. |
67
|
|
|
|
|
|
|
'status', # Task execution status. |
68
|
|
|
|
|
|
|
'code_history_id', # Code package revision id? |
69
|
|
|
|
|
|
|
'code_rev', # Code package revision number. |
70
|
|
|
|
|
|
|
'start_time', # Execution started? |
71
|
|
|
|
|
|
|
'end_time', # Execution finished? |
72
|
|
|
|
|
|
|
'duration', # Execution duration? |
73
|
|
|
|
|
|
|
'updated_at', # Timestamp (ISO) of last update. |
74
|
|
|
|
|
|
|
'created_at', # Timestamp (ISO) of creation. E.g. "2012-11-10T18:31:08.064Z" |
75
|
|
|
|
|
|
|
); |
76
|
0
|
|
|
|
|
|
lock_keys(%{$self}, @self_keys); |
|
0
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
$self->{'ironworker_client'} = $params->{'ironworker_client'} if defined $params->{'ironworker_client'}; |
78
|
0
|
0
|
|
|
|
|
$self->{'connection'} = $params->{'connection'} if defined $params->{'connection'}; |
79
|
0
|
|
|
|
|
|
assert_isa( $self->{'connection'}, 'IO::Iron::Connection', 'self->{\'connection\'} is IO::Iron::Connection.' ); |
80
|
0
|
|
|
|
|
|
assert_isa( $self->{'ironworker_client'}, 'IO::Iron::IronWorker::Client', 'self->{\'ironworker_client\'} is IO::Iron::IronWorker::Client.' ); |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
$self->{'code_name'} = $params->{'code_name'}; |
83
|
0
|
|
|
|
|
|
$self->{'payload'} = $params->{'payload'}; |
84
|
0
|
0
|
|
|
|
|
$self->{'priority'} = $params->{'priority'} if defined $params->{'priority'}; |
85
|
0
|
0
|
|
|
|
|
$self->{'timeout'} = $params->{'timeout'} if defined $params->{'timeout'}; |
86
|
0
|
0
|
|
|
|
|
$self->{'delay'} = $params->{'delay'} if defined $params->{'delay'}; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
$self->{'run_every'} = $params->{'run_every'} if defined $params->{'run_every'}; |
89
|
0
|
0
|
|
|
|
|
$self->{'end_at'} = $params->{'end_at'} if defined $params->{'end_at'}; |
90
|
0
|
0
|
|
|
|
|
$self->{'run_times'} = $params->{'run_times'} if defined $params->{'run_times'}; |
91
|
0
|
0
|
|
|
|
|
$self->{'start_at'} = $params->{'start_at'} if defined $params->{'start_at'}; |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
$self->{'id'} = $params->{'id'} if defined $params->{'id'}; |
94
|
0
|
0
|
|
|
|
|
$self->{'project_id'} = $params->{'project_id'} if defined $params->{'project_id'}; |
95
|
0
|
0
|
|
|
|
|
$self->{'code_id'} = $params->{'code_id'} if defined $params->{'code_id'}; |
96
|
0
|
0
|
|
|
|
|
$self->{'status'} = $params->{'status'} if defined $params->{'status'}; |
97
|
0
|
0
|
|
|
|
|
$self->{'code_history_id'} = $params->{'code_history_id'} if defined $params->{'code_history_id'}; |
98
|
0
|
0
|
|
|
|
|
$self->{'code_rev'} = $params->{'code_rev'} if defined $params->{'code_rev'}; |
99
|
0
|
0
|
|
|
|
|
$self->{'start_time'} = $params->{'start_time'} if defined $params->{'start_time'}; |
100
|
0
|
0
|
|
|
|
|
$self->{'end_time'} = $params->{'end_time'} if defined $params->{'end_time'}; |
101
|
0
|
0
|
|
|
|
|
$self->{'duration'} = $params->{'duration'} if defined $params->{'duration'}; |
102
|
0
|
0
|
|
|
|
|
$self->{'updated_at'} = $params->{'updated_at'} if defined $params->{'updated_at'}; |
103
|
0
|
0
|
|
|
|
|
$self->{'created_at'} = $params->{'created_at'} if defined $params->{'created_at'}; |
104
|
0
|
0
|
|
|
|
|
$self->{'name'} = $params->{'name'} if defined $params->{'name'}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# All of the above can be undefined, except the codename and payload. |
107
|
0
|
|
|
|
|
|
assert_nonblank( $self->{'code_name'}, 'code_name is defined and is not blank.' ); |
108
|
0
|
|
|
|
|
|
assert_defined( $self->{'payload'}, 'payload is defined, can be blank.' ); |
109
|
|
|
|
|
|
|
# If priority, timeout or delay are undefined, the IronWorker defaults (at the server) will be used. |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
unlock_keys(%{$self}); |
|
0
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $blessed_ref = bless $self, $class; |
113
|
0
|
|
|
|
|
|
lock_keys(%{$self}, @self_keys); |
|
0
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
$log->tracef('Exiting new: %s', $blessed_ref); |
116
|
0
|
|
|
|
|
|
return $blessed_ref; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
0
|
1
|
|
sub code_name { return $_[0]->_access_internal('code_name', $_[1]); } |
121
|
0
|
|
|
0
|
1
|
|
sub payload { return $_[0]->_access_internal('payload', $_[1]); } |
122
|
0
|
|
|
0
|
1
|
|
sub priority { return $_[0]->_access_internal('priority', $_[1]); } |
123
|
0
|
|
|
0
|
1
|
|
sub timeout { return $_[0]->_access_internal('timeout', $_[1]); } |
124
|
0
|
|
|
0
|
1
|
|
sub delay { return $_[0]->_access_internal('delay', $_[1]); } |
125
|
0
|
|
|
0
|
1
|
|
sub name { return $_[0]->_access_internal('name', $_[1]); } |
126
|
|
|
|
|
|
|
# These are for scheduled task: |
127
|
0
|
|
|
0
|
1
|
|
sub run_every { return $_[0]->_access_internal('run_every', $_[1]); } |
128
|
0
|
|
|
0
|
1
|
|
sub end_at { return $_[0]->_access_internal('end_at', $_[1]); } |
129
|
0
|
|
|
0
|
1
|
|
sub run_times { return $_[0]->_access_internal('run_times', $_[1]); } |
130
|
0
|
|
|
0
|
1
|
|
sub start_at { return $_[0]->_access_internal('start_at', $_[1]); } |
131
|
|
|
|
|
|
|
# Returned when queried a queued task: |
132
|
0
|
|
|
0
|
1
|
|
sub id { return $_[0]->_access_internal('id', $_[1]); } |
133
|
0
|
|
|
0
|
1
|
|
sub project_id { return $_[0]->_access_internal('project_id', $_[1]); } |
134
|
0
|
|
|
0
|
1
|
|
sub code_id { return $_[0]->_access_internal('code_id', $_[1]); } |
135
|
0
|
|
|
0
|
1
|
|
sub status { return $_[0]->_access_internal('status', $_[1]); } |
136
|
0
|
|
|
0
|
1
|
|
sub code_history_id { return $_[0]->_access_internal('code_history_id', $_[1]); } |
137
|
0
|
|
|
0
|
1
|
|
sub code_rev { return $_[0]->_access_internal('code_rev', $_[1]); } |
138
|
0
|
|
|
0
|
1
|
|
sub start_time { return $_[0]->_access_internal('start_time', $_[1]); } |
139
|
0
|
|
|
0
|
1
|
|
sub end_time { return $_[0]->_access_internal('end_time', $_[1]); } |
140
|
0
|
|
|
0
|
1
|
|
sub duration { return $_[0]->_access_internal('duration', $_[1]); } |
141
|
0
|
|
|
0
|
1
|
|
sub updated_at { return $_[0]->_access_internal('updated_at', $_[1]); } |
142
|
0
|
|
|
0
|
1
|
|
sub created_at { return $_[0]->_access_internal('created_at', $_[1]); } |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub _access_internal { |
145
|
0
|
|
|
0
|
|
|
my ($self, $var_name, $var_value) = @_; |
146
|
0
|
|
|
|
|
|
$log->tracef('_access_internal(%s, %s)', $var_name, $var_value); |
147
|
0
|
0
|
|
|
|
|
if( defined $var_value ) { |
148
|
0
|
|
|
|
|
|
$self->{$var_name} = $var_value; |
149
|
0
|
|
|
|
|
|
return $self; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
else { |
152
|
0
|
|
|
|
|
|
return $self->{$var_name}; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub log { ## no critic (Subroutines::ProhibitBuiltinHomonyms) |
158
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
159
|
0
|
|
|
|
|
|
$log->tracef('Entering log().'); |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my $task_id = $self->id(); |
162
|
0
|
|
|
|
|
|
assert_nonblank($task_id, 'task id not set. Task queued yet?'); |
163
|
0
|
|
|
|
|
|
my $connection = $self->{'connection'}; |
164
|
0
|
|
|
|
|
|
my ( $http_status_code, $response_message ) = |
165
|
|
|
|
|
|
|
$connection->perform_iron_action( |
166
|
|
|
|
|
|
|
IO::Iron::IronWorker::Api::IRONWORKER_GET_A_TASKS_LOG(), |
167
|
|
|
|
|
|
|
{ '{Task ID}' => $task_id, } ); |
168
|
0
|
|
|
|
|
|
$self->{'last_http_status_code'} = $http_status_code; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
$log->tracef( 'Exiting log(): %s', $response_message ); |
171
|
0
|
|
|
|
|
|
return $response_message; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub cancel { |
176
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
177
|
0
|
|
|
|
|
|
$log->tracef('Entering cancel().'); |
178
|
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
|
my $task_id = $self->id(); |
180
|
0
|
|
|
|
|
|
assert_nonblank($task_id, 'task id not set. Task queued yet?'); |
181
|
0
|
|
|
|
|
|
my $connection = $self->{'connection'}; |
182
|
0
|
|
|
|
|
|
my ( $http_status_code, $response_message ) = |
183
|
|
|
|
|
|
|
$connection->perform_iron_action( |
184
|
|
|
|
|
|
|
IO::Iron::IronWorker::Api::IRONWORKER_CANCEL_A_TASK(), |
185
|
|
|
|
|
|
|
{ '{Task ID}' => $task_id, } ); |
186
|
0
|
|
|
|
|
|
$self->{'last_http_status_code'} = $http_status_code; |
187
|
0
|
|
|
|
|
|
assert_is($response_message->{'msg'}, 'Cancelled'); |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
$log->tracef( 'Exiting cancel(): %s', 1 ); |
190
|
0
|
|
|
|
|
|
return 1; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub set_progress { |
195
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
196
|
|
|
|
|
|
|
my %params = validate_with( |
197
|
|
|
|
|
|
|
'params' => \@_, |
198
|
0
|
|
|
0
|
|
|
'normalize_keys' => sub { return lc shift }, |
199
|
0
|
|
|
|
|
|
'spec' => { |
200
|
|
|
|
|
|
|
'percent' => { type => SCALAR, }, # percentage. |
201
|
|
|
|
|
|
|
'msg' => { type => SCALAR, }, # message. |
202
|
|
|
|
|
|
|
}, |
203
|
|
|
|
|
|
|
); |
204
|
0
|
|
|
|
|
|
$log->tracef('Entering set_progress(%s)', \%params); |
205
|
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
my $task_id = $self->id(); |
207
|
0
|
|
|
|
|
|
assert_nonblank($task_id, 'task id not set. Task queued yet?'); |
208
|
0
|
|
|
|
|
|
my $connection = $self->{'connection'}; |
209
|
0
|
|
|
|
|
|
my ( $http_status_code, $response_message ) = $connection->perform_iron_action( |
210
|
|
|
|
|
|
|
IO::Iron::IronWorker::Api::IRONWORKER_SET_A_TASKS_PROGRESS(), |
211
|
|
|
|
|
|
|
{ '{Task ID}' => $task_id, |
212
|
|
|
|
|
|
|
'body' => \%params, |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
); |
215
|
0
|
|
|
|
|
|
$self->{'last_http_status_code'} = $http_status_code; |
216
|
0
|
|
|
|
|
|
assert_is($response_message->{'msg'}, 'Progress set'); |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
$log->tracef( 'Exiting set_progress(): %s', 1 ); |
219
|
0
|
|
|
|
|
|
return 1; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub retry { |
224
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
225
|
|
|
|
|
|
|
my %params = validate_with( |
226
|
|
|
|
|
|
|
'params' => \@_, |
227
|
0
|
|
|
0
|
|
|
'normalize_keys' => sub { return lc shift }, |
228
|
0
|
|
|
|
|
|
'spec' => { |
229
|
|
|
|
|
|
|
'delay' => { type => SCALAR, }, # delay |
230
|
|
|
|
|
|
|
}, |
231
|
|
|
|
|
|
|
); |
232
|
0
|
|
|
|
|
|
$log->tracef( 'Entering retry(%s)', \%params ); |
233
|
|
|
|
|
|
|
|
234
|
0
|
|
|
|
|
|
my $task_id = $self->id(); |
235
|
0
|
|
|
|
|
|
assert_nonblank($task_id, 'task id not set. Task queued yet?'); |
236
|
0
|
|
|
|
|
|
my $connection = $self->{'connection'}; |
237
|
0
|
|
|
|
|
|
my ( $http_status_code, $response_message ) = $connection->perform_iron_action( |
238
|
|
|
|
|
|
|
IO::Iron::IronWorker::Api::IRONWORKER_RETRY_A_TASK(), |
239
|
|
|
|
|
|
|
{ |
240
|
|
|
|
|
|
|
'{Task ID}' => $task_id, |
241
|
|
|
|
|
|
|
'body' => \%params, |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
); |
244
|
0
|
|
|
|
|
|
$self->{'last_http_status_code'} = $http_status_code; |
245
|
0
|
|
|
|
|
|
assert_is($response_message->{'msg'}, 'Queued up'); |
246
|
0
|
|
|
|
|
|
my $new_task_id = $response_message->{'tasks'}->[0]->{'id'}; |
247
|
0
|
|
|
|
|
|
$self->id($new_task_id); # We get a new id. |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
$log->tracef( 'Exiting retry(): %s', $new_task_id ); |
250
|
0
|
|
|
|
|
|
return $new_task_id; |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub cancel_scheduled { |
255
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
256
|
0
|
|
|
|
|
|
$log->tracef('Entering cancel_scheduled().'); |
257
|
|
|
|
|
|
|
|
258
|
0
|
|
|
|
|
|
my $task_id = $self->id(); |
259
|
0
|
|
|
|
|
|
assert_nonblank($task_id, 'task id not set. Task scheduled yet?'); |
260
|
0
|
|
|
|
|
|
my $connection = $self->{'connection'}; |
261
|
0
|
|
|
|
|
|
my ( $http_status_code, $response_message ) = |
262
|
|
|
|
|
|
|
$connection->perform_iron_action( |
263
|
|
|
|
|
|
|
IO::Iron::IronWorker::Api::IRONWORKER_CANCEL_A_SCHEDULED_TASK(), |
264
|
|
|
|
|
|
|
{ '{Schedule ID}' => $task_id, } ); |
265
|
0
|
|
|
|
|
|
$self->{'last_http_status_code'} = $http_status_code; |
266
|
0
|
|
|
|
|
|
assert_is($response_message->{'msg'}, 'Cancelled'); |
267
|
|
|
|
|
|
|
|
268
|
0
|
|
|
|
|
|
$log->tracef( 'Exiting cancel_scheduled(): %s', 1 ); |
269
|
0
|
|
|
|
|
|
return 1; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
1; |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
__END__ |