Working with spawn and start Minion::Job events

30 views
Skip to first unread message

Brad Robertson

unread,
May 9, 2019, 2:48:42 PM5/9/19
to Mojolicious
Where I work we use both Mojolicious and Minion daily, and I think they
are fantastic. Hats off to the development team!

In our use of Minion we routinely employ the 'failed' and 'finished'
events, but there are times when we might also like to take some action
with a 'start' event, and I'm having trouble getting even the simple
example script below to do anything with $job->on(start...). I suspect
I am either doing something stupid or fundamentally misunderstanding
what the 'start' event type is for and how it is to be used. Can anyone
comment on what I'm trying to do here? There seems to be no problem
with $job->on(finished...).

Thanks!

#!/usr/bin/env perl

use strict;
use warnings;
use Minion;
use Minion::Backend::SQLite;
use feature qw(say);

my $backend = Minion::Backend::SQLite->new('sqlite:test.db');

my $minion = Minion->new(SQLite => 'sqlite:minion.db');

# Add tasks
$minion->add_task(some_task => sub {
my ($job, @args) = @_;

$job->on(start => sub {
my $job = shift;
say 'This is a background worker process.';
});

$job->on(finished => sub {
my ($job, $results) = @_;
say for @$results, "\n";
});

$job->on(failed => sub {
my ($job, $error) = @_;

my $info = $job->info;
say "Failed: $info $error";
});

sleep 1;
$job->finish(\@args);
});

# Enqueue jobs
$minion->enqueue(some_task => [ 'Fry', 'Leela', 'Bender', 'Farnsworth' ]);
$minion->enqueue(some_task => [ 'Good news, everyone!' ] => {priority => 5});
$minion->enqueue(some_task => [ "Leela: I'm an alien, alright? Let's drop the subject.",
'Fry: Cool, an alien. Has your race taken over the planet?',
'Leela: No, I just work here.' ]);
$minion->perform_jobs;

# Start a worker
my $worker = $minion->worker;
$worker->status->{jobs} = 2;
$worker->run;

--
Brad Robertson
<bradrob...@gmail.com>

sri

unread,
May 9, 2019, 2:58:46 PM5/9/19
to Mojolicious
Job events are not meant to be used from the task callback, it's too late. If some of
them work it's purely accidental. I believe you can only use them from other events
at the moment, such as worker dequeue.

--
sebastian

Brad Robertson

unread,
May 9, 2019, 3:15:42 PM5/9/19
to mojol...@googlegroups.com
Ah, okay. Thanks!

--
Brad Robertson
<bradrob...@gmail.com>
Reply all
Reply to author
Forward
0 new messages