Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created June 21, 2016 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/4a25a824142405cbbd59525eb87d62cf to your computer and use it in GitHub Desktop.
Save brianmed/4a25a824142405cbbd59525eb87d62cf to your computer and use it in GitHub Desktop.
Minimal Mojolicious and Minion example
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
@Skeeve
Copy link

Skeeve commented Apr 10, 2019

What should this do?

Each time I add, I get a new job id.

Each time I ask for a jobid I get null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment