jump to navigation

fixing a moodle cron problem – lastcron not updating January 26, 2010

Posted by ficial in code fixes, LMS, moodle, techy.

I recently finished a section of code that’s supposed to send reminder emails to users once per day. Moodle has an interesting approach for how this kind of thing is handled. In brief, there’s a DB table that tracks how often a process is supposed to run and the last time it ran, and the moodle system has a single central cron job that runs once every five minutes or so and calls the sub-processes based on the timing info stored in the DB. In principle that’s a reasonable approach. HOWEVER, it turns out that main cron code was failing to record when each sub-process ran, so the sub-process was running EVERY TIME the main process ran. In my case this meant it was sending out emails every 5 minutes instead of once a day – not good.

It’s a bit strange, since the main cron code looks like it’s supposed to do that update:

if ($blockobj->cron()) {
  if (!set_field('block','lastcron',$timenow,'id',$block->id)) {
    mtrace('Error: could not update timestamp for '.$block->name);

but for whatever reason that lastcron field isn’t actually getting updated AND no error message is being generated.  Perhaps the cron process in the block needs to return true very explicitly? Perhaps set_field is silently failing? Whatever the cause, it’s a serious problem.

The work around I found (in a timetable import script bug fix) was to explicitly update that field as a part of the cron sub-process.  So, after the emailing code I added:


and now everything is working as it should. It’s not ideal – the main cron process should be doing that lastcron update – but it gets the job done, it’s easy, and it won’t interfere if/when the main problem is fixed.



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: