diggersworld
diggersworld

Reputation: 13080

Cron jobs with Bolt

Just a quick question on task scheduling and extending with Bolt CM.

Documentation: https://docs.bolt.cm/v20/tasks

When adding task scheduling to an extension, does the listener have to be specified outside of the class?

use Bolt\CronEvents;

$this->app['dispatcher']->addListener(CronEvents::CRON_INTERVAL, array($this, 'myJobCallbackMethod'));

class MyExtension extends \Bolt\BaseExtension {

    // ...

Or does it need to be declared in the initialize function?

use Bolt\CronEvents;

class MyExtension extends \Bolt\BaseExtension {

    public function initialize() {
         $this->app['dispatcher']->addListener(CronEvents::CRON_INTERVAL, array($this, 'myJobCallbackMethod'));
    }

    // ...

I assume it's the latter because $this outside of the class would be outside of the object context.
The documentation makes it look as if it directly follows, so thought I'd double check.

Upvotes: 0

Views: 318

Answers (2)

Michał G
Michał G

Reputation: 2302

I got it in initialize function and it works fine

 public function initialize()
{
     $this->app['dispatcher']->addListener(CronEvents::CRON_DAILY, array($this, 'run'));

}

public function run(){
 // code to run
}

Upvotes: 0

Gawain
Gawain

Reputation: 1568

You are correct, the $this->app['dispatcher']->addListener() call does need to be in a class context.

Oversimplification in the docs there.

Upvotes: 1

Related Questions