« blib/lib/App/Netdisco/SSHCollector/Platform/Clavister.pm
↑ Summary
blib/lib/App/Netdisco/Web/Theme.pm »
| 1 | package App::Netdisco::Worker::Status; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Dancer qw/:moose :syntax !error !info/; | ||
| 7 | |||
| 8 | use Moo; | ||
| 9 | use namespace::clean; | ||
| 10 | |||
| 11 | has 'status' => ( | ||
| 12 | is => 'rw', | ||
| 13 | default => undef, | ||
| 14 | ); | ||
| 15 | |||
| 16 | has [qw/log phase/] => ( | ||
| 17 | is => 'rw', | ||
| 18 | default => '', | ||
| 19 | ); | ||
| 20 | |||
| 21 | =head1 INTRODUCTION | ||
| 22 | |||
| 23 | The status can be: | ||
| 24 | |||
| 25 | =over 4 | ||
| 26 | |||
| 27 | =item * C<done> | ||
| 28 | |||
| 29 | Indicates a state of success and a log message which may be used as the | ||
| 30 | outcome for the action. | ||
| 31 | |||
| 32 | =item * C<info> | ||
| 33 | |||
| 34 | The worker has completed successfully and a debug log will be issued, but the | ||
| 35 | outcome is not the main goal of the action. | ||
| 36 | |||
| 37 | =item * C<defer> | ||
| 38 | |||
| 39 | Issued when the worker has failed to connect to the remote device, or is not | ||
| 40 | permitted to connect (through user config). | ||
| 41 | |||
| 42 | =item * C<error> | ||
| 43 | |||
| 44 | Something went wrong which should not normally be the case. | ||
| 45 | |||
| 46 | =item * C<()> | ||
| 47 | |||
| 48 | This is not really a status. The worker can return any value not an instance | ||
| 49 | of this class to indicate a "pass", or non-error conclusion. | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =head1 METHODS | ||
| 54 | |||
| 55 | =head2 done, info, defer, error | ||
| 56 | |||
| 57 | Shorthand for new() with setting param, accepts log as arg. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub _make_new { | ||
| 62 | my ($self, $status, $log) = @_; | ||
| 63 | die unless $status; | ||
| 64 | my $new = (ref $self ? $self : $self->new()); | ||
| 65 | $new->log($log); | ||
| 66 | $new->status($status); | ||
| 67 | return $new; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub done { shift->_make_new('done', @_) } # <- jq_complete | ||
| 71 | sub info { shift->_make_new('info', @_) } # <- jq_complete | ||
| 72 | sub defer { shift->_make_new('defer', @_) } # <- jq_defer | ||
| 73 | sub error { shift->_make_new('error', @_) } # <- jq_complete | ||
| 74 | |||
| 75 | =head2 is_ok | ||
| 76 | |||
| 77 | Returns true if status is C<done>. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub is_ok { return $_[0]->status eq 'done' } | ||
| 82 | |||
| 83 | =head2 not_ok | ||
| 84 | |||
| 85 | Returns true if status is C<error>, C<defer>, or C<info>. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub not_ok { return (not $_[0]->is_ok) } | ||
| 90 | |||
| 91 | =head2 level | ||
| 92 | |||
| 93 | A numeric constant for the status, to allow comparison. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub level { | ||
| 98 | my $self = shift; | ||
| 99 | return (($self->status eq 'error') ? 4 | ||
| 100 | : ($self->status eq 'done') ? 3 | ||
| 101 | : ($self->status eq 'defer') ? 2 | ||
| 102 | : ($self->status eq 'info') ? 1 : 0); | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; |