1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
package Mojolicious::Plugin::Minion::Admin;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::File 'path';
sub register {
my ($self, $app, $config) = @_;
# Config
my $prefix = $config->{route} // $app->routes->any('/minion');
$prefix->to(return_to => $config->{return_to} // '/');
# Static files
my $resources = path(__FILE__)->sibling('resources');
push @{$app->static->paths}, $resources->child('public')->to_string;
# Templates
push @{$app->renderer->paths}, $resources->child('templates')->to_string;
# Routes
$prefix->get('/' => \&_dashboard)->name('minion_dashboard');
$prefix->get('/stats' => \&_stats)->name('minion_stats');
$prefix->get('/history' => \&_history)->name('minion_history');
$prefix->get('/jobs' => \&_list_jobs)->name('minion_jobs');
$prefix->patch('/jobs' => \&_manage_jobs)->name('minion_manage_jobs');
$prefix->get('/locks' => \&_list_locks)->name('minion_locks');
$prefix->delete('/locks' => \&_unlock)->name('minion_unlock');
$prefix->get('/workers' => \&_list_workers)->name('minion_workers');
}
sub _dashboard {
my $c = shift;
my $history = $c->minion->backend->history;
$c->render('minion/dashboard', history => $history);
}
sub _history {
my $c = shift;
$c->render(json => $c->minion->history);
}
sub _list_jobs {
my $c = shift;
my $v = $c->validation;
$v->optional('id');
$v->optional('limit')->num;
$v->optional('offset')->num;
$v->optional('queue');
$v->optional('state')->in(qw(active failed finished inactive));
$v->optional('task');
my $options = {};
$v->is_valid($_) && ($options->{"${_}s"} = $v->every_param($_))
for qw(id queue state task);
my $limit = $v->param('limit') || 10;
my $offset = $v->param('offset') || 0;
my $results = $c->minion->backend->list_jobs($offset, $limit, $options);
$c->render(
'minion/jobs',
jobs => $results->{jobs},
total => $results->{total},
limit => $limit,
offset => $offset
);
}
sub _list_locks {
my $c = shift;
my $v = $c->validation;
$v->optional('limit')->num;
$v->optional('offset')->num;
$v->optional('name');
my $options = {};
$options->{names} = $v->every_param('name') if $v->is_valid('name');
my $limit = $v->param('limit') || 10;
my $offset = $v->param('offset') || 0;
my $results = $c->minion->backend->list_locks($offset, $limit, $options);
$c->render(
'minion/locks',
locks => $results->{locks},
total => $results->{total},
limit => $limit,
offset => $offset
);
}
sub _list_workers {
my $c = shift;
my $v = $c->validation;
$v->optional('id');
$v->optional('limit')->num;
$v->optional('offset')->num;
my $limit = $v->param('limit') || 10;
my $offset = $v->param('offset') || 0;
my $options = {};
$options->{ids} = $v->every_param('id') if $v->is_valid('id');
my $results = $c->minion->backend->list_workers($offset, $limit, $options);
$c->render(
'minion/workers',
workers => $results->{workers},
total => $results->{total},
limit => $limit,
offset => $offset
);
}
sub _manage_jobs {
my $c = shift;
my $v = $c->validation;
$v->required('id');
$v->required('do')
->in('remove', 'retry', 'sig_int', 'sig_usr1', 'sig_usr2', 'stop');
$c->redirect_to('minion_jobs') if $v->has_error;
my $minion = $c->minion;
my $ids = $v->every_param('id');
my $do = $v->param('do');
if ($do eq 'retry') {
my $fail = grep { $minion->job($_)->retry ? () : 1 } @$ids;
if ($fail) { $c->flash(danger => "Couldn't retry all jobs.") }
else { $c->flash(success => 'All selected jobs retried.') }
}
elsif ($do eq 'remove') {
my $fail = grep { $minion->job($_)->remove ? () : 1 } @$ids;
if ($fail) { $c->flash(danger => "Couldn't remove all jobs.") }
else { $c->flash(success => 'All selected jobs removed.') }
}
elsif ($do eq 'stop') {
$minion->broadcast(stop => [$_]) for @$ids;
$c->flash(info => 'Trying to stop all selected jobs.');
}
elsif ($do =~ /^sig_(.+)$/) {
my $signal = uc $1;
$minion->broadcast(kill => [$signal, $_]) for @$ids;
$c->flash(info => "Trying to send $signal signal to all selected jobs.");
}
$c->redirect_to($c->url_for('minion_jobs')->query(id => $ids));
}
sub _stats {
my $c = shift;
$c->render(json => $c->minion->stats);
}
sub _unlock {
my $c = shift;
my $v = $c->validation;
$v->required('name');
$c->redirect_to('minion_locks') if $v->has_error;
my $names = $v->every_param('name');
my $minion = $c->minion;
$minion->unlock($_) for @$names;
$c->flash(success => 'All selected named locks released.');
$c->redirect_to('minion_locks');
}
1;
=encoding utf8
=head1 NAME
Mojolicious::Plugin::Minion::Admin - Admin UI
=head1 SYNOPSIS
# Mojolicious
$self->plugin('Minion::Admin');
# Mojolicious::Lite
plugin 'Minion::Admin';
# Secure access to the admin ui with Basic authentication
my $under = $self->routes->under('/minion' =>sub {
my $c = shift;
return 1 if $c->req->url->to_abs->userinfo eq 'Bender:rocks';
$c->res->headers->www_authenticate('Basic');
$c->render(text => 'Authentication required!', status => 401);
return undef;
});
$self->plugin('Minion::Admin' => {route => $under});
=head1 DESCRIPTION
L<Mojolicious::Plugin::Minion::Admin> is a L<Mojolicious> plugin providing an
admin ui for the L<Minion> job queue.
=head1 OPTIONS
L<Mojolicious::Plugin::Minion::Admin> supports the following options.
=head2 return_to
# Mojolicious::Lite
plugin 'Minion::Admin' => {return_to => 'some_route'};
Name of route or path to return to when leaving the admin ui, defaults to C</>.
=head2 route
# Mojolicious::Lite
plugin 'Minion::Admin' => {route => app->routes->any('/admin')};
L<Mojolicious::Routes::Route> object to attach the admin ui to, defaults to
generating a new one with the prefix C</minion>.
=head1 METHODS
L<Mojolicious::Plugin::Minion::Admin> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 register
$plugin->register(Mojolicious->new);
Register plugin in L<Mojolicious> application.
=head1 SEE ALSO
L<Minion>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
=cut
|