NAME

GearmanX::Starter - Start Gearman Workers

SYNOPSIS

use GearmanX::Starter;

my $gms = GearmanX::Starter->new();

my $f = sub {
  my $job = shift;

  my $workload = $job->workload();
  my $result   = reverse($workload);

  return $result;
};

$gms->start({
  name => 'Reverser',
  func_list => [
    [ 'reverse', $f ],
  ],
  logger => $logger,
  init_func => sub { Log::Log4Perl::init($conf_file) },
});

# Also possible
# Delayed local worker creation
my $args = {
  name => 'Reverser',
  func_list => [
    [ 'reverse', $f ],
  ],
  logger => $logger,
};
$args->{init_func} = sub {
  Log::Log4Perl::init($conf_file);
  $args->{worker} = Gearman::XS::Worker->new()
};
$gms->start($args);

DESCRIPTION

Starts a Gearman worker and registers functions. Forks and backgrounds the forked process as a daemon. When the worker receives a SIGTERM signal, it will complete any current request and then exit.

METHODS

new()

Returns a Gearman starter object.

start()

Starts the Gearman worker with the supplied function list. Accepts the following options as a hash reference:

name

This is the name which will be assigned to $0 and is what will show up in ps output as the program name.

func_list

A list of function name and callback pairs that will be registered with the Gearman worker, and an optional flag to not mark this function as 'critical' to complete in case of a termination signal. A fourth argument is a scalar that will be used as the Gearman worker "options" argument when registering the function. If the fourth argument is supplied, you must also supply the third argument. E.g.:

my $not_critical = 1;
my $options = "abc";
...
func_list => [
  ['func1', \&func1],
  ['func2', \&func2, $not_critical]
  ['func3', \&func3, '', $options]
],
...
dereg_func

Optional. By default, a function will be registered that will unregister a function in the worker. The name of the function will be 'dereg:%PID%' where '%PID%' will be the pid of the worker process. You can use this option to change the name, and you can use the string '%PID%' in the name and it will be replaced by the pid of the worker. If a false value is used for this option, the dereg function will not be registered.

sigterm

Optional. A list of signal names that will terminate the worker loop. Default is ['TERM']. You can also pass an empty arrayref to not install a signal handler.

logger

Optional. A Log::Log4perl logger object.

init_func

Optional. A function to call after forking the daemon process, but before entering the worker loop, and just before creating the Gearman::XS::Worker object. In this function, you may want to re-open the log filehandles.

servers

Optional. A list of gearman server host/port pairs. Default is an arrayref containing one empty list: [[]] (which defaults to the gearman server on the localhost). You can pass in an empty arrayref if you are passing in a worker with servers already added.

worker

Optional. A Gearman::XS::Worker object. A new object is created if this is not supplied.

sleep_and_retry

Optional. If a gearman error is returned from the work() method, sleep for this many seconds, and retry.

VARIABLES

$QUIT

A package global varible that, if set, will cause the worker to exit after finishing the current job.

AUTHOR

Douglas Wilson, <dougw at cpan.org>

BUGS

Please report any bugs or feature requests to bug-gearmanx-starter at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=GearmanX-Starter. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc GearmanX::Starter

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2010 Douglas Wilson.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.