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