Last active
March 11, 2021 06:43
-
-
Save worldmind/21eeaecc8b1a0c22388424afed1786ff to your computer and use it in GitHub Desktop.
Minion example
This file contains hidden or 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
#!/usr/bin/perl -w | |
use Mojolicious::Lite; | |
use Minion; | |
# run app: morbo minion-example.pl | |
# run worker: perl minion-example.pl minion worker | |
# enqueue job: curl 'http://127.0.0.1:3000/do?msg=hello' | |
# show job status: perl minion-example.pl minion job 1 | |
plugin Minion => { SQLite => 'sqlite:test.db' }; | |
get '/do' => sub { | |
my $c = shift; | |
my $msg = $c->param('msg') // 'HELLO'; | |
my $id = app->minion->enqueue(do_things => [$msg]); | |
$c->render(text => "Task id:$id is enqueued$/"); | |
}; | |
app->minion->add_task(do_things => sub { | |
my ($job, $msg) = @_; | |
sleep 3; | |
say $msg; | |
$job->on(finished => sub { | |
my ($job, $result) = @_; | |
my $id = $job->id; | |
say "Job id: $id is finished. Result: $result"; | |
}); | |
$job->finish($msg); | |
}); | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment