Skip to content

Instantly share code, notes, and snippets.

@melo
Created April 18, 2013 06:49
Show Gist options
  • Save melo/5410690 to your computer and use it in GitHub Desktop.
Save melo/5410690 to your computer and use it in GitHub Desktop.
A Role for Gearman workers that uses Moose MOP to find out which queues it should listen to... I love Moose...
package E6::Queue::Legacy::WorkerRole;
## Role for all the Gearman Legacy Workers out there: provides run, run_once, and startup logic
use E6::Setup::Role;
with 'E6::Queue::Legacy::HasQueueRole';
has 'is_started' => ( is => 'rw', writer => '_is_started', default => sub {0} );
method startup () {
## startup could be a attr with a laxy builder, but after/before have
## issues with attrs accessors in some situations
return if $self->is_started;
my $meta = $self->meta;
my $queue = $self->queue;
for my $meth_name ($meta->get_method_list) {
next unless $meth_name =~ m/^do_([\w]+)$/a;
## We never return nothing from these workers...
$queue->register_queue($1, sub { $self->$meth_name(@_); return });
}
$self->_is_started(1);
}
has 'should_exit' => ( is => 'rw', default => sub {0} );
method run () {
$self->startup;
$self->run_once until $self->should_exit;
}
method run_once () { $self->startup; $self->queue->work }
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment