Extension Cron Job

Discussion in 'Extensions' started by stan911, May 15, 2018.

  1. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    2018-10-24 17:06:58
    L.T:
    Regular
    L.C:
    1
    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.
     
  2. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    9,463
    Likes Received:
    2,223
    @stan911 - it works the other way around, from an extension you can hook into an existing cron job.
    Can you explain what you are after ?
     
  3. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    2018-10-24 17:06:58
    L.T:
    Regular
    L.C:
    1
    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;
     
  4. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    9,463
    Likes Received:
    2,223
    @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.
    });
     
  5. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    2018-10-24 17:06:58
    L.T:
    Regular
    L.C:
    1
    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?
     
  6. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    9,463
    Likes Received:
    2,223
    Our system is inspired from wordpress'es one, see: https://codex.wordpress.org/Glossary#Action and https://codex.wordpress.org/Glossary#Filter
    In wordpress you foo add_action() and add_filter() and in mailwizz you do Yii::app()->hooks->addAction(); and Yii::app()->hooks->addFilter();
    Exactly.
     
  7. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    2018-10-24 17:06:58
    L.T:
    Regular
    L.C:
    1
    I understand now. Thank you for the documentation link and further clarification.
     
    twisted1919 likes this.
  8. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    2018-10-24 17:06:58
    L.T:
    Regular
    L.C:
    1
    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.
     
  9. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    9,463
    Likes Received:
    2,223
    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();
     
  10. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    2018-10-24 17:06:58
    L.T:
    Regular
    L.C:
    1
    It doesn't seem to get triggered. Any advice on how to debug this?
     

Share This Page