Skip to content

Instantly share code, notes, and snippets.

@polettix
Created September 11, 2016 10:04
Show Gist options
  • Save polettix/bb0fe42d3bef036b709ee24eb33009b0 to your computer and use it in GitHub Desktop.
Save polettix/bb0fe42d3bef036b709ee24eb33009b0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Mojolicious::Lite;
use Data::Dumper;
$Data::Dumper::Indent = 1;
my $uri = 'http://mojolicious.org';
plugin Minion => {SQLite => 'sqlite:test4bug.db'};
app->minion->add_task(poke_block => \&poke_block);
app->minion->add_task(poke_noblock => \&poke_noblock);
my $allowed_uas = [ua => [qw< app new >]];
get '/minion/:op/:ua' => $allowed_uas => sub {
my $c = shift;
my $operation = $c->stash('op');
my $uas = $c->stash('ua');
$c->minion->enqueue('poke_' . $operation, [$uri, $uas]);
$c->render(text => "minion $operation/$uas will be performed soon\n");
};
app->start;
sub poke_block {
my ($job, $uri, $uas) = @_;
my $log = $job->app->log;
my $ua = $uas eq 'app' ? $job->app->ua : Mojo::UserAgent->new();
my $tx = $ua->get($uri);
my $outcome = log_outcome($log, $tx, "/minion/block/$uas");
$tx->success ? $job->finish($outcome) : $job->fail($outcome);
} ## end sub poke_block
sub poke_noblock {
my ($job, $uri, $uas) = @_;
my $log = $job->app->log;
my $ua = $uas eq 'app' ? $job->app->ua : Mojo::UserAgent->new();
my $ioloop = $ua->ioloop;
$ioloop->start unless $ioloop->is_running;
$ua->get(
$uri => sub {
my ($ua, $tx) = @_;
my $outcome = log_outcome($log, $tx, "/minion/noblock/$uas");
$tx->success ? $job->finish($outcome) : $job->fail($outcome);
}
);
} ## end sub poke_noblock
sub log_outcome {
my ($log, $tx, $msg) = @_;
$msg = defined($msg) ? $msg . ' ' : '';
my $outcome = $tx->success ? 'SUCCESS!' : 'FAILURE!';
$log->info($msg . $outcome);
$log->debug(Dumper $tx->res) unless $tx->success;
return $outcome;
} ## end sub log_outcome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment