0

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.

diggersworld
  • 12,770
  • 24
  • 84
  • 119

2 Answers2

1

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

Oversimplification in the docs there.

Gawain
  • 1,568
  • 10
  • 8
  • Updated example pushed to the repo here: https://github.com/bolt/bolt-docs/blob/master/source/tasks.md – Gawain Sep 26 '14 at 11:38
0

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
}
Michał G
  • 2,234
  • 19
  • 27