Search code examples
cronbolt-cms

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.


Solution

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

    Oversimplification in the docs there.