Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to stop a delay job? #87

Open
yoage opened this issue Jul 20, 2017 · 5 comments
Open

how to stop a delay job? #87

yoage opened this issue Jul 20, 2017 · 5 comments
Assignees
Labels
type:docs Documentation type:enhancement Enhancement

Comments

@yoage
Copy link

yoage commented Jul 20, 2017

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!

@zhuravljov
Copy link
Member

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 zhuravljov added question type:docs Documentation and removed question labels Jul 21, 2017
@cebe
Copy link
Member

cebe commented Jul 21, 2017

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

@cebe cebe reopened this Jul 21, 2017
@zhuravljov zhuravljov self-assigned this Jul 23, 2017
@yoage
Copy link
Author

yoage commented Jul 24, 2017

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

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

@TerraSkye
Copy link
Contributor

the behavior provides that functionality @yoage

@cebe cebe added the type:enhancement Enhancement label Jul 24, 2017
@yoage
Copy link
Author

yoage commented Jul 26, 2017

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

abstract class HelperBase extends Object

zhuravljov added a commit that referenced this issue Jul 29, 2017
@samdark samdark added this to the 2.0.1 milestone Aug 15, 2017
@zhuravljov zhuravljov modified the milestones: 2.0.1, 2.0.2 Nov 3, 2017
@zhuravljov zhuravljov modified the milestones: 2.0.2, 2.0.x Dec 15, 2017
@samdark samdark removed this from the 2.0.x milestone May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

5 participants