Created
March 29, 2016 03:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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