Douglas Wilson


GearmanX::Starter - Start Gearman Workers


  use GearmanX::Starter;

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

  my $f = sub {
    my $job = shift;
    my $workload = $job->workload();
    my $result   = reverse($workload);
    return $result;

    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 {
    $args->{worker} = Gearman::XS::Worker->new()


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.



Returns a Gearman starter object.


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


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


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]

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.


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.


Optional. A Log::Log4perl logger object.


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.


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.


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


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



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


Douglas Wilson, <dougw at>


Please report any bugs or feature requests to bug-gearmanx-starter at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


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

    perldoc GearmanX::Starter

You can also look for information at:



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 for more information.