Scheduled SQL queries will never run

Scheduled SQL queries will never run

by Róbert Toth -
Number of replies: 0

Hello all,

I have a custom SQL query which I need to run every day. The problem is that the query has never been started. After a day of struggling I have managed to find the fix, so I am creating this post to help anyone facing the same issue in the future. I have also filed a bug report on Moodle Bug Tracker.

I am running Moodle 3.7.2 with the latest Configurable reports plugin version 3.8.0 (2019122000).

Problem

So, let's analyze the problem:

  • First, I have ticked the "Auto run daily" option in report properties, so that is not the problem.
  • Secondly, my Cron is set up properly, as I can see Configurable Reports cron task being called in Moodle task log every minute:
Starting blocks
Processing cron function for configurable_reports....done.
Finished blocks
    • To check that this is working correctly on your site, just go to ./admin/tasklogs.php and search for word " configurable". You should see many \core\task\legacy_plugin_cron_task entries. Clicking on magnifying glass (🔍) on the right will reveal the Cron task log for that entry, which should contain (something like) the text above.
  • However, nothing more happens, and the scheduled task is never actually triggered.

Solution

Checking the source code of the plugin, I have found out where the problem lies. To fix it, you only have to change one line of code. In block_configurable_reports.php method cron() of class block_configurable_reports, lines 182-185:

$lastcron = $DB->get_field('block', 'lastcron', array('name' => 'configurable_reports'));
if (!$lastcron and ($lastcron + $this->cron < time()) ) {
  return false;
}

The condition to exit from method by returning false should be if ($lastcron …), and not (!$lastcron …) as it is now. Otherwise whenever the plugin never ever run its cron tasks before, it will always return, as $lastcron will be false.

Thus the only thing you need to do is to remove the exclamation mark ! at line 183, zip the whole plugin again and install it on your site.


All the best,
Robert

Average of ratings: Useful (2)