GearmanX::Worker - Working class for the Gearmand job server


    #-- define a class for your workers:
    package MyWorker;
    use base qw(GearmanX::Worker);

    sub echo  :Expose  {
        my $param = shift;
        # ... compute something
        return $result;


    #-- in the meantime in the worker server
    my $w = new MyWorker;

    #-- or alternatively
    $w->run; # block here

    # somewhere else in a gearman client
    use GearmanX::Client;
    my $c = new GearmanX::Client;
    my $r = $c->job ('echo', '1+2');


This class implements the necessary infrastructure to comfortably write a gearman ( server. Instead of messing around with a task object where you get your arguments, you simply derive a subclass of GearmanX::Worker and define some methods (subs actually) which can handle certain gearman jobs. For that you mark these methods with an attribute Expose.

Parameter Handling

Every job handler receives exactly one parameter. That can be a scalar, a list reference or a hash reference. This is the data sent from the client, which may use GearmanX::Client.

Result Handling

Every job handler is supposed to return a result. That should be a scalar, a list reference or a hash reference. This data will be sent back to the client.

Owning the Protocol

As the gearman system only allows strings to be passed between clients and workers, there is a special encoding for list and hash references. See the implementation for details.



The constructor expects the following fields:

SERVERS (optional, default:

This field controls where the jobs servers are.



With this attribute you signal to the constructor that you intent a certain method to be exposed to the gearman systems as a job handler. At constructor time of your worker this method will be registered with the gearman server.



This starts the worker and blocks there. This method will never terminate. Well, unless the world explodes.


This method launches a thread and detaches it. It will not block and returns the thread object.


Robert Barta, <rho at>


Please report any bugs or feature requests to bug-gearmanx-worker 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.


Copyright 2009 Robert Barta, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.