Skip to content

Commit

Permalink
rename fail_after_idle to timeout, document it better, and change
Browse files Browse the repository at this point in the history
job_servers getter/setter to be getter (or setter calling
set_job_servers).



git-svn-id: http://code.sixapart.com/svn/gearman/trunk@115 011c6a6d-750f-0410-a5f6-93fdcd050bc4
  • Loading branch information
bradfitz committed Jul 4, 2006
1 parent 76f2a41 commit 90c30db
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
36 changes: 22 additions & 14 deletions lib/Gearman/Client.pm
Expand Up @@ -22,7 +22,7 @@ sub new {
$self->{js_count} = 0;
$self->{sock_cache} = {};

$self->job_servers(@{ $opts{job_servers} })
$self->set_job_servers(@{ $opts{job_servers} })
if $opts{job_servers};

return $self;
Expand All @@ -36,8 +36,16 @@ sub new_task_set {
# getter/setter
sub job_servers {
my Gearman::Client $self = shift;
return $self->{job_servers} unless @_;
my $list = [ @_ ];
unless (@_) {
return wantarray ? @{$self->{job_servers}} : $self->{job_servers};
}
$self->set_job_servers(@_);
}

sub set_job_servers {
my Gearman::Client $self = shift;
my $list = ref $_[0] ? $_[0] : [ @_ ]; # take arrayref or array

$self->{js_count} = scalar @$list;
foreach (@$list) {
$_ .= ":7003" unless /:/;
Expand All @@ -48,14 +56,14 @@ sub job_servers {
sub _get_task_from_args {
my Gearman::Task $task;
if (ref $_[0]) {
$task = $_[0];
Carp::croak("Argument isn't a Gearman::Task") unless ref $_[0] eq "Gearman::Task";
$task = $_[0];
Carp::croak("Argument isn't a Gearman::Task") unless ref $_[0] eq "Gearman::Task";
} else {
my ($func, $arg_p, $opts) = @_;
my $argref = ref $arg_p ? $arg_p : \$arg_p;
Carp::croak("Function argument must be scalar or scalarref")
unless ref $argref eq "SCALAR";
$task = Gearman::Task->new($func, $argref, $opts);
my ($func, $arg_p, $opts) = @_;
my $argref = ref $arg_p ? $arg_p : \$arg_p;
Carp::croak("Function argument must be scalar or scalarref")
unless ref $argref eq "SCALAR";
$task = Gearman::Task->new($func, $argref, $opts);
}
return $task;

Expand All @@ -70,13 +78,13 @@ sub do_task {
my $did_err = 0;

$task->{on_complete} = sub {
$ret = shift;
$ret = shift;
};

$task->{on_fail} = sub {
$did_err = 1;
$did_err = 1;
};

my $ts = $self->new_task_set;
$ts->add_task($task);
$ts->wait;
Expand Down Expand Up @@ -195,7 +203,7 @@ Gearman::Client - Client for gearman distributed job system
# waiting on a set of tasks in parallel
my $taskset = $client->new_task_set;
$taskset->add_task( "add" => "1+2", {
on_complete => sub { ... }
on_complete => sub { ... }
});
$taskset->add_task( "divide" => "5/0", {
on_fail => sub { print "divide by zero error!\n"; },
Expand Down
2 changes: 1 addition & 1 deletion lib/Gearman/Objects.pm
Expand Up @@ -35,7 +35,7 @@ use fields (
'on_retry',
'on_status',
'retry_count',
'fail_after_idle',
'timeout',
'high_priority',

# from server:
Expand Down
11 changes: 6 additions & 5 deletions lib/Gearman/Task.pm
Expand Up @@ -20,7 +20,7 @@ sub new {
my $opts = shift || {};
for my $k (qw( uniq
on_complete on_fail on_retry on_status
retry_count fail_after_idle high_priority
retry_count timeout high_priority
)) {
$self->{$k} = delete $opts->{$k};
}
Expand Down Expand Up @@ -238,11 +238,12 @@ Number of times job will be retried if there are failures. Defaults to 0.
Boolean, whether this job should take priority over other jobs already
enqueued.
=item * fail_after_idle
=item * timeout
Automatically fail after this many seconds have elapsed. Defaults to 0,
which means never. Bypasses any retry_count remaining. Directly fails
after this amount of time.
Automatically fail, calling your on_fail callback, after this many
seconds have elapsed without an on_fail or on_complete being
called. Defaults to 0, which means never. Bypasses any retry_count
remaining.
=back
Expand Down
10 changes: 6 additions & 4 deletions t/10-all.t
Expand Up @@ -76,11 +76,11 @@ is($client->do_task('fail_exit'), undef,
'Job that failed via exit returned undef');
pid_is_dead(wait());

## Worker process times out (takes longer than fail_after_idle seconds).
## Worker process times out (takes longer than timeout seconds).
TODO: {
todo_skip 'fail_after_idle is not yet implemented', 1;
is($client->do_task('sleep', 5, { fail_after_idle => 3 }), undef,
'Job that timed out after 3 seconds returns failure (fail_after_idle)');
todo_skip 'timeout is not yet implemented', 1;
is($client->do_task('sleep', 5, { timeout => 3 }), undef,
'Job that timed out after 3 seconds returns failure');
}

## Test retry_count.
Expand Down Expand Up @@ -144,6 +144,8 @@ do {
$status = $client->get_status($handle);
} until $status->percent == 1;



sub pid_is_dead {
my($pid) = @_;
return if $pid == -1;
Expand Down

0 comments on commit 90c30db

Please sign in to comment.