The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Gearman::Driver::Worker - Base class for workers

SYNOPSIS

    package My::Worker;

    use base qw(Gearman::Driver::Worker);
    use Moose;

    sub begin {
        my ( $self, $job, $workload ) = @_;
        # called before each job
    }

    sub prefix {
        # default: return ref(shift) . '::';
        return join '_', split /::/, __PACKAGE__;
    }

    sub do_something : Job : MinChilds(2) : MaxChilds(15) {
        my ( $self, $job, $workload ) = @_;
        # $job => Gearman::XS::Job instance
    }

    sub end {
        my ( $self, $job, $workload ) = @_;
        # called after each job
    }

    sub spread_work : Job {
        my ( $self, $job, $workload ) = @_;

        my $gc = Gearman::XS::Client->new;
        $gc->add_servers( $self->server );

        $gc->do_background( 'some_job_1' => $job->workload );
        $gc->do_background( 'some_job_2' => $job->workload );
        $gc->do_background( 'some_job_3' => $job->workload );
        $gc->do_background( 'some_job_4' => $job->workload );
        $gc->do_background( 'some_job_5' => $job->workload );
    }

    1;

DESCRIPTION

ATTRIBUTES

server

Gearman::Driver connects to the server passed to its constructor. This value is also stored in this class. This can be useful if a job uses Gearman::XS::Client to add another jobs. See 'spread_work' method in "SYNOPSIS" above.

METHODATTRIBUTES

Job

This will register the method with gearmand.

MinChilds

Minimum number of childs working parallel on this job/method.

MaxChilds

Maximum number of childs working parallel on this job/method.

Encode

This will automatically look for a method encode in this object which has to be defined in the subclass. It will call the encode method passing the return value from the job method. The return value of the encode method will be returned to the Gearman client. This is useful to serialize Perl datastructures to JSON before sending them back to the client.

    sub do_some_job : Job : Encode : Decode {
        my ( $self, $job, $workload ) = @_;
        return { message => 'OK', status => 1 };

        # calls 'encode' and returns JSON string: {"status":1,"message":"OK"}
    }

    sub custom_encoder : Job : Encode(enc_yaml) : Decode(dec_yaml) {
        my ( $self, $job, $workload ) = @_;
        return { message => 'OK', status => 1 };

        # calls 'enc_yaml' and returns YAML string:
        # ---
        # message: OK
        # status: 1
    }

    sub encode {
        my ( $self, $result ) = @_;
        return JSON::XS::encode_json($result);
    }

    sub decode {
        my ( $self, $workload ) = @_;
        return JSON::XS::decode_json($workload);
    }

    sub enc_yaml {
        my ( $self, $result ) = @_;
        return YAML::XS::Dump($result);
    }

    sub dec_yaml {
        my ( $self, $workload ) = @_;
        return YAML::XS::Load($workload);
    }

Decode

This will automatically look for a method decode in this object which has to be defined in the subclass. It will call the decode method passing the workload value ($job->workload). The return value of the decode method will be passed as 3rd argument to the job method. This is useful to deserialize JSON workload to Perl datastructures for example. If this attribute is not set, $job->workload and $workload is the same.

Example, workload is this string: {"status":1,"message":"OK"}

    sub decode {
        my ( $self, $workload ) = @_;
        return JSON::XS::decode_json($workload);
    }

    sub job1 : Job {
        my ( $self, $job, $workload ) = @_;
        # $workload eq $job->workload eq '{"status":1,"message":"OK"}'
    }

    sub job2 : Job : Decode {
        my ( $self, $job, $workload ) = @_;
        # $workload ne $job->workload
        # $job->workload eq '{"status":1,"message":"OK"}'
        # $workload = { status => 1, message => 'OK' }
    }

METHODS

prefix

Having the same method name in two different classes would result in a clash when registering it with gearmand. To avoid this, all jobs are registered with the full package and method name (e.g. My::Worker::some_job). The default prefix is ref(shift . '::'), but this can be changed by overriding the prefix method in the subclass, see "SYNOPSIS" above.

begin

This method is called before a job method is called. In this base class this methods just does nothing, but can be overridden in a subclass.

The parameters are the same as in the job method:

  • $self

  • $job

end

This method is called after a job method has been called. In this base class this methods just does nothing, but can be overridden in a subclass.

The parameters are the same as in the job method:

  • $self

  • $job

process_name

If this method is overridden in the subclass it will change the process name after a job has been forked.

The following parameters are passed to this method:

  • $self

  • $orig - the original process name ( $0 )

  • $job_name - the name of the job

Example:

    sub process_name {
        my ( $self, $orig, $job_name ) = @_;
        return "$orig ($job_name)";
    }

This may look like:

    plu       2034  0.0  1.7  22392 17948 pts/2    S    21:17   0:00 ./examples/test.pl (GDExamples::Sleeper::ZzZzZzzz)
    plu       2035  0.0  1.7  22392 17944 pts/2    S    21:17   0:00 ./examples/test.pl (GDExamples::Sleeper::ZzZzZzzz)
    plu       2036  0.0  1.7  22392 17948 pts/2    S    21:17   0:00 ./examples/test.pl (GDExamples::Sleeper::ZzZzZzzz)
    plu       2037  0.0  1.7  22392 17956 pts/2    S    21:17   0:00 ./examples/test.pl (GDExamples::Sleeper::long_running_ZzZzZzzz)

AUTHOR

Johannes Plunien <plu@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2009 by Johannes Plunien

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

SEE ALSO