Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created March 29, 2016 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/710ab70cf225fd121363 to your computer and use it in GitHub Desktop.
Save brianmed/710ab70cf225fd121363 to your computer and use it in GitHub Desktop.
Attempt at Minion launcher in a Mojo app. Assumes only one worker and one prefork server.
#!/opt/perl
use Mojolicious::Lite;
plugin Minion => { SQLite => 'sqlite:test.db' };
app->minion->add_task(something_slow => sub {
my ($job, @args) = @_;
say 'This is a background worker process.';
$job->finish;
});
get '/' => sub {
my $c = shift;
$c->minion->enqueue("something_slow");
$c->render(text => 'Hello');
};
app->minion->repair; ### Is this needed?
my $stats = app->minion->stats;
# Minion spawn
$SIG{CHLD} = 'IGNORE';
# Only do a double launch if we are told to prefork
if (0 == $stats->{active_workers} && 0 == $stats->{inactive_workers} && @ARGV && "prefork" eq $ARGV[0]) {
die "Can't fork: $!" unless defined(my $pid = fork);
if (0 == $pid) {
@ARGV=("minion", "worker");
$0 = "gogo_minion.pl minion worker";
### Is QUIT necessary in our train?
local $SIG{QUIT} = local $SIG{INT} = local $SIG{TERM} = sub {
app->minion->worker->unregister;
};
}
}
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment