Jump to content

simple lazy cron from template not working


Marc
 Share

Recommended Posts

I have a template called cronjobs.php. All it has (for now) is this super simple laze cron cal that attempts to write an entry to a processwire log file:

function cronTest(HookEvent $e) {

    wire('log')->save("cronjobs", "Cronjobs executed.");

}

// add a hook to your function:
wire()->addHook('LazyCron::every30Seconds', null, 'cronTest');

This does not work (no entry is ever added to the 'cronjobs' log file (despite me refreshing the page with the cronjobs.php template a hundred times). 

Am I doing something wrong?

Link to comment
Share on other sites

Your code cannot work. Your template is loaded after initialization of LazyCron. You have either to put your function in an autoload module or to initialize LazyCron again.

function cronTest(HookEvent $e) {     
  wire('log')->save("cronjobs", "Cronjobs executed.");     
}
wire()->addHook('LazyCron::every30Seconds', null, 'cronTest');
// $modules->get('LazyCron')->init();

I have to correct myself! Lazycron adds a hook in ProcessPageView::finished which is executed after the template is loaded. Sorry for confusing.
I tried your code and it works here. Beside caching missing write permission (assets/logs/) could cause the problem.

Edited by kixe
Link to comment
Share on other sites

Thanks for the suggestions guys, but it turned out not to be a cache or permission problem: I had set the page to 'unpublished' because I want nobody but an automated cron job to use that page. Publishing the page enables lazy cron to start working.

Edit: it worked a few times (as in the log was updated with a few new entries), but now it stopped working. Refreshing the page with the cronjobs.php template does absolutely nothing as far as the lazy cron is concerned  :( If I write to the log file directly in this template, it works just fine, for example wire('log')->save("cronjobs", "Cronjobs template executed.");

Edit 2: the cron started working again after I put some extra output in the cronjobs.php template. So it seems like if there is no output in a template (e.g. echo "hi"), the template file won't be processed. This is in the default PW multilanguage setup (with _main.php and _init.php automatically prepended to templates).

Link to comment
Share on other sites

  • 2 months later...

Just thought I'd add the solution to this problem, for reference. Turns out there's a file called /site/assets/cache/LazyCron.lock (or something along those lines) preventing the cron functionality from working when there are PHP errors in my cron logic, which can happen during development of course. Fixing the errors and deleting the lock file restores functionality. 

  • Like 8
Link to comment
Share on other sites

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

×
×
  • Create New...