When we've experienced this problem, the cron job has been stuck on either deleting items from the recycle bin, or deleting old draft files. The scheduled task is 'file_trash_cleanup_task'. This task is currently set to run every six hours, and does not get 'stuck' normally. The last time we experienced this problem, the error in the cron job log was:
Execute scheduled task: Cleanup files in trash (core\task\file_trash_cleanup_task)
... started 17:00:58. Current memory use 14MB.
Deleting old draft files... ... started 17:00:58. Current memory use 14.8MB.
... used 3 dbqueries
... used 418.41858696938 seconds
Scheduled task failed: Cleanup files in trash (core\task\file_trash_cleanup_task),Error writing to database
!!! Error reading from database !!!
The error in our PHP log seems to be more specific:
[12-Nov-2017 17:07:56 America/New_York] Default exception handler: Error reading from database Debug: SQLState: 08S01<br>
Error Code: 232<br>
Message: [Microsoft][ODBC Driver 11 for SQL Server]Shared Memory Provider: The pipe is being closed.
<br>
SQLState: 08S01<br>
Error Code: 232<br>
Message: [Microsoft][ODBC Driver 11 for SQL Server]Communication link failure<br>
SELECT * FROM mdl_task_scheduled WHERE classname = N'\core\task\file_trash_cleanup_task'
[array (
0 => '\\core\\task\\file_trash_cleanup_task',
)]
Error code: dmlreadexception
* line 486 of \lib\dml\moodle_database.php: dml_read_exception thrown
* line 324 of \lib\dml\sqlsrv_native_moodle_database.php: call to moodle_database->query_end()
* line 431 of \lib\dml\sqlsrv_native_moodle_database.php: call to sqlsrv_native_moodle_database->query_end()
* line 896 of \lib\dml\sqlsrv_native_moodle_database.php: call to sqlsrv_native_moodle_database->do_query()
* line 972 of \lib\dml\sqlsrv_native_moodle_database.php: call to sqlsrv_native_moodle_database->get_recordset_sql()
* line 1558 of \lib\dml\moodle_database.php: call to sqlsrv_native_moodle_database->get_records_sql()
* line 1530 of \lib\dml\moodle_database.php: call to moodle_database->get_record_sql()
* line 1509 of \lib\dml\moodle_database.php: call to moodle_database->get_record_select()
* line 630 of \lib\classes\task\manager.php: call to moodle_database->get_record()
* line 169 of \lib\cronlib.php: call to core\task\manager::scheduled_task_failed()
* line 67 of \lib\cronlib.php: call to cron_run_inner_scheduled_task()
* line 61 of \admin\cli\cron.php: call to cron_run()
[12-Nov-2017 17:07:56 America/New_York] PHP Warning: sqlsrv_free_stmt(): supplied resource is not a valid ss_sqlsrv_stmt resource in C:\Moodle\lib\dml\sqlsrv_native_moodle_recordset.php on line 135
[12-Nov-2017 17:07:56 America/New_York] PHP Fatal error: Uncaught coding_exception: Coding error detected, it must be fixed by a programmer: A lock was created but not released at:
C:\Moodle\lib\classes\task\manager.php on line 507
Code should look like:
$factory = \core\lock\lock_config::get_lock_factory('type');
$lock = $factory->get_lock(Resource id #656);
$lock->release(); // Locks must ALWAYS be released like this.
in C:\Moodle\lib\classes\lock\lock.php:117
Stack trace:
#0 [internal function]: core\lock\lock->__destruct()
#1 {main}
thrown in C:\Moodle\lib\classes\lock\lock.php on line 117
[12-Nov-2017 17:09:17 America/New_York] PHP Notice: Undefined index: SERVER_ADDR in C:\Moodle\lib\dmllib.php on line 351
[12-Nov-2017 17:09:22 America/New_York] PHP Notice: Undefined index: SERVER_ADDR in C:\Moodle\lib\dmllib.php on line 351
So it seems that the task is becoming disconnected from the database. The cron job is running on the database server, so the connection should be local, and network problems shouldn't be a factor. I'm not sure at this point if the disconnect is being caused by SQL Server or the cron job. Does anyone know what these two errors mean?
[12-Nov-2017 17:07:56 America/New_York] PHP Warning: sqlsrv_free_stmt(): supplied resource is not a valid ss_sqlsrv_stmt resource in C:\Moodle\lib\dml\sqlsrv_native_moodle_recordset.php on line 135
[12-Nov-2017 17:07:56 America/New_York] PHP Fatal error: Uncaught coding_exception: Coding error detected, it must be fixed by a programmer: A lock was created but not released at:
C:\Moodle\lib\classes\task\manager.php on line 507
I'm hoping these two errors will provide more insight as to what is going on.
Thanks