Jump to content

Calling LazyCron in Process (Admin) module


Dadog
 Share

Recommended Posts

Is it possible to call LazyCron in a Process (Admin) module? I've built an XML importscript to import pages via het API. Everything is working fine. I wanted to automate this module and run it at a particular time e.g. once a day. I've implemented LazyCron in my module like so:

class dataImport extends Process implements Module {

    public function init() {
        parent::init();
        $this->addHook('LazyCron::everyMinute', $this, 'myFunc');
    }

    public function myFunc() {
        Do some stuff ...
    }

} 

To my understanding the Process module is only activated in the backend and not the frontend? So when LazyCron kicks in on the front-end, Process modules addHook are not included in the execution? This the behaviour that we see now ... the hooks are executed when someone is working in the module, but not on the frontend ... How do I fix this and is this even possible?

Link to comment
Share on other sites

Like LostKobrakai said, you want to create a separate module that does the hooking. The key is to make it autoloadable (it usually makes sense to make it singular too). This way the hook will always be active, regardless of where you are. Here's a simple example

class MyHookingModule extends WireData implements Module
{
  public static function getModuleInfo()
  {
     return Array(
       'title'    => 'MyHookingModule', 
       'summary'  => 'Just a demo module', 
       'version'  => 1, 
       'autoload' => true, 
       'singular' => true, 
     ); 
  }

  public function init()
  {
     $m = $this->modules->get("dataImport");
     $this->addHook('LazyCron::everyMinute', $m, 'myFunc');
  }
}

However I would definitely suggest doing it like LostKobrakai suggested. Separate the Process-module from the importing module and with the Process-module, provide only PW admin-related aspects.

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...