microsoft / o365-moodle

Office 365 and Azure Active Directory plugins for Moodle
GNU General Public License v3.0
180 stars 137 forks source link

Scheduled task failed: Sync users with Azure AD. (local_o365\task\usersync) Error reading from database #1896

Open flipmode45 opened 2 years ago

flipmode45 commented 2 years ago

We're having an issue where a number of our Azure users are missing from Moodle. Having checked the sync task, it is producing the below error on a full sync so never syncs all the users.

Moodle 3.114 / Microsoft 365 plugin 3.11.1 2021051705

Execute scheduled task: Sync users with Azure AD. (local_o365\task\usersync) ... started 15:13:39. Current memory use 12.1MB. ...... Starting sync ...... No skiptoken (delta) stored. ...... No deltatoken stored. ...... Using delta sync. ...... Contacting Azure AD... ...... Got response from Azure AD ...... Storing deltatoken ...... Clearing skiptoken (delta) (none received) ...... 11047 users received. Syncing... ...... 10000 users in chunk. Syncing... ... used 7 dbqueries ... used 1218.516176939 seconds Scheduled task failed: Sync users with Azure AD. (local_o365\task\usersync),Error reading from database

weilai-irl commented 2 years ago

Hi @flipmode45,

Please turn debug on and paste full error details.

Regards, Lai

flipmode45 commented 2 years ago

Execute scheduled task: Sync users with Azure AD. (local_o365\task\usersync) ... started 08:30:00. Current memory use 12.1MB. ...... Starting sync ...... No skiptoken (delta) stored. ...... No deltatoken stored. ...... Using delta sync. ...... Contacting Azure AD... ...... Got response from Azure AD ...... Storing deltatoken ...... Clearing skiptoken (delta) (none received) ...... 11036 users received. Syncing... ...... 10000 users in chunk. Syncing... ++ Did you remember to make the first column something unique in your call to get_records? Duplicate value 'external_examiner' found in column 'username'. ++

Notice: fwrite(): write of 22 bytes failed with errno=22 Invalid argument in D:\moodlesite\lib\moodlelib.php on line 9136

flipmode45 commented 2 years ago

@weilai-irl Any ideas with this please?

weilai-irl commented 2 years ago

Hi @flipmode45,

It looks like you have duplicate Moodle users with same username "external_examiner", possibly with different mnethostid. Could you check your list of users to confirm? If confirmed, could you verify these users are valid? The plugins assume Moodle usernames are unique, (i.e. it hasn't been specifically tested for Moodle sites having multiple MNet hosts). If supporting multiple mnethostid is required, please log a feature request and it will be queued.

Regards, Lai

flipmode45 commented 2 years ago

Hi @weilai-irl That error around a duplicate user seems like a misnomer. I've been into the database and removed the duplicate user record, but the task still ends with the same error each time:

Notice: fwrite(): write of 21 bytes failed with errno=22 Invalid argument in D:\moodlesite\lib\moodlelib.php on line 9136

The full output is now:

Execute scheduled task: Sync users with Azure AD. (local_o365\task\usersync) ... started 09:03:04. Current memory use 12.1MB. ...... Starting sync ...... No skiptoken (delta) stored. ...... No deltatoken stored. ...... Using delta sync. ...... Contacting Azure AD... ...... Got response from Azure AD ...... Storing deltatoken ...... Clearing skiptoken (delta) (none received) ...... 11129 users received. Syncing... ...... 10000 users in chunk. Syncing... ... used 7 dbqueries

Notice: fwrite(): write of 21 bytes failed with errno=22 Invalid argument in D:\moodlesite\lib\moodlelib.php on line 9136

weilai-irl commented 2 years ago

Hi @flipmode45,

I don't think this issue is caused by the Microsoft plugins. The plugins make calls to function mtrace() in lib/moodlelib.php to print debugging information, and it's the call to mtrace() that throws this error. I can confirm that there is no complicated logic in the calls to mtrace() function from the plugins.

You will need to troubleshoot this on your end as the issue is most likely related to your environment. Maybe write a simple script to do nothing but making a call to the mtrace() function, and see if it works. By looking at the implementation of mtrace(), the most probable reason of the issue is with STDOUT. Maybe it's not writable?

Regards, Lai

RayCoSector commented 1 year ago

Hi all, this might not be the same issue, but I think it is worth you trying what is mentioned here on my blog. It's happened to a few of our larger customers when they run a full sync or when it hits a Error in delta usersync: Error in API call. (I'm suspecting caused by a duplicate email) then the sync does not use the deltatoken.

I imagine the value of your max_allowed_packet will be set to the default value of 16mb, set this to 256mb and the sync will complete without the database going away.

Regards, Ray.