Hi.
I'm thinking of using Minion as a job queue in an existing Mojolicious application. The concept of handing off a job to Minion is straight forward, but how to get the result back?
Here's a small (not working) example application:
#!/usr/bin/env perl
use Mojolicious::Lite;
use Net::Ping;
plugin Minion => { File => "minion.db" };
app->minion->add_task(ping => sub {
my ($job, $host) = @_;
my $p = Net::Ping->new;
$p->port_number(22);
if ($p->ping($host)) {
$job->finish("Host $host is alive");
} else {
$job->fail("Host $host doesn't respond");
}
return;
});
get '/ping/(:host)' => sub {
my $c = shift;
my $jobid = $c->minion->enqueue(
ping => [ $c->param('host') ]
);
$c->render_later;
# Register event handlers to get the result/error
# from Minion
my $job = $c->minion->job($jobid);
$job->on(error => sub {
my ($job, $err) = @_;
$c->render(status => 500, text => $err);
});
$job->on(finish => sub {
my ($job, $result) = @_;
$c->render(text => $result);
});
};
app->start;
The above doesn't work at all. I suspect that's because the worker is running in another process than the app, and doesn't share the same event scopes.
Is there another (working) way to retrieve the result in the same route?
--
Pelle
Research is what I'm doing when I don't know what I'm doing.
- Wernher von Braun