Extension Cron Job

stan911

New Member
Is it possible to add a cron job to an extension and triggered automatically when that extension is installed/setup on an app?

I could not find any related information neither in the documentation nor the extension example provided by you guys.
 
The target is as follows:
- build an extra functionality, as an extension, in order to make it portable;
- that extension will allow access to some statistics;
- the statistics should be generated hourly, by running a cron job;
 
@stan911 - We do have a hourly cron job, so from your extension, you can hook into it, as follows:
PHP:
Yii::app()->hooks->addAction('console_command_hourly_after_process', function(){
    // do here your hourly work.
});
 
That's great. Thank you for the hint.
Is there a documentation regarding hooks and how to use them in general?

For example in this case, I'm wondering where will I use this? In the file ExtensionExt.php, in function run() ? If not there, where exactly?
 
One more question regarding this. After adding the hook as mentioned above, in the run() method of the main file of the extension, I've used within the callback function a method from a model as follows:

Yii::app()->hooks->addAction('console_command_hourly_after_process', function(){
Model::staticMethodName();
});

But it seems not to be triggered or working. Do you have any further hint on this, or how to test the cronjob if it takes this into account ? the extension is installed and enabled properly.
 
That looks correct. You might want, before calling Model::staticMethod to also import the model in the current scope ;)
PHP:
Yii::import('ext-your-extension.common.models.*');
Model::staticMethod();
 
Back
Top