mojolicious / minion

:octopus: Perl high performance job queue

Home Page:https://metacpan.org/release/Minion

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Worker event emitted too soon?

EmilianoBruni opened this issue · comments

In documentation

$minion->on(worker => sub {
  my ($minion, $worker) = @_;
  my $id = $worker->id;
  say "Worker $$:$id started.";
});

But this event is emitted here (Minion::worker)

sub worker {
  my $self = shift;
 
  # No fork emulation support
  croak 'Minion workers do not support fork emulation' if $Config{d_pseudofork};
 
  my $worker = Minion::Worker->new(minion => $self);
  $self->emit(worker => $worker);
  return $worker;
}

so how can I read the worker id if this (and most of the other workers properties) value is available after its registration (Minion::Worker::register)

sub register {
  my $self   = shift;
  my $status = {status => $self->status};
  return $self->id($self->minion->backend->register_worker($self->id, $status));
}

Here an example (it used MongoDB backend but it should be insignificant because there is no db interaction)

use v5.24;
use Minion;
my $mongo_uri = 'mongodb://minion:minion@127.0.0.1:27017/minion';
my $minion = Minion->new(MongoDB => $mongo_uri);
$minion->reset;
$minion->on(worker => sub {
  my ($minion, $worker) = @_;
  my $id = $worker->id;
  say "Worker $$:$id started.";
});
my $worker = $minion->worker;

Result is: "Worker 29609: started."

I suggest to move worker event after registration.

You are correct, the example was wrong.