Per Carlson
2015-01-26 21:36:26 UTC
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
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
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.