| 1 | package App::Sque::Command::work; | ||
| 2 | $App::Sque::Command::work::VERSION = '0.010'; | ||
| 3 | use App::Sque -command; | ||
| 4 | use Parallel::ForkManager; | ||
| 5 | use Sque; | ||
| 6 | |||
| 7 | # ABSTRACT: Worker command for sque command-line tool | ||
| 8 | |||
| 9 | sub usage_desc { "Start sque worker(s)" } | ||
| 10 | |||
| 11 | sub opt_spec { | ||
| 12 | return ( | ||
| 13 | [ "host|h=s", "Set the stomp host" ], | ||
| 14 | [ "lib|l=s@", "Add a lib directory if needed to find worker classes" ], | ||
| 15 | [ "port|p=i", "Set the stomp port" ], | ||
| 16 | [ "queues|q=s", "Comma-separted list of queues to listen to" ], | ||
| 17 | [ "verbose|v", "Be verbose?" ], | ||
| 18 | [ "workers|w=i", "Number of workers to start", { default => 1 } ], | ||
| 19 | ); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub validate_args { | ||
| 23 | my ($self, $opt, $args) = @_; | ||
| 24 | |||
| 25 | # We must have the host/part | ||
| 26 | $self->usage_error("host required") unless $opt->{host}; | ||
| 27 | $self->usage_error("port required") unless $opt->{port}; | ||
| 28 | |||
| 29 | # We must have queues | ||
| 30 | $self->usage_error("queues required") unless $opt->{queues}; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub execute { | ||
| 34 | my ($self, $opt, $args) = @_; | ||
| 35 | |||
| 36 | # Use user-specified lib directories | ||
| 37 | unshift @INC, @{ $opt->{lib} }; | ||
| 38 | my @queues = split /,/, $opt->{queues}; | ||
| 39 | |||
| 40 | if($opt->{verbose}){ | ||
| 41 | $" =", "; | ||
| 42 | print "Listening to: @queues with $opt->{workers} worker(s).\n"; | ||
| 43 | } | ||
| 44 | |||
| 45 | $pm = new Parallel::ForkManager($opt->{workers}); | ||
| 46 | while (1) { | ||
| 47 | my $pid = $pm->start and next; | ||
| 48 | |||
| 49 | my $w = Sque->new( stomp => "$opt->{host}:$opt->{port}" )->worker; | ||
| 50 | $w->verbose( $opt->{verbose} ? 1 : 0 ); | ||
| 51 | $w->add_queues( @queues ); | ||
| 52 | $w->work; | ||
| 53 | |||
| 54 | $pm->finish; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | __END__ |