chrisboulton / php-resque

PHP port of resque (Workers and Queueing)
MIT License
3.43k stars 759 forks source link

explicit die during onFailure doesn't work as expected. #312

Closed yccheok closed 8 years ago

yccheok commented 8 years ago

I wish to kill the entire script which picks up the queue item, when onFailure happen.

I tried.


\Resque_Event::listen('onFailure', function ($exception, $job) {

    // Requeue the failed item and dead.
    Job::enqueue($job->payload['class'], $args);

    // But, this doesn't kill the script? It still pick up the requeue item immediately.
    die();
});

I believe onFailure is run under different process (or thread?!)

What is the proper way to prevent resque from picking up the item, just after I requeue the item?

danhunsaker commented 8 years ago

So you want to stop the worker as soon as a failed job comes through...

You're right that every part of the job itself, including most of its hooks, is executed in its own forked process (see HOWITWORKS.md to see exactly where this split happens, and what is affected). To stop the worker, you'd need to send it a signal that it should either stop work or shut down. There's a section in the README on the signals a worker listens to.

Alternately, you could requeue the failed job to a different queue that the worker isn't listening on, then move it back over manually when it's ready to actually be run.

yccheok commented 8 years ago

Thanks. The following code works fine so far

    $pid = getmypid();
    \Resque_Event::listen('onFailure', function ($exception, $job) use (&$pid) {
        ...
        posix_kill($pid, SIGQUIT);
    });