Spread::Queue::Worker - accept Spread::Queue message assignments


Spread-Queue documentation  | view source Contained in the Spread-Queue distribution.

Index


NAME

Top

Spread::Queue::Worker - accept Spread::Queue message assignments

SYNOPSIS

Top

  use Spread::Queue::Worker;

  my $worker = new Spread::Queue::Worker(QUEUE => "myqueue",
                                         CALLBACK => \&mycallback,
                                        );
  $worker->run;

  sub mycallback {
    my ($worker, $originator, $input) = @_;

    my $result = {
		  response => "I heard you!",
		 };
    $worker->respond($originator, $result);
  }

DESCRIPTION

Top

A process that declares itself to be a Spread::Queue::Worker will be assigned messages in FIFO fashion by the sqm queue manager.

Messages as supported by Spread::Queue are serialized Perl hashes. Spread::Queue does not enforce structure on message contents.

A running sqm for the queue is required before any messages will be routed to the worker. Worker will not terminate if sqm is not running, or if it goes away. If the sqm terminates and restarts, it will reacquire any running workers (via heartbeat status signals).

METHODS

Top

  my $worker = new Spread::Queue::Worker("myqueue");

Establish link to Spread messaging environment, and prepare to receive messages on specific queue. Queue name will be obtained from SPREAD_QUEUE environment variable if not provided here.

  $worker->run;

Main loop for queue processing. Each incoming message will trigger a call to the user-specified callback function.

The loop will exit when $worker->terminate is called.

  use Event;
  $worker->setup_Event;
  Event::loop;

Configure Event.pm callback for processing incoming messages. $worker->terminate is still recommended in this configuration, to advise the queue manager to no longer assign tasks to this worker.

    $worker->respond($originator, $result);

If the worker wants to send a reply back to the originator of the request (e.g. in a request-reply environment). $originator is the Spread private mailbox address sent to the callback function. $result is a reference to a Perl hash.

    $worker->terminate;

Advises the queue manager that this worker is no longer available for task assignment. This will cause the runloop to exit.

Note that this is not automatically called on process termination. This means that the sqm might not realize that the worker is gone until its next automatic internal review cycle in a few seconds. For best messaging performance, it is important to notify the sqm as quickly as possible when a worker aborts.

AUTHOR

Top

Jason W. May <jmay@pobox.com>

COPYRIGHT

Top

SEE ALSO

Top

  L<Spread::Queue>
  L<Data::Serializer>
  L<Event>


Spread-Queue documentation  | view source Contained in the Spread-Queue distribution.