Jump to content

Calling LazyCron in Process (Admin) module


Recommended Posts

Posted

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?

Posted

I think the easiest would be to separate the admin page (process module) from the data importer, this way the importer would be independent from admin page views. 

Posted

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.

  • 4 weeks later...
Posted

I would be interested in seeing your solution Dadog if you would not mind sharing. I am trying to use LazyCron with the Clean Empty Page Files Dir. I am wanting to run the clean directory module with the Every30Minutes hook offered by LazyCron on front-end page views, but kinda confused on how I should approach it.

Thanks

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
  • Recently Browsing   0 members

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