Cron Job Issues

Cron Job Issues

by Chelsy Ann Koshy -
Number of replies: 10

Moodle Version: 3.5+

No: of courses: 300

No: of users: 3000/each courses

Issue 1: When a user checked My Courses block in the dashboard to know their completion status after completing the courses, it's showing as 33% or 70% and not showing as 100%. But after 30 minutes, it will update as 100% as cron job for Calculate regular completion data is taking 30 minutes or 1 hr to complete the task even if we set as every minute.

Question: What should be done to run cron job (completion task) in every minute, so that students can't face the confusion?

Issue 2: The cron task Completion mark as started (\core\task\completion_daily_task) is not completing the task and interrupting other cron tasks also. When we disabled this task other task start running.

Question: Why and how can we resolve this?

Issue 3: We have sets one welcome email, two or three reminder emails and one completion email in each course as a re-engagement activity. So, what's happening is it will take more than 2 hours to send out a welcome email after the enrollment as well as completion email after the course completion as the re-engagement task in the cron job is taking more than 1 hour to complete the task.

Question: How can we resolve this?

Thanks in advance!!!

Average of ratings: -
In reply to Chelsy Ann Koshy

Re: Cron Job Issues

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi Chelsy,

Not sure if this is going to be helpful or not but you need to figure out where the bottleneck is. Are you running out of memory, CPU or is the database slowing your site down. Are things perhaps getting backed-up at your mail server?

By the way, you can have millions of users on a Moodle site. What matters is the number of users concurrently using Moodle and the types of activities they are using. For example, chats and quizzes are pretty intensive but a page activity is not.

At what time of the day the problem is happening. Is the same thing happening in the middle of the night?

Best regards,

Michael
In reply to Chelsy Ann Koshy

Re: Cron Job Issues

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
You can control the scheduled times by navigating to Site administration->Server->Scheduled tasks (/admin/tool/task/scheduledtasks.php). Be careful that some tasks are time/resource hungry and will have an effect on the performance of the site.
Average of ratings: Useful (1)
In reply to Chelsy Ann Koshy

Re: Cron Job Issues

by Pawan Pandey -
Hi there!

For your issue 1 and issue 2, things is this,
For marking course completion, Moodle utilize two task, core\task\completion_daily_task and another is core\task\completion_regular_task, so rather than running whole cron job at once, you can make one script . Following is same one


require_once('config.php');
require_once($CFG->dirroot.'/lib/classes/task/completion_regular_task.php');
require_once($CFG->dirroot.'/lib/classes/task/completion_daily_task.php');
try {
increment_revision_number('course', 'cacherev', '');
$obj = new core\task\completion_daily_task();
$obj->execute();
$obj = new core\task\completion_regular_task();
for ($i = 0 ; $i < 2; $i++) {
$obj->execute();
}
} catch (Exception $e) {
$obj->execute();
}

This script can help you to do course completion stuff, put this in place where it can run frequently, If you havent run cron for long time, then for first time, it will take time as it will create entry in {course_completions} table. Try above one.


For issue 3: I dont know.... sorry but i will research it and tell you.
Average of ratings: Useful (1)
In reply to Pawan Pandey

Re: Cron Job Issues

by Aye Min Htut -
Hi Pawan Pandey,

Thank you so much for your help. I'm trying to solve course completion problem. Your solution is working. But, please can you tell me where can I place your code? I tested it in "config.php and index.php". The config.php shows UI problems when I insert your code. Index.php needs to be refreshed the page. Sorry, I'm a beginner level coder. I need your help smile
In reply to Aye Min Htut

Re: Cron Job Issues

by Aye Min Htut -
Okay, I got it. I placed your script at the end of '/report/completion/lib.php'. Thanks smile
In reply to Aye Min Htut

Re: Cron Job Issues

by Pawan Pandey -
Hi ! make sure this script should be part of the file, which run frequently, I dont know how frequently you report/completion/lib.php will run, better is create one php file and run as cron job with 1~2min runtime,
Average of ratings: Useful (1)
In reply to Pawan Pandey

Re: Cron Job Issues

by Han Thet Nyein -
Hi Pawan, I don't know how to make it run in every 1~2min runtime. Please can you help me? After I use your script, my cron job is always running and I can't re-edit course completion setting again. Thanks ~
In reply to Han Thet Nyein

Re: Cron Job Issues

by Pawan Pandey -

Hi Han Thet Nyein

Breif: See provided script in above  is for marking course completion, when user complete the course.

Now it can be done by two ways. 

1. by running cron job... which apart from marking course complete perform lot of jobs. Which jobs and how frequently it to can be check in scheduled task settings in Moodle Site Administration. For this script to run i,e  in your moodle folder you have to run the script 

<moodlefolder>/admin/cli/cron.php

You can set it to run periodically. If you working on linux environment. USe following

vi /etc/crontab

In crontab add this entry

*/5 * * * /usr/bin/php /var/www/<moodlefolder>/admin/cli/cron.php

In windows, even i dont know.

2. Same thing is with custom  script. It run only specific job.

You can directly put path to above given script if you want to run via crontab in linux environment. But dont forget to add following line at top of script

define('CLI_SCRIPT', true);
require(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/clilib.php');

Note: The above are code add to run php file in moodle via CLI


Note: If you have lot of course in system, and lot of marking to be done... and depending upon your hardware where moodle is running. it may take time.

Average of ratings: Useful (1)
In reply to Pawan Pandey

Re: Cron Job Issues

by Han Thet Nyein -
Thank you Pawan Pandey for your detailed reply. I will try as you say. Thanks smile