diff --git a/Netdisco/lib/App/Netdisco/Core/Discover.pm b/Netdisco/lib/App/Netdisco/Core/Discover.pm index 1b8fa01e..364b8ca9 100644 --- a/Netdisco/lib/App/Netdisco/Core/Discover.pm +++ b/Netdisco/lib/App/Netdisco/Core/Discover.pm @@ -5,6 +5,7 @@ use Dancer::Plugin::DBIC 'schema'; use App::Netdisco::Util::Device qw/get_device is_discoverable/; use App::Netdisco::Util::DNS ':all'; +use App::Netdisco::JobQueue qw/jq_queued jq_insert/; use NetAddr::IP::Lite ':lower'; use List::MoreUtils (); use Encode; @@ -900,20 +901,12 @@ sub discover_new_neighbors { next; } - # Don't queued if job already exists - my $is_queued = schema('netdisco')->resultset('Admin')->search( - { device => $ip, + # Don't queue if job already exists + if (List::MoreUtils::none {$_ eq $ip} jq_queued('discover')) { + jq_insert({ + device => $ip, action => 'discover', - status => { -like => 'queued%' }, - } - )->single; - unless ($is_queued) { - schema('netdisco')->resultset('Admin')->create( - { device => $ip, - action => 'discover', - status => 'queued', - } - ); + }); } } } diff --git a/Netdisco/lib/App/Netdisco/Daemon/JobQueue.pm b/Netdisco/lib/App/Netdisco/Daemon/JobQueue.pm index 84ea889f..db48aa05 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/JobQueue.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/JobQueue.pm @@ -17,97 +17,4 @@ sub jq_defer { shift and JobQueue::jq_defer(@_) } sub jq_complete { shift and JobQueue::jq_complete(@_) } sub jq_insert { shift and JobQueue::jq_insert(@_) } -=head1 NAME - -App::Netdisco::Daemon::JobQueue - -=head1 DESCRIPTION - -Interface for Netdisco job queue. - -There are no default exports, however the C<:all> tag will export all -subroutines. - -=head1 EXPORT_OK - -=head2 jq_get( $num? ) - -Returns a list of randomly selected queued jobs. Default is to return one job, -unless C<$num> is provided. Jobs are returned as objects which implement the -Netdisco job instance interface (see below). - -=head2 jq_getlocal() - -Returns the list of jobs currently booked out to this processing node (denoted -by the local hostname). Jobs are returned as objects which implement the -Netdisco job instance interface (see below). - -=head2 jq_queued( $job_type ) - -Returns a list of IP addresses of devices which currently have a job of the -given C<$job_type> queued (e.g. C, C, etc). - -=head2 jq_lock( $job ) - -Marks a job in the queue as booked out to this processing node (denoted by the -local hostname). The C<$job> parameter must be an object which implements the -Netdisco job instance interface (see below). - -Returns true if successful else returns false. - -=head2 jq_defer( $job ) - -Marks a job in the queue as available for taking. This is usually done after a -job is booked but the processing node changes its mind and decides to return -the job to the queue. The C<$job> parameter must be an object which implements -the Netdisco job instance interface (see below). - -Returns true if successful else returns false. - -=head2 jq_complete( $job ) - -Marks a job as complete. The C<$job> parameter must be an object which -implements the Netdisco job instance interface (see below). The queue item's -status, log and finished fields will be updated from the passed C<$job>. - -Returns true if successful else returns false. - -=head2 jq_insert( \%job | [ %job, \%job ...] ) - -Adds the passed jobs to the queue. - -=head1 Job Instance Interface - -=head2 id (auto) - -=head2 type (required) - -=head2 wid (required, default 0) - -=head2 entered - -=head2 started - -=head2 finished - -=head2 device - -=head2 port - -=head2 action - -=head2 subaction or extra - -=head2 status - -=head2 username - -=head2 userip - -=head2 log - -=head2 debug - -=cut - true; diff --git a/Netdisco/lib/App/Netdisco/JobQueue.pm b/Netdisco/lib/App/Netdisco/JobQueue.pm new file mode 100644 index 00000000..46f28ab6 --- /dev/null +++ b/Netdisco/lib/App/Netdisco/JobQueue.pm @@ -0,0 +1,115 @@ +package App::Netdisco::JobQueue; + +use Dancer qw/:moose :syntax :script/; + +use Module::Load (); +Module::Load::load + 'App::Netdisco::JobQueue::' . setting('job_queue') => ':all'; + +use base 'Exporter'; +our @EXPORT = (); +our @EXPORT_OK = qw/ + jq_get + jq_getlocal + jq_queued + jq_lock + jq_defer + jq_complete + jq_insert +/; +our %EXPORT_TAGS = ( all => \@EXPORT_OK ); + +=head1 NAME + +App::Netdisco::Daemon::JobQueue + +=head1 DESCRIPTION + +Interface for Netdisco job queue. + +There are no default exports, however the C<:all> tag will export all +subroutines. + +=head1 EXPORT_OK + +=head2 jq_get( $num? ) + +Returns a list of randomly selected queued jobs. Default is to return one job, +unless C<$num> is provided. Jobs are returned as objects which implement the +Netdisco job instance interface (see below). + +=head2 jq_getlocal() + +Returns the list of jobs currently booked out to this processing node (denoted +by the local hostname). Jobs are returned as objects which implement the +Netdisco job instance interface (see below). + +=head2 jq_queued( $job_type ) + +Returns a list of IP addresses of devices which currently have a job of the +given C<$job_type> queued (e.g. C, C, etc). + +=head2 jq_lock( $job ) + +Marks a job in the queue as booked out to this processing node (denoted by the +local hostname). The C<$job> parameter must be an object which implements the +Netdisco job instance interface (see below). + +Returns true if successful else returns false. + +=head2 jq_defer( $job ) + +Marks a job in the queue as available for taking. This is usually done after a +job is booked but the processing node changes its mind and decides to return +the job to the queue. The C<$job> parameter must be an object which implements +the Netdisco job instance interface (see below). + +Returns true if successful else returns false. + +=head2 jq_complete( $job ) + +Marks a job as complete. The C<$job> parameter must be an object which +implements the Netdisco job instance interface (see below). The queue item's +status, log and finished fields will be updated from the passed C<$job>. + +Returns true if successful else returns false. + +=head2 jq_insert( \%job | [ %job, \%job ...] ) + +Adds the passed jobs to the queue. + +=head1 Job Instance Interface + +=head2 id (auto) + +=head2 type (required) + +=head2 wid (required, default 0) + +=head2 entered + +=head2 started + +=head2 finished + +=head2 device + +=head2 port + +=head2 action + +=head2 subaction or extra + +=head2 status + +=head2 username + +=head2 userip + +=head2 log + +=head2 debug + +=cut + +true;