how to stop a delay job?
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!
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 how about making this behavior part of the extension? sounds really useful.
@zhuravljov thank you . But it's too complicated for me. how about add a function like this.
Yii::$app->queue->stop($jobId);
the behavior provides that functionality @yoage
@TerraSkye yes,I know but my class is not extend from component . like this
abstract class HelperBase extends Object