blib/lib/App/Netdisco/Backend/Job.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 223 CC 14 SCAR 22.3
CC
14
Cov
0%
apply_config_overrides
20.0
finalise_status
20.0
best_status
12.0
CRAP 9.3
1 package App::Netdisco::Backend::Job;
2  
3 use Dancer qw/:moose :syntax !error !params/;
4 use aliased 'App::Netdisco::Worker::Status';
5  
6 use App::Netdisco::Util::Configuration
7 qw/parse_params_to_config split_params_to_config/;
8 use App::Netdisco::Util::ManagedConfig 'load_managed_config';
9 use Hash::Util::FieldHash 'fieldhash';
10  
11 use Moo;
12 use Term::ANSIColor qw(:constants :constants256);
13 use namespace::clean;
14  
15 foreach my $slot (qw/
16 job
17 entered
18 started
19 finished
20 device
21 port
22 action
23 only_namespace
24 subaction
25 status
26 username
27 userip
28 log
29 device_key
30 backend
31 job_priority
32 is_cancelled
33 is_offline
34  
35 _current_phase
36 /) {
37  
38 has $slot => (
39 is => 'rw',
40 );
41 }
42  
43 has '_statuslist' => (
44 is => 'rw',
45 default => sub { [] },
46 );
47  
48 around BUILDARGS => sub {
49 my ( $orig, $class, @arguments ) = @_;
50 my $args = $arguments[0];
51 die "strange arguments to Job->new()" unless ref {} eq ref $args;
52  
53 if ($args->{action} and $args->{action} =~ m/^(\w+)::(\w+)$/i) {
54 $args->{action} = $1;
55 $args->{only_namespace} = $2;
56 }
57  
58 $args->{subaction} = q{}
59 if ! defined $args->{subaction};
60  
61 $arguments[0] = $args;
62 return $class->$orig(@arguments);
63 };
64  
65 =head1 METHODS
66  
67 =head2 apply_config_overrides
68  
69 Applies any configuration overrides carried in C<port> or C<subaction> to
70 this process's configuration, leaving the residual value in their place.
71 Only the first call on a job does anything.
72  
73 =cut
74  
75 # kept off the object: py_worklet hands every job key to Python's
76 # JobManager, which rejects keys it does not know
77 fieldhash my %overrides_applied;
78  
79 # Not done in BUILDARGS: a queued job is built in the backend manager and run
80 # in a poller, a separate process, so overrides applied while building it
81 # changed only the manager's configuration, and never reached the poller.
82 sub apply_config_overrides {
83 my $job = shift;
84  
85 # a residual value can itself parse as an override
86 return if $overrides_applied{$job}++;
87  
88 # The Poller calls this before Runner::run, to keep overrides out of the
89 # process title, and Runner::run's own call is then a no-op. Loading
90 # anywhere later than here would overwrite the overrides applied below.
91 load_managed_config();
92  
93 foreach my $field (_override_fields($job->action)) {
94 $job->$field( parse_params_to_config($job->$field) )
95 if defined $job->$field;
96 }
97  
98 $job->subaction(q{})
99 if ! defined $job->subaction;
100 }
101  
102 =head2 carries_config_overrides( \%spec )
103  
104 Class method. True when a job queued from C<%spec> would apply configuration
105 overrides, as C<apply_config_overrides> reads them. Takes the same keys as
106 C<jq_insert>, where C<extra> is an alias for C<subaction>.
107  
108 =cut
109  
110 sub carries_config_overrides {
111 my ($class, $spec) = @_;
112 my %value = (port => $spec->{port},
113 subaction => ($spec->{extra} || $spec->{subaction}));
114  
115 return scalar grep { (split_params_to_config($value{$_}))[1] }
116 _override_fields($spec->{action});
117 }
118  
119 # hook and custom field jobs keep their subaction as data, and so does the
120 # scheduler job, whose subaction is the schedule entry it walks; its extra
121 # overrides apply to the jobs it queues, not to itself
122 sub _override_fields {
123 my $action = shift;
124 return ('port',
125 (($action and $action !~ m/^(?:hook|cf_|scheduler$)/) ? 'subaction' : ()));
126 }
127  
128 =head2 display_name
129  
130 An attempt to make a meaningful written statement about the job.
131  
132 =cut
133  
134 sub display_name {
135 my $job = shift;
136 return join ' ',
137 $job->action,
138 ($job->device || ''),
139 ($job->port || '');
140 }
141  
142 =head2 cancel
143  
144 Log a status and prevent other stages from running.
145  
146 =cut
147  
148 sub cancel {
149 my ($job, $msg) = @_;
150 $msg ||= 'unknown reason for cancelled job';
151 $job->is_cancelled(true);
152 return Status->error($msg);
153 }
154  
155 =head2 best_status
156  
157 Find the best status so far. The process is to track back from the last worker
158 and find the highest scoring status, skipping the check phase.
159  
160 =cut
161  
162 sub best_status {
163 my $job = shift;
164 my $cur_level = 0;
165 my $cur_status = '';
166  
167 foreach my $status (reverse @{ $job->_statuslist }) {
168 next if $status->phase
169 and $status->phase !~ m/^(?:early|main|store|late)$/;
170  
171 if ($status->level >= $cur_level) {
172 $cur_level = $status->level;
173 $cur_status = $status->status;
174 }
175 }
176  
177 return $cur_status;
178 }
179  
180 =head2 finalise_status
181  
182 Find the best status and log it into the job's C<status> and C<log> slots.
183  
184 =cut
185  
186 sub finalise_status {
187 my $job = shift;
188 # use DDP; p $job->_statuslist;
189  
190 # fallback
191 $job->status('error');
192 $job->log('failed to report from any worker!');
193  
194 my $max_level = 0;
195  
196 foreach my $status (reverse @{ $job->_statuslist }) {
197 next if $status->phase
198 and $status->phase !~ m/^(?:check|early|main|user|store|late)$/;
199  
200 # done() from check phase should not be the action's done()
201 next if $status->phase eq 'check' and $status->is_ok;
202  
203 # for done() we want the latest log message
204 # for error() (and others) we want the earliest log message
205  
206 if (($max_level != Status->done()->level and $status->level >= $max_level)
207 or ($status->level > $max_level)) {
208  
209 $job->status( $status->status );
210 $job->log( $status->log );
211 $max_level = $status->level;
212 }
213 }
214 }
215  
216 =head2 check_passed
217  
218 Returns true if at least one worker during the C<check> phase flagged status
219 C<done>.
220  
221 =cut
222  
223 sub check_passed {
224 my $job = shift;
225 return true if 0 == scalar @{ $job->_statuslist };
226  
227 foreach my $status (@{ $job->_statuslist }) {
228 return true if
229 (($status->phase eq 'check') and $status->is_ok);
230 }
231 return false;
232 }
233  
234 =head2 enter_phase( $phase )
235  
236 Pass the name of the phase being entered.
237  
238 =cut
239  
240 sub enter_phase {
241 my ($job, $phase) = @_;
242  
243 $job->_current_phase( $phase );
244 debug BRIGHT_CYAN, "//// ", uc($phase), ' \\\\\\\\ ', GREY10, 'phase', RESET;
245 }
246  
247 =head2 add_status
248  
249 Passed an L<App::Netdisco::Worker::Status> will add it to this job's internal
250 status cache. Phase slot of the Status will be set to the current phase.
251  
252 =cut
253  
254 sub add_status {
255 my ($job, $status) = @_;
256 return unless ref $status eq 'App::Netdisco::Worker::Status';
257 $status->phase( $job->_current_phase || '' );
258 push @{ $job->_statuslist }, $status;
259 if ($status->log) {
260 debug GREEN, "\N{LEFTWARDS BLACK ARROW} ", BRIGHT_GREEN, '(', $status->status, ') ', GREEN, $status->log, RESET;
261 }
262 }
263  
264 =head1 ADDITIONAL COLUMNS
265  
266 Columns which exist in this class but are not in
267 L<App::Netdisco::DB::Result::Admin> class.
268  
269 =head2 id
270  
271 Alias for the C<job> column.
272  
273 =cut
274  
275 sub id { (shift)->job }
276  
277 =head2 extra
278  
279 Alias for the C<subaction> column.
280  
281 =cut
282  
283 sub extra { (shift)->subaction }
284  
285 =head2 only_namespace
286  
287 Action command from the user can be an action name or the action name plus one
288 child namespace in the form: "C<action::child>". This slot stores the C<child>
289 component of the command so that C<action> is backwards compatible with
290 Netdisco.
291  
292 =head2 job_priority
293  
294 When selecting jobs from the database, some types of job are higher priority -
295 usually those submitted in the web interface by a user, and those making
296 changes (writing to) the device. This slot stores a number which is the
297 priority of the job and is used by L<MCE> when managing its job queue.
298  
299 =cut
300  
301 true;