more JobQueue migration

This commit is contained in:
Oliver Gorwits
2014-05-10 23:18:12 +01:00
parent 9569bda4d8
commit 5413e34e83
7 changed files with 64 additions and 25 deletions

View File

@@ -45,5 +45,6 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key("job"); __PACKAGE__->set_primary_key("job");
sub extra { (shift)->subaction } sub extra { (shift)->subaction }
sub entered_stamp { (shift)->entered }
1; 1;

View File

@@ -12,6 +12,8 @@ our @EXPORT_OK = qw/
jq_get jq_get
jq_getlocal jq_getlocal
jq_queued jq_queued
jq_log
jq_userlog
jq_lock jq_lock
jq_defer jq_defer
jq_complete jq_complete
@@ -49,6 +51,17 @@ Netdisco job instance interface (see below).
Returns a list of IP addresses of devices which currently have a job of the Returns a list of IP addresses of devices which currently have a job of the
given C<$job_type> queued (e.g. C<discover>, C<arpnip>, etc). given C<$job_type> queued (e.g. C<discover>, C<arpnip>, etc).
=head2 jq_log()
Returns a list of the most recent 50 jobs in the queue. Jobs are returned as
objects which implement the Netdisco job instance interface (see below).
=head2 jq_userlog( $user )
Returns a list of jobs which have been entered into the queue by the passed
C<$user>. Jobs are returned as objects which implement the Netdisco job
instance interface (see below).
=head2 jq_lock( $job ) =head2 jq_lock( $job )
Marks a job in the queue as booked out to this processing node (denoted by the Marks a job in the queue as booked out to this processing node (denoted by the

View File

@@ -12,6 +12,8 @@ our @EXPORT_OK = qw/
jq_get jq_get
jq_getlocal jq_getlocal
jq_queued jq_queued
jq_log
jq_userlog
jq_lock jq_lock
jq_defer jq_defer
jq_complete jq_complete
@@ -34,7 +36,6 @@ sub jq_get {
push @returned, schema('daemon')->resultset('Admin') push @returned, schema('daemon')->resultset('Admin')
->new_result({ $job->get_columns, type => $job_type }); ->new_result({ $job->get_columns, type => $job_type });
} }
return @returned; return @returned;
} }
@@ -50,7 +51,6 @@ sub jq_getlocal {
push @returned, schema('daemon')->resultset('Admin') push @returned, schema('daemon')->resultset('Admin')
->new_result({ $job->get_columns, type => $job_type }); ->new_result({ $job->get_columns, type => $job_type });
} }
return @returned; return @returned;
} }
@@ -64,6 +64,39 @@ sub jq_queued {
})->get_column('device')->all; })->get_column('device')->all;
} }
sub jq_log {
my @returned = ();
my $rs = schema('netdisco')->resultset('Admin')->search({}, {
order_by => { -desc => [qw/entered device action/] },
rows => 50,
});
while (my $job = $rs->next) {
my $job_type = setting('job_types')->{$job->action} or next;
push @returned, schema('daemon')->resultset('Admin')
->new_result({ $job->get_columns, type => $job_type });
}
return @returned;
}
sub jq_userlog {
my $user = shift;
my @returned = ();
my $rs = schema('netdisco')->resultset('Admin')->search({
username => $user,
finished => { '>' => \"(now() - interval '5 seconds')" },
});
while (my $job = $rs->next) {
my $job_type = setting('job_types')->{$job->action} or next;
push @returned, schema('daemon')->resultset('Admin')
->new_result({ $job->get_columns, type => $job_type });
}
return @returned;
}
sub jq_lock { sub jq_lock {
my $job = shift; my $job = shift;
my $fqdn = hostfqdn || 'localhost'; my $fqdn = hostfqdn || 'localhost';

View File

@@ -6,6 +6,7 @@ use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
use App::Netdisco::JobQueue 'jq_log';
register_admin_task({ register_admin_task({
tag => 'jobqueue', tag => 'jobqueue',
@@ -28,16 +29,9 @@ ajax '/ajax/control/admin/jobqueue/delall' => require_role admin => sub {
}; };
ajax '/ajax/content/admin/jobqueue' => require_role admin => sub { ajax '/ajax/content/admin/jobqueue' => require_role admin => sub {
my $set = schema('netdisco')->resultset('Admin')
->with_times
->search({}, {
order_by => { -desc => [qw/entered device action/] },
rows => 50,
});
content_type('text/html'); content_type('text/html');
template 'ajax/admintask/jobqueue.tt', { template 'ajax/admintask/jobqueue.tt', {
results => $set, results => [ jq_log ],
}, { layout => undef }; }, { layout => undef };
}; };

View File

@@ -5,7 +5,7 @@ use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::JobQueue 'jq_insert'; use App::Netdisco::JobQueue qw/jq_insert jq_userlog/;
ajax '/ajax/portcontrol' => require_role port_control => sub { ajax '/ajax/portcontrol' => require_role port_control => sub {
send_error('No device/port/field', 400) send_error('No device/port/field', 400)
@@ -62,21 +62,20 @@ ajax '/ajax/portcontrol' => require_role port_control => sub {
}; };
ajax '/ajax/userlog' => require_login sub { ajax '/ajax/userlog' => require_login sub {
my $rs = schema('netdisco')->resultset('Admin')->search({ my @jobs = jq_userlog( session('logged_in_user') );
username => session('logged_in_user'),
action => [qw/location contact portcontrol portname vlan power
discover macsuck arpnip/],
finished => { '>' => \"(now() - interval '5 seconds')" },
});
my %status = ( my %status = (
'done' => [ 'done' => [
map {s/\[\]/&lt;empty&gt;/; $_} map {s/\[\]/&lt;empty&gt;/; $_}
$rs->search({status => 'done'})->get_column('log')->all map { $_->log }
grep { $_->status eq 'done' }
@jobs
], ],
'error' => [ 'error' => [
map {s/\[\]/&lt;empty&gt;/; $_} map {s/\[\]/&lt;empty&gt;/; $_}
$rs->search({status => 'error'})->get_column('log')->all map { $_->log }
grep { $_->status eq 'error' }
@jobs
], ],
); );

View File

@@ -1 +0,0 @@
../../../share

View File

@@ -1,4 +1,4 @@
[% IF NOT results.has_rows %] [% IF NOT results.size %]
<div class="span2 alert alert-info">The job queue is empty.</div> <div class="span2 alert alert-info">The job queue is empty.</div>
[% ELSE %] [% ELSE %]
<table class="table table-bordered table-condensed table-hover nd_floatinghead"> <table class="table table-bordered table-condensed table-hover nd_floatinghead">
@@ -17,7 +17,7 @@
</tr> </tr>
</thead> </thead>
</tbody> </tbody>
[% WHILE (row = results.next) %] [% FOREACH row IN results %]
<tr <tr
[% ' class="nd_jobqueueitem success"' IF row.status == 'done' %] [% ' class="nd_jobqueueitem success"' IF row.status == 'done' %]
[% ' class="nd_jobqueueitem error"' IF row.status == 'error' %] [% ' class="nd_jobqueueitem error"' IF row.status == 'error' %]