Created
June 21, 2016 13:06
-
-
Save brianmed/4a25a824142405cbbd59525eb87d62cf to your computer and use it in GitHub Desktop.
Minimal Mojolicious and 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
use Mojolicious::Lite; | |
plugin Minion => {SQLite => 'jobs.sqlite'}; | |
app->secrets(['06650c45-b2db-45ad-9fce-7e64702208ce']); | |
app->minion->add_task(add => sub { | |
my $job = shift; | |
my $number = shift; | |
$job->finish({ result => $number + $number }); | |
}); | |
get '/' => 'index'; | |
any '/api/v1/add/:number' => sub { | |
my $c = shift; | |
my $number = $c->param("number"); | |
my $jobid = $c->minion->enqueue(add => [$number]); | |
return($c->render(json => {success => 1, message => "Adding numbers: $number", jobid => $jobid})); | |
}; | |
any '/api/v1/result/:job_id' => sub { | |
my $c = shift; | |
my $jobid = $c->param("job_id"); | |
unless ($jobid =~ m/^\d+$/) { | |
return($c->render(json => {success => 0, message => "JobID not an unsigned integer"})); | |
} | |
my $job = $c->minion->job($jobid); | |
if ($job) { | |
return($c->render(json => {success => 1, message => $job->info->{result}})); | |
} | |
else { | |
return($c->render(json => {success => 0, message => "Job not: found for $jobid"})); | |
} | |
}; | |
app->start; | |
__DATA__ | |
@@ index.html.ep | |
Try:<br> | |
GET /api/v1/add/:number<br> | |
GET /api/v1/result/:job_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What should this do?
Each time I add, I get a new job id.
Each time I ask for a jobid I get null.