p-alik / perl-Gearman

9 stars 10 forks source link

some jobs are repeated indefinitely #48

Closed cghiban closed 4 years ago

cghiban commented 4 years ago

Hi, I have a worker with this function registered

my $worker = ...

sub get_job_history {
        my $gearman = shift;
        my $args = thaw $gearman->arg;

        #.....

        $worker->send_work_complete($gearman);
        return 'ok';
}
$worker->register_function("get_job_history", \&get_job_history);
$worker->work(
        on_start => sub {
                my ($jobhandle) = @_;
                print STDERR "on_start:", $jobhandle, $/;
        },
        on_complete => sub {
                my ($jobhandle, $result) = @_;
                print STDERR "on_complete:", $jobhandle, "\n", Dumper($result), $/;
        },
        on_fail => sub {
                my ($jobhandle, $err) = @_;
                print STDERR "on_fail:", $jobhandle, "\t", $err, $/;
        },
);

which I call it once in the background. For some reason this function is executed until I stop the worker:

on_start:127.0.0.1:4730//H:server:80
on_complete:127.0.0.1:4730//H:server:80
$VAR1 = 'ok';

on_start:127.0.0.1:4730//H:server:80
on_complete:127.0.0.1:4730//H:server:80
$VAR1 = 'ok';

on_start:127.0.0.1:4730//H:server:80
on_complete:127.0.0.1:4730//H:server:80
$VAR1 = 'ok';

using gearadmin I can see these:

$ gearadmin --show-jobs H:server:80 182 0 1

$ gearadmin --show-jobs H:server:80 5 0 1



How can I make sure the job runs only one time? I'm using gearmand v1.1.12 (not Gearman::Server). The server options are:

`/usr/sbin/gearmand -d -p 4730 -R`

Thanks,
Cornel
p-alik commented 4 years ago

Your get_job_history calls send_work_complete. Gearman::Worker->work executes the sub also.

https://github.com/p-alik/perl-Gearman/blob/6ad2eab514b81f07e8cc6eaf0db4b0bfccf434c1/lib/Gearman/Worker.pm#L361

I guess all will be fine if you remove send_work_complete from get_job_history

cghiban commented 4 years ago

Thanks, this seems to work. I actually added these 2 lines when trying to solve this issue:

$worker->send_work_complete($gearman);
return 'ok';

Basically I need to return something so that the server can remove the jobs?

p-alik commented 4 years ago

Basically I need to return something so that the server can remove the jobs?

You've to send something, but for sake of worker implementation. Otherwise the worker sends a WORK_FAIL packet to the client.

cghiban commented 4 years ago

Got it. Thanks