« blib/lib/App/Netdisco/Util/PostgresFloor.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/OS10.pm »
| 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 | ||
| 120 | sub _override_fields { | ||
| 121 | my $action = shift; | ||
| 122 | return ('port', | ||
| 123 | (($action and $action !~ m/^(?:hook|cf_)/) ? 'subaction' : ())); | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 display_name | ||
| 127 | |||
| 128 | An attempt to make a meaningful written statement about the job. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub display_name { | ||
| 133 | my $job = shift; | ||
| 134 | return join ' ', | ||
| 135 | $job->action, | ||
| 136 | ($job->device || ''), | ||
| 137 | ($job->port || ''); | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 cancel | ||
| 141 | |||
| 142 | Log a status and prevent other stages from running. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub cancel { | ||
| 147 | my ($job, $msg) = @_; | ||
| 148 | $msg ||= 'unknown reason for cancelled job'; | ||
| 149 | $job->is_cancelled(true); | ||
| 150 | return Status->error($msg); | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 best_status | ||
| 154 | |||
| 155 | Find the best status so far. The process is to track back from the last worker | ||
| 156 | and find the highest scoring status, skipping the check phase. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub best_status { | ||
| 161 | my $job = shift; | ||
| 162 | my $cur_level = 0; | ||
| 163 | my $cur_status = ''; | ||
| 164 | |||
| 165 | foreach my $status (reverse @{ $job->_statuslist }) { | ||
| 166 | next if $status->phase | ||
| 167 | and $status->phase !~ m/^(?:early|main|store|late)$/; | ||
| 168 | |||
| 169 | if ($status->level >= $cur_level) { | ||
| 170 | $cur_level = $status->level; | ||
| 171 | $cur_status = $status->status; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | return $cur_status; | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 finalise_status | ||
| 179 | |||
| 180 | Find the best status and log it into the job's C<status> and C<log> slots. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub finalise_status { | ||
| 185 | my $job = shift; | ||
| 186 | # use DDP; p $job->_statuslist; | ||
| 187 | |||
| 188 | # fallback | ||
| 189 | $job->status('error'); | ||
| 190 | $job->log('failed to report from any worker!'); | ||
| 191 | |||
| 192 | my $max_level = 0; | ||
| 193 | |||
| 194 | foreach my $status (reverse @{ $job->_statuslist }) { | ||
| 195 | next if $status->phase | ||
| 196 | and $status->phase !~ m/^(?:check|early|main|user|store|late)$/; | ||
| 197 | |||
| 198 | # done() from check phase should not be the action's done() | ||
| 199 | next if $status->phase eq 'check' and $status->is_ok; | ||
| 200 | |||
| 201 | # for done() we want the latest log message | ||
| 202 | # for error() (and others) we want the earliest log message | ||
| 203 | |||
| 204 | if (($max_level != Status->done()->level and $status->level >= $max_level) | ||
| 205 | or ($status->level > $max_level)) { | ||
| 206 | |||
| 207 | $job->status( $status->status ); | ||
| 208 | $job->log( $status->log ); | ||
| 209 | $max_level = $status->level; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | =head2 check_passed | ||
| 215 | |||
| 216 | Returns true if at least one worker during the C<check> phase flagged status | ||
| 217 | C<done>. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub check_passed { | ||
| 222 | my $job = shift; | ||
| 223 | return true if 0 == scalar @{ $job->_statuslist }; | ||
| 224 | |||
| 225 | foreach my $status (@{ $job->_statuslist }) { | ||
| 226 | return true if | ||
| 227 | (($status->phase eq 'check') and $status->is_ok); | ||
| 228 | } | ||
| 229 | return false; | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 enter_phase( $phase ) | ||
| 233 | |||
| 234 | Pass the name of the phase being entered. | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | sub enter_phase { | ||
| 239 | my ($job, $phase) = @_; | ||
| 240 | |||
| 241 | $job->_current_phase( $phase ); | ||
| 242 | debug BRIGHT_CYAN, "//// ", uc($phase), ' \\\\\\\\ ', GREY10, 'phase', RESET; | ||
| 243 | } | ||
| 244 | |||
| 245 | =head2 add_status | ||
| 246 | |||
| 247 | Passed an L<App::Netdisco::Worker::Status> will add it to this job's internal | ||
| 248 | status cache. Phase slot of the Status will be set to the current phase. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | sub add_status { | ||
| 253 | my ($job, $status) = @_; | ||
| 254 | return unless ref $status eq 'App::Netdisco::Worker::Status'; | ||
| 255 | $status->phase( $job->_current_phase || '' ); | ||
| 256 | push @{ $job->_statuslist }, $status; | ||
| 257 | if ($status->log) { | ||
| 258 | debug GREEN, "\N{LEFTWARDS BLACK ARROW} ", BRIGHT_GREEN, '(', $status->status, ') ', GREEN, $status->log, RESET; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | =head1 ADDITIONAL COLUMNS | ||
| 263 | |||
| 264 | Columns which exist in this class but are not in | ||
| 265 | L<App::Netdisco::DB::Result::Admin> class. | ||
| 266 | |||
| 267 | =head2 id | ||
| 268 | |||
| 269 | Alias for the C<job> column. | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub id { (shift)->job } | ||
| 274 | |||
| 275 | =head2 extra | ||
| 276 | |||
| 277 | Alias for the C<subaction> column. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub extra { (shift)->subaction } | ||
| 282 | |||
| 283 | =head2 only_namespace | ||
| 284 | |||
| 285 | Action command from the user can be an action name or the action name plus one | ||
| 286 | child namespace in the form: "C<action::child>". This slot stores the C<child> | ||
| 287 | component of the command so that C<action> is backwards compatible with | ||
| 288 | Netdisco. | ||
| 289 | |||
| 290 | =head2 job_priority | ||
| 291 | |||
| 292 | When selecting jobs from the database, some types of job are higher priority - | ||
| 293 | usually those submitted in the web interface by a user, and those making | ||
| 294 | changes (writing to) the device. This slot stores a number which is the | ||
| 295 | priority of the job and is used by L<MCE> when managing its job queue. | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | true; |