Files
netdisco/lib/App/Netdisco/Backend/Worker/Common.pm
Oliver Gorwits d74ccac4f6 rename *-daemon apps to be *-backend
Squashed commit of the following:

commit 39b438aa4b
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:40:11 2017 +0100

    add release notes

commit ca4ea90d35
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:32:06 2017 +0100

    update distmeta

commit 4e35b904b0
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:30:22 2017 +0100

    rename files from Daemon to Backend

commit 86a605ba68
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:26:43 2017 +0100

    rename daemon to backend in code

commit ffe8fc180f
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:15:57 2017 +0100

    add daemon files which exec to backend equivalents

commit 53e041594e
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 15:32:49 2017 +0100

    rename netdisco-daemon to netdisco-backend
2017-05-06 16:40:48 +01:00

71 lines
1.8 KiB
Perl
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package App::Netdisco::Backend::Worker::Common;
use Dancer qw/:moose :syntax :script/;
use Try::Tiny;
use App::Netdisco::Util::Backend;
use Role::Tiny;
use namespace::clean;
use Time::HiRes 'sleep';
use App::Netdisco::JobQueue qw/jq_defer jq_complete/;
sub worker_begin { (shift)->{started} = time }
sub worker_body {
my $self = shift;
my $wid = $self->wid;
while (1) {
prctl sprintf 'netdisco-backend: worker #%s poller: idle', $wid;
my $job = $self->{queue}->dequeue(1);
next unless defined $job;
my $action = $job->action;
try {
$job->started(scalar localtime);
prctl sprintf 'netdisco-backend: worker #%s poller: working on #%s: %s',
$wid, $job->job, $job->summary;
info sprintf "pol (%s): starting %s job(%s) at %s",
$wid, $action, $job->job, $job->started;
my ($status, $log) = $self->$action($job);
$job->status($status);
$job->log($log);
}
catch {
$job->status('error');
$job->log("error running job: $_");
$self->sendto('stderr', $job->log ."\n");
};
$self->close_job($job);
sleep( setting('workers')->{'min_runtime'} || 0 );
$self->exit(0); # recycle worker
}
}
sub close_job {
my ($self, $job) = @_;
my $now = scalar localtime;
prctl sprintf 'netdisco-backend: worker #%s poller: wrapping up %s #%s: %s',
$self->wid, $job->action, $job->job, $job->status;
info sprintf "pol (%s): wrapping up %s job(%s) - status %s at %s",
$self->wid, $job->action, $job->job, $job->status, $now;
try {
if ($job->status eq 'defer') {
jq_defer($job);
}
else {
$job->finished($now);
jq_complete($job);
}
}
catch { $self->sendto('stderr', "error closing job: $_\n") };
}
1;