GearmanX::Client - Gearman client which works with GearmanX::Worker


GearmanX-Worker documentation Contained in the GearmanX-Worker distribution.

Index


Code Index:

NAME

Top

GearmanX::Client - Gearman client which works with GearmanX::Worker

SYNOPSIS

Top

  use GearmanX::Client;
  my $c = new GearmanX::Client ( SERVERS => ['1.1.1.1'] );

  # launch job and wait for results
  $result = $c->job ('jobname', { one => 'param', at => 'a time' });
  # same thing
  $result = $c->job_sync (....);

  # launch job and continue
  $jobid = $c->job_async ('jobname', { one => 'param', at => 'a time' });

DESCRIPTION

Top

This class is simply a convenience to work comfortably alongside GearmanX::Worker. In that it mostly takes care that the single parameter (a scalar, a hash reference or a list reference) is serialized before the job is submitted to the gearman server.

INTERFACE

Top

Constructor

The constructor expects the following fields:

SERVERS

This is a list reference holding the list of IP addresses of the involved gearman job servers.

Methods

status

die unless $client->status

Returns non-zero if a jobserver can be contact. Launches a fake job to test that.

job, job_sync

$client->job ($jobname, $parameter);

Launches a job, serializes the parameter and waits for the result. That will be deserialized.

job_async

$client->job_async ($jobname, $parameter);

Launches a job, serializes the parameter and immediately returns the job id.

AUTHOR

Top

Robert Barta, <rho at devc.at>

BUGS

Top

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

COPYRIGHT & LICENSE

Top


GearmanX-Worker documentation Contained in the GearmanX-Worker distribution.
package GearmanX::Client;

use Data::Dumper;
use GearmanX::Serialization;

our $VERSION = '0.01';

sub new {
    my $class = shift;
    my %options = @_;
    my $self = bless \%options, $class;
    use Gearman::Client;
    $self->{client} = Gearman::Client->new ( job_servers => $self->{SERVERS} );
    return $self;
}

sub status {
    my $self = shift;
    my $r = $self->{client}->dispatch_background ('xadjmw32345fjasdcsdfsd9sdf');  # HACK: unlikely to be registered
    return defined $r; # anything non zero means the server is running
}

sub job {
    my $self = shift;
    return $self->job_sync (@_);
}

sub job_sync {
    my $self   = shift;
    my $jname  = shift || die "need a job name";
    my $param  = shift;

    my $s = GearmanX::Serialization::_serialize ($param);
#    warn "client before sending ".Dumper $s;
	     
    $s = $self->{client}->do_task( $jname, $s );                                      # only one scalar is passed there
#    warn "client got ".Dumper $s;

    return GearmanX::Serialization::_deserialize ($s);
}

sub job_async {
    my $self  = shift;
    my $jname = shift || die "need a job name";
    my $param = shift;

    my $s = GearmanX::Serialization::_serialize ($param);
    return $self->{client}->dispatch_background( $jname, $s );
}

"against all gods";