blib/lib/IO/Iron/IronWorker/Task.pm

filter: Statementstmt 18.6% 28 / 150 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 26.8% 11 / 41 Podpod 100% 27 / 27 totaltotal 25.1% 66 / 262 SCAR 58.7
CC
23
Cov
14%
new
506.0
_access_internal
6.0
code_name
2.0
CRAP 354.4
« blib/lib/IO/Iron/IronMQ/Queue.pm ↑ Summary blib/lib/IO/Iron/Connection.pm »
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 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Global creator
13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 }
15  
16 # Global destructor
17 ▶END {
Subroutine END: called CC=1 SCAR=0.0
18 }
19  
20  
21 # ABSTRACT: IronWorker (Online Worker Platform) Client (Task).
22  
23 4 our $VERSION = '0.13'; # VERSION: generated by DZP::OurPkgVersion
24  
25  
26  
27 4 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use Hash::Util 0.06 qw{lock_keys unlock_keys};
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use Carp::Assert::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 4 ▶use IO::Iron::IronWorker::Api ();
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ my ($class, $params) = @_;
Subroutine new: not called CC=22 SCAR=62.3
Pod documented
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);
77 0 ▶ $self->{'ironworker_client'} = $params->{'ironworker_client'} if defined $params->{'ironworker_client'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"ironworker_client"} 0 0 0
78 0 ▶ $self->{'connection'} = $params->{'connection'} if defined $params->{'connection'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"connection"} 0 0 0
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 ▶ $self->{'priority'} = $params->{'priority'} if defined $params->{'priority'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"priority"} 0 0 0
85 0 ▶ $self->{'timeout'} = $params->{'timeout'} if defined $params->{'timeout'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"timeout"} 0 0 0
86 0 ▶ $self->{'delay'} = $params->{'delay'} if defined $params->{'delay'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"delay"} 0 0 0
87  
88 0 ▶ $self->{'run_every'} = $params->{'run_every'} if defined $params->{'run_every'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"run_every"} 0 0 0
89 0 ▶ $self->{'end_at'} = $params->{'end_at'} if defined $params->{'end_at'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"end_at"} 0 0 0
90 0 ▶ $self->{'run_times'} = $params->{'run_times'} if defined $params->{'run_times'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"run_times"} 0 0 0
91 0 ▶ $self->{'start_at'} = $params->{'start_at'} if defined $params->{'start_at'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"start_at"} 0 0 0
92  
93 0 ▶ $self->{'id'} = $params->{'id'} if defined $params->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"id"} 0 0 0
94 0 ▶ $self->{'project_id'} = $params->{'project_id'} if defined $params->{'project_id'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"project_id"} 0 0 0
95 0 ▶ $self->{'code_id'} = $params->{'code_id'} if defined $params->{'code_id'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"code_id"} 0 0 0
96 0 ▶ $self->{'status'} = $params->{'status'} if defined $params->{'status'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"status"} 0 0 0
97 0 ▶ $self->{'code_history_id'} = $params->{'code_history_id'} if defined $params->{'code_history_id'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"code_history_id"} 0 0 0
98 0 ▶ $self->{'code_rev'} = $params->{'code_rev'} if defined $params->{'code_rev'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"code_rev"} 0 0 0
99 0 ▶ $self->{'start_time'} = $params->{'start_time'} if defined $params->{'start_time'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"start_time"} 0 0 0
100 0 ▶ $self->{'end_time'} = $params->{'end_time'} if defined $params->{'end_time'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"end_time"} 0 0 0
101 0 ▶ $self->{'duration'} = $params->{'duration'} if defined $params->{'duration'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"duration"} 0 0 0
102 0 ▶ $self->{'updated_at'} = $params->{'updated_at'} if defined $params->{'updated_at'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"updated_at"} 0 0 0
103 0 ▶ $self->{'created_at'} = $params->{'created_at'} if defined $params->{'created_at'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"created_at"} 0 0 0
104 0 ▶ $self->{'name'} = $params->{'name'} if defined $params->{'name'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"name"} 0 0 0
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});
112 0 my $blessed_ref = bless $self, $class;
113 0 lock_keys(%{$self}, @self_keys);
114  
115 0 $log->tracef('Exiting new: %s', $blessed_ref);
116 0 return $blessed_ref;
117 }
118  
119  
120 0 ▶sub code_name { return $_[0]->_access_internal('code_name', $_[1]); }
Subroutine code_name: not called CC=1 SCAR=6.9
Pod documented
121 0 ▶sub payload { return $_[0]->_access_internal('payload', $_[1]); }
Subroutine payload: not called CC=1 SCAR=6.9
Pod documented
122 0 ▶sub priority { return $_[0]->_access_internal('priority', $_[1]); }
Subroutine priority: not called CC=1 SCAR=6.9
Pod documented
123 0 ▶sub timeout { return $_[0]->_access_internal('timeout', $_[1]); }
Subroutine timeout: not called CC=1 SCAR=6.9
Pod documented
124 0 ▶sub delay { return $_[0]->_access_internal('delay', $_[1]); }
Subroutine delay: not called CC=1 SCAR=6.9
Pod documented
125 0 ▶sub name { return $_[0]->_access_internal('name', $_[1]); }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
126 # These are for scheduled task:
127 0 ▶sub run_every { return $_[0]->_access_internal('run_every', $_[1]); }
Subroutine run_every: not called CC=1 SCAR=6.9
Pod documented
128 0 ▶sub end_at { return $_[0]->_access_internal('end_at', $_[1]); }
Subroutine end_at: not called CC=1 SCAR=6.9
Pod documented
129 0 ▶sub run_times { return $_[0]->_access_internal('run_times', $_[1]); }
Subroutine run_times: not called CC=1 SCAR=6.9
Pod documented
130 0 ▶sub start_at { return $_[0]->_access_internal('start_at', $_[1]); }
Subroutine start_at: not called CC=1 SCAR=6.9
Pod documented
131 # Returned when queried a queued task:
132 0 ▶sub id { return $_[0]->_access_internal('id', $_[1]); }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
133 0 ▶sub project_id { return $_[0]->_access_internal('project_id', $_[1]); }
Subroutine project_id: not called CC=1 SCAR=6.9
Pod documented
134 0 ▶sub code_id { return $_[0]->_access_internal('code_id', $_[1]); }
Subroutine code_id: not called CC=1 SCAR=6.9
Pod documented
135 0 ▶sub status { return $_[0]->_access_internal('status', $_[1]); }
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
136 0 ▶sub code_history_id { return $_[0]->_access_internal('code_history_id', $_[1]); }
Subroutine code_history_id: not called CC=1 SCAR=6.9
Pod documented
137 0 ▶sub code_rev { return $_[0]->_access_internal('code_rev', $_[1]); }
Subroutine code_rev: not called CC=1 SCAR=6.9
Pod documented
138 0 ▶sub start_time { return $_[0]->_access_internal('start_time', $_[1]); }
Subroutine start_time: not called CC=1 SCAR=6.9
Pod documented
139 0 ▶sub end_time { return $_[0]->_access_internal('end_time', $_[1]); }
Subroutine end_time: not called CC=1 SCAR=6.9
Pod documented
140 0 ▶sub duration { return $_[0]->_access_internal('duration', $_[1]); }
Subroutine duration: not called CC=1 SCAR=6.9
Pod documented
141 0 ▶sub updated_at { return $_[0]->_access_internal('updated_at', $_[1]); }
Subroutine updated_at: not called CC=1 SCAR=6.9
Pod documented
142 0 ▶sub created_at { return $_[0]->_access_internal('created_at', $_[1]); }
Subroutine created_at: not called CC=1 SCAR=6.9
Pod documented
143  
144 sub _access_internal {
145 0 ▶ my ($self, $var_name, $var_value) = @_;
Subroutine _access_internal: not called CC=2 SCAR=17.9
146 0 $log->tracef('_access_internal(%s, %s)', $var_name, $var_value);
147 0 ▶ if( defined $var_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var_value) { } 0 0 0
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 ▶ my ($self) = @_;
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
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 ▶ my ($self) = @_;
Subroutine cancel: not called CC=1 SCAR=6.9
Pod documented
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 ▶ my $self = shift;
Subroutine set_progress: not called CC=1 SCAR=6.9
Pod documented
196 my %params = validate_with(
197 'params' => \@_,
198 0 ▶ 'normalize_keys' => sub { return lc shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 ▶ my $self = shift;
Subroutine retry: not called CC=1 SCAR=6.9
Pod documented
225 my %params = validate_with(
226 'params' => \@_,
227 0 ▶ 'normalize_keys' => sub { return lc shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 ▶ my ($self) = @_;
Subroutine cancel_scheduled: not called CC=1 SCAR=6.9
Pod documented
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 4 1;
273  
274 __END__
« blib/lib/IO/Iron/IronMQ/Queue.pm ↑ Summary blib/lib/IO/Iron/Connection.pm »