Enrol by user profile fields scheduled task not running

Enrol by user profile fields scheduled task not running

by Leon Flowers -
Number of replies: 16

Hi All,

The enrol by user profile fields schedule task is not running any more. It says "Unable to get task lock for enrol_attributes\task\processenrolments_task". However, if you go to any of the courses and force enrollment, it works fine. Anyone have a clue how to resolve this? I've tried restarting MySQL services as well as a server restart. Thanks in advance.



Average of ratings: -
In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Have you verified that your version of the plugin is compatible with your version of Moodle? Can you run it from the scheduled tasks page?
In reply to Emma Richardson

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
Yes it is compatible. It was working. Two people we manually running the task from the schedule tasks page on the day it broke and then next run time switched from every 5 mins to ASAP and now when you try to use "Run Now" the above message is what you get. However, you can process the enrolment rules from the course page using "Force Enrolments Now" with no problem.
In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
I guess you could try going to the scheduled tasks table in the db and checking the entries there for that task (and, if you are brave and backup first, maybe try deleting those entries and then set it up again.). I would guess that something glitched if two people were trying to run it at once...I would also try turning debugging on and trying to run it again and see if that tells you something else.
In reply to Emma Richardson

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
Do you you know the path to the scheduled tasks table in the db?
In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Not sure what you mean - just look at the list of tables and find the one for scheduled tasks/scheduled tasks logs...
In reply to Emma Richardson

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
The only thing I noticed that seems out of place is "blocking" set to 1 (instead of zero) on \core\task\create_contexts_task in the mdl_task_scheduled table. Everything else is zero. In your database, is that set to 1 or 0?
In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Mine is also set to 1. Just found this though - https://github.com/microsoft/o365-moodle/issues/1132 - you should be able to delete the lock on the mdl_lock_db table
In reply to Emma Richardson

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
I deleted two locks related to the plugin but it did not work. Seems like there are more locks though. I'll have to test and see. Thank you for your help.
In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Can you tell us what version of Moodle this is? From the Unable to get task lock presumably it's between Moodle 3.3 and 3.8.

Assuming Moodle 3.8, as far as I can see with MySQL it uses the filesystem for locking by attempting to obtain a lock on a file in the moodledata/lock subfolder. It looks like each lock file is specific to the scheduled task so it might be specifically moodledata/lock/5c/5c06584b18d6ad7a3d656bd935efc66b. Does this file exist? Can whichever user account the scheduled task being run as access write to this file? The timestamp of the file indicates when it was accessed and it contains the time it was written to in Unix epoch format.

Alternatively, you should be able to delete the file and if the Moodledata permissions are correct it will get re-created.

If it's still not working let us know the Moodle version, web server operating system, how the scheduled task is being run (for example: crontab), and also whether Moodledata is on a local filesystem or if it's some remote share such as NFS.

Average of ratings: Useful (1)
In reply to Leon Stringer

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
Hi Leon,
That specific file you mentioned does exist. I tried deleting it and it came right back when i tried to run the task again. The user account can access write to this file. I even tried deleting the lock directory and it came back with all the locks.

This is Moodle version 3.5.3 running php 7.4.25 on an Apache server. Remote. The schedule task is being run from the web interface using the "Run Now" button because it wont run with the cron auto scheduler. The next run time always displays "ASAP". 

I even tried installing a cron lock plugin to give me more information. See screenshot




In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

The obvious cause of Unable to get task lock would be that the initial run of the processenrolments_task isn't completing successfully, perhaps it's stuck in an infinite loop. Then any subsequent run of this task cannot get a lock and would show this error until the lock times out after 24 hours. Then the task would attempt to run again and, if the problem still exists, that lock will be held again and subsequent running of this task will fail with this error.

So you would need to troubleshoot this. Do you have command line access to the web server? If so try 1) enabling debugging (Debug messages: DEVELOPER, Display debug messages: Yes), 2) deleting the lock file, and 3) running the task at the command line. On my system the command is:

 sudo -u apache php admin/tool/task/cli/schedule_task.php --execute='\enrol_attributes\task\processenrolments_task'.

You may need a user other than apache. Common ones are www-data or php-fpm.

It wont run with the cron auto scheduler – do you mean that the cron is running OK apart from this one task? Or is there a problem running the cron too. Are all the other scheduled tasks running OK?

Which version of the enrol by user profile fields plugin is this? Hopefully the latest, 2021100800.

There are two other issues:

  1. Moodle 3.5.x supports up to PHP 7.2 and you have PHP 7.4. I can't see that this is causing this specific problem but when you enable debugging you'll probably see a errors and warnings from PHP as a result. You should switch to PHP 7.2 or upgrade Moodle to a version that supports PHP 7.4. Table of Moodle versions and PHP versions they support.
  2. Moodle 3.5.3 is missing two and half years of bug fixes and security updates. Again, I can't see that this problem is something that's fixed but you should update to Moodle 3.5.18.
In reply to Leon Stringer

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
"The obvious cause of Unable to get task lock would be that the initial run of the processenrolments_task isn't completing successfully...." I think this is exactly the case. I did the steps you recommended but none of the listed users work. As for your question, the cron is running ok expect for this one task. If I force it with "Run Now" it gives me that error message otherwise it stays in a state of "next run = ASAP". I try to release the lock with my cron plugin and get "Lock still in use by another process"


In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Sorry if I wasn't clear on what I was suggesting.

For the user to run admin/tool/task/cli/schedule_task.php as, you will need to determine this. Checking which user owns the files in moodledata/filedir may help (for example: with ls -lh), or indeed the owner of moodledata/lock/5c/5c06584b18d6ad7a3d656bd935efc66b. Trying each user I gave as examples is unlikely to identify this user, and your server may have specific users for each web site.

You would then need to suspend the running of the site cron (admin/cli/cron.php). For example: make a note of the current entry, then remove it. If you don't suspend this then the cron will attempt to run processenrolments_task when the lock is deleted creating the lock file again and preventing the task being manually run.

Then enable debugging (Debug messages: DEVELOPER, Display debug messages: Yes).

Then delete the lock file, moodledata/lock/5c/5c06584b18d6ad7a3d656bd935efc66b.

Then run the task, sudo -u USERNAME php admin/tool/task/cli/schedule_task.php --execute='\enrol_attributes\task\processenrolments_task' where USERNAME is the server user account you have determined PHP scripts are being run as.

Hopefully there'll be some output, maybe an error message or other output providing some clue as to what is going wrong with the task. The problem with the lock file suggests the task doesn't end normally but it may just hang,

Then turn debugging off again, you don't normally want this on a production site.

Then unsuspend the site cron, for example: add this again if it was removed.

Average of ratings: Useful (1)
In reply to Leon Stringer

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
Hi Leon,
Sorry, i took a break over the holidays. I think i have identified the user but this is the output i get when running that command line in the terminal: 

No input file specified.
[root@ded5306 ~]# sudo -u "USERNAME" php admin/tool/task/cli/schedule_task.php --execute='\enrol_attributes\task\processenrolments_task'
Status: 404 Not Found
Content-type: text/html; charset=UTF-8
In reply to Leon Flowers

Re: Enrol by user profile fields scheduled task not running

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

You need to use cd to change into the Moodle source code folder first. You'll need to work out the name of this folder – it contains the Moodle source code for your site – it's probably called something like public_html or moodle. For example, if it's the former the commands would be:

cd public_html
sudo -u "USERNAME" php admin/tool/task/cli/schedule_task.php --execute='\enrol_attributes\task\processenrolments_task'

The output:

Status: 404 Not Found
Content-type: text/html; charset=UTF-8

suggests that the build of PHP on your server is always expecting to send output to the browser, not to the command line. I've seen this before but I don't know why it happens. But hopefully it won't affect running the above command.

Remember, you will need to have debugging enabled for this test – and delete the lock file – to run the processenrolments_task and see if it reports errors. However, if you're running Moodle 3.5 on PHP 7.4 then expect to lots of other errors (such as PHP Deprecated warnings) because Moodle 3.5 doesn't support PHP 7.4.

In reply to Leon Stringer

Re: Enrol by user profile fields scheduled task not running

by Leon Flowers -
I'm also noticing this, if it gives any clue. The times stretch from 1 min to 34 mins