yii2-queue icon indicating copy to clipboard operation
yii2-queue copied to clipboard

how to stop a delay job?

Open yoage opened this issue 8 years ago • 5 comments

hello,I got a delay jobID and I want to stop it later. how to stop it ? this is my code

 $jobID = Yii::$app->queue->delay(1234)->push(new \console\jobs\DemoJob([
            'function' => "test1",
            'params'   => ['order_id' => 123],

        ]));

Thank you!

yoage avatar Jul 20 '17 15:07 yoage

You can use stop condition inside a job execute method. For example:

class SomeJob implements \yii\queue\Job
{
    public function execute($queue)
    {
        if (/* stop condition */) {
            return;
        }

        // ...
    }
}

Also you can add behavior to component. Example:

class StopBehavior extends Behavior
{
    public function events()
    {
        return [
            Queue::EVENT_BEFORE_EXEC => function (ExecEvent $event) {
                if (Yii::$app->cache->exists('stoped' . $event->id)) {
                    Yii::$app->cache->delete('stoped' . $event->id);
                    $event->handled = true; // A job will not be executed
                }
            }
        ];
    }

    public function stop($jobId)
    {
        Yii::$app->cache->set('stoped' . $jobId, true);
    }
}

zhuravljov avatar Jul 21 '17 13:07 zhuravljov

@zhuravljov how about making this behavior part of the extension? sounds really useful.

cebe avatar Jul 21 '17 13:07 cebe

@zhuravljov thank you . But it's too complicated for me. how about add a function like this.

Yii::$app->queue->stop($jobId);

yoage avatar Jul 24 '17 02:07 yoage

the behavior provides that functionality @yoage

TerraSkye avatar Jul 24 '17 11:07 TerraSkye

@TerraSkye yes,I know but my class is not extend from component . like this

abstract class HelperBase extends Object

yoage avatar Jul 26 '17 02:07 yoage