Squashed commit of the following: commitcb6f125c73Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 20:26:59 2013 +0100 discover root_ip properly commit8228e73f5bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 19:47:23 2013 +0100 better name for util package commit4546036f4fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 19:23:55 2013 +0100 bug fixes in getting wireless info commit78554e5516Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 19:07:44 2013 +0100 refactor snmp_connect to handle versions and device classes commitca9edd114aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 15:23:52 2013 +0100 rename discoverall to discovernew commit1b897e4aeeAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 14:51:06 2013 +0100 change debug log tag for store_device commit8a5306e056Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 14:50:10 2013 +0100 rename Discover.pm to Device.pm commit3197e38819Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Apr 13 14:48:31 2013 +0100 allow netdisco-do to do all acton
93 lines
2.4 KiB
Perl
93 lines
2.4 KiB
Perl
package App::Netdisco::Daemon::Worker::Scheduler;
|
|
|
|
use Dancer qw/:moose :syntax :script/;
|
|
use Dancer::Plugin::DBIC 'schema';
|
|
|
|
use Algorithm::Cron;
|
|
use Try::Tiny;
|
|
|
|
use Role::Tiny;
|
|
use namespace::clean;
|
|
|
|
my $jobactions = {
|
|
map {$_ => undef} qw/
|
|
refresh
|
|
discovernew
|
|
/
|
|
# saveconfigs
|
|
# macwalk
|
|
# arpwalk
|
|
# nbtwalk
|
|
# backup
|
|
};
|
|
|
|
sub worker_begin {
|
|
my $self = shift;
|
|
my $wid = $self->wid;
|
|
debug "entering Scheduler ($wid) worker_begin()";
|
|
|
|
foreach my $a (keys %$jobactions) {
|
|
next unless setting('housekeeping')
|
|
and exists setting('housekeeping')->{$a};
|
|
my $config = setting('housekeeping')->{$a};
|
|
|
|
# accept either single crontab format, or individual time fields
|
|
my $cron = Algorithm::Cron->new(
|
|
base => 'local',
|
|
%{
|
|
(ref {} eq ref $config->{when})
|
|
? $config->{when}
|
|
: {crontab => $config->{when}}
|
|
}
|
|
);
|
|
|
|
$jobactions->{$a} = $config;
|
|
$jobactions->{$a}->{when} = $cron;
|
|
}
|
|
}
|
|
|
|
sub worker_body {
|
|
my $self = shift;
|
|
my $wid = $self->wid;
|
|
|
|
while (1) {
|
|
# sleep until some point in the next minute
|
|
my $naptime = 60 - (time % 60) + int(rand(45));
|
|
debug "sched ($wid): sleeping for $naptime seconds";
|
|
sleep $naptime;
|
|
|
|
# NB next_time() returns the next *after* win_start
|
|
my $win_start = time - (time % 60) - 1;
|
|
my $win_end = $win_start + 60;
|
|
|
|
# if any job is due, add it to the queue
|
|
foreach my $a (keys %$jobactions) {
|
|
next unless defined $jobactions->{$a};
|
|
my $sched = $jobactions->{$a};
|
|
|
|
# next occurence of job must be in this minute's window
|
|
debug sprintf "sched ($wid): $a: win_start: %s, win_end: %s, next: %s",
|
|
$win_start, $win_end, $sched->{when}->next_time($win_start);
|
|
next unless $sched->{when}->next_time($win_start) <= $win_end;
|
|
|
|
# queue it!
|
|
# due to a table constraint, this will (intentionally) fail if a
|
|
# similar job is already queued.
|
|
try {
|
|
debug "sched ($wid): queueing $a job";
|
|
schema('netdisco')->resultset('Admin')->create({
|
|
action => $a,
|
|
device => ($sched->{device} || undef),
|
|
subaction => ($sched->{extra} || undef),
|
|
status => 'queued',
|
|
});
|
|
}
|
|
catch {
|
|
debug "sched ($wid): action $a was not queued (dupe?)";
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
1;
|