« blib/lib/App/Netdisco/Util/SiteLocal.pm
↑ Summary
blib/lib/App/Netdisco/DB/ResultSet/DeviceIp.pm »
| 1 | package App::Netdisco::Backend::Role::Manager; | ||
| 2 | |||
| 3 | use Dancer qw/:moose :syntax :script/; | ||
| 4 | |||
| 5 | use List::Util 'sum'; | ||
| 6 | use Proc::ProcessTable; | ||
| 7 | use App::Netdisco::Util::MCE; | ||
| 8 | |||
| 9 | use App::Netdisco::Backend::Job; | ||
| 10 | use App::Netdisco::Backend::Queues 'queue_for'; | ||
| 11 | use App::Netdisco::JobQueue | ||
| 12 | qw/jq_locked jq_getsome jq_lock jq_warm_thrusters/; | ||
| 13 | use App::Netdisco::Util::ManagedConfig 'load_managed_config'; | ||
| 14 | |||
| 15 | use Role::Tiny; | ||
| 16 | use namespace::clean; | ||
| 17 | |||
| 18 | sub worker_begin { | ||
| 19 | my $self = shift; | ||
| 20 | my $wid = $self->wid; | ||
| 21 | |||
| 22 | return debug "mgr ($wid): no need for manager... skip begin" | ||
| 23 | if setting('workers')->{'no_manager'}; | ||
| 24 | |||
| 25 | debug "entering Manager ($wid) worker_begin()"; | ||
| 26 | |||
| 27 | # job queue initialisation | ||
| 28 | # the expensive parts of this were moved to primeskiplist job | ||
| 29 | jq_warm_thrusters; | ||
| 30 | |||
| 31 | # queue a job to rebuild the device action skip list | ||
| 32 | queue_for($self->{queues}, 'primeskiplist')->{queue}->enqueuep(200, | ||
| 33 | App::Netdisco::Backend::Job->new({ job => 0, action => 'primeskiplist' })); | ||
| 34 | |||
| 35 | # requeue jobs locally | ||
| 36 | debug "mgr ($wid): searching for jobs booked to this processing node"; | ||
| 37 | my @jobs = jq_locked; | ||
| 38 | |||
| 39 | if (scalar @jobs) { | ||
| 40 | info sprintf "mgr (%s): found %s jobs booked to this processing node", | ||
| 41 | $wid, scalar @jobs; | ||
| 42 | queue_for($self->{queues}, $_->action)->{queue}->enqueuep(100, $_) | ||
| 43 | for @jobs; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | # creates a 'signature' for each job so that we can check for duplicates ... | ||
| 48 | # it happens from time to time due to the distributed nature of the job queue | ||
| 49 | # and manager(s) - also kinder to the DB to skip here rather than jq_lock() | ||
| 50 | my $memoize = sub { | ||
| 51 | no warnings 'uninitialized'; | ||
| 52 | my $job = shift; | ||
| 53 | return join chr(28), map {$job->{$_}} | ||
| 54 | (qw/action port subaction/, ($job->{device_key} ? 'device_key' : 'device')); | ||
| 55 | }; | ||
| 56 | |||
| 57 | sub worker_body { | ||
| 58 | my $self = shift; | ||
| 59 | my $wid = $self->wid; | ||
| 60 | |||
| 61 | if (setting('workers')->{'no_manager'}) { | ||
| 62 | prctl sprintf 'nd2: #%s mgr: inactive', $wid; | ||
| 63 | return debug "mgr ($wid): no need for manager... quitting" | ||
| 64 | } | ||
| 65 | |||
| 66 | while (1) { | ||
| 67 | prctl sprintf 'nd2: #%s mgr: gathering', $wid; | ||
| 68 | my %seen_job = (); | ||
| 69 | |||
| 70 | # jq_getsome skiplists devices by discover_only and the other device | ||
| 71 | # ACLs, which can name an ACL Manager host ACL as group:<name>. without | ||
| 72 | # this the group is unknown here and matches nothing, so the manager | ||
| 73 | # would skip devices a poller allows. one counter read per pass; the | ||
| 74 | # rows only when it moved. | ||
| 75 | load_managed_config(); | ||
| 76 | |||
| 77 | # this does have a race condition, but the jobs we're protecting | ||
| 78 | # against are likely to be long running | ||
| 79 | my $t = Proc::ProcessTable->new( 'enable_ttys' => 0 ); | ||
| 80 | |||
| 81 | foreach my $q (@{ $self->{queues} }) { | ||
| 82 | # each queue is topped up from the job queue on its own | ||
| 83 | my $num_slots = $q->{tasks} - $q->{queue}->pending(); | ||
| 84 | debug sprintf "mgr (%s): getting potential jobs for %s workers (%s)", | ||
| 85 | $wid, $num_slots, $q->{name} | ||
| 86 | if not $ENV{ND2_SINGLE_WORKER}; | ||
| 87 | |||
| 88 | JOB: foreach my $job ( jq_getsome($num_slots, | ||
| 89 | actions => $q->{actions}, only => $q->{only}) ) { | ||
| 90 | my $display_name = $job->action .' '. ($job->device || ''); | ||
| 91 | |||
| 92 | if ($seen_job{ $memoize->($job) }++) { | ||
| 93 | debug "mgr ($wid): duplicate queue job detected: $display_name"; | ||
| 94 | next JOB; | ||
| 95 | } | ||
| 96 | |||
| 97 | # 1392 check for any of the same job running already | ||
| 98 | if ($job->device) { | ||
| 99 | my ($action, $device) = ($job->action, $job->device); | ||
| 100 | foreach my $p ( @{$t->table} ) { | ||
| 101 | if ($p->cmndline | ||
| 102 | and $p->cmndline =~ m/nd2: #\d+ poll: #\d+: \Q${action}\E ${device}/) { | ||
| 103 | debug "mgr ($wid): duplicate running job detected: $display_name"; | ||
| 104 | next JOB; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | # mark job as running | ||
| 110 | jq_lock($job) or next JOB; | ||
| 111 | info sprintf "mgr (%s): job %s booked out for this processing node", | ||
| 112 | $wid, $job->id; | ||
| 113 | |||
| 114 | # copy job to local queue | ||
| 115 | $q->{queue}->enqueuep($job->job_priority, $job); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | #if (scalar grep {$_ > 1} values %seen_job) { | ||
| 120 | # debug 'WARNING: saw duplicate jobs after getsome()'; | ||
| 121 | # use DDP; debug p %seen_job; | ||
| 122 | #} | ||
| 123 | |||
| 124 | debug "mgr ($wid): sleeping now..." if not $ENV{ND2_SINGLE_WORKER}; | ||
| 125 | prctl sprintf 'nd2: #%s mgr: idle', $wid; | ||
| 126 | sleep( setting('workers')->{sleep_time} || 1 ); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; |