MDL-61165 core: Final deprecation and removal of legacy cron.

Following MDL-52846 this now finally deprecates and removes the
following classes:
- \core\task\legacy_plugin_cron_task.
- \mod_quiz\task\legacy_quiz_reports_cron
- \mod_quiz\task\legacy_quiz_accessrules_cron
- \mod_workshop\task\legacy_workshop_allocation_cron

Please, use the Task API instead:
https://moodledev.io/docs/apis/subsystems/task

This also removes the corresponding and specific to legacy cron strings
from mod_quiz and mod_workshop.

Following MDL-52846 this now finally deprecates and removes the
functions:
  - cron_execute_plugin_type()
  - cron_bc_hack_plugin_functions()

Please, use the Task API instead:
https://moodledev.io/docs/apis/subsystems/task

Signed-off-by: Daniel Ziegenberg <[email protected]>
This commit is contained in:
Daniel Ziegenberg
2023-08-23 15:52:52 +08:00
committed by Andrew Nicols
parent cccc00954d
commit 7ff4626871
11 changed files with 20 additions and 491 deletions
@@ -1,176 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Scheduled task class.
*
* @package core
* @copyright 2013 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\task;
/**
* Simple task to run cron for all plugins.
* Note - this is only for plugins using the legacy cron method,
* plugins can also now just add their own scheduled tasks which is the preferred method.
* @deprecated since Moodle 3.9 MDL-52846. Please use new task API.
* @todo MDL-61165 This will be deleted in Moodle 4.1
*/
class legacy_plugin_cron_task extends scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('tasklegacycron', 'admin');
}
/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
global $CFG, $DB;
$timenow = time();
// Run the auth cron, if any before enrolments
// because it might add users that will be needed in enrol plugins.
$auths = get_enabled_auth_plugins();
mtrace("Running auth crons if required...");
foreach ($auths as $auth) {
$authplugin = get_auth_plugin($auth);
if (method_exists($authplugin, 'cron')) {
mtrace("Running cron for auth/$auth...");
$authplugin->cron();
debugging("Use of legacy cron is deprecated (auth/$auth). Please use scheduled tasks.",
DEBUG_DEVELOPER);
if (!empty($authplugin->log)) {
mtrace($authplugin->log);
}
}
unset($authplugin);
}
// It is very important to run enrol early
// because other plugins depend on correct enrolment info.
mtrace("Running enrol crons if required...");
$enrols = enrol_get_plugins(true);
foreach ($enrols as $ename => $enrol) {
// Do this for all plugins, disabled plugins might want to cleanup stuff such as roles.
if (!$enrol->is_cron_required()) {
continue;
}
mtrace("Running cron for enrol_$ename...");
$enrol->cron();
debugging("Use of legacy cron is deprecated (enrol_$ename). Please use scheduled tasks.",
DEBUG_DEVELOPER);
$enrol->set_config('lastcron', time());
}
// Run all cron jobs for each module.
mtrace("Starting activity modules");
if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
foreach ($mods as $mod) {
$libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
if (file_exists($libfile)) {
include_once($libfile);
$cronfunction = $mod->name."_cron";
if (function_exists($cronfunction)) {
mtrace("Processing module function $cronfunction ...\n", '');
$predbqueries = null;
$predbqueries = $DB->perf_get_queries();
$pretime = microtime(1);
if ($cronfunction()) {
debugging("Use of legacy cron is deprecated ($cronfunction). Please use scheduled tasks.",
DEBUG_DEVELOPER);
$DB->set_field("modules", "lastcron", $timenow, array("id" => $mod->id));
}
if (isset($predbqueries)) {
mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
mtrace("... used " . (microtime(1) - $pretime) . " seconds");
}
// Reset possible changes by modules to time_limit. MDL-11597.
\core_php_time_limit::raise();
mtrace("done.");
}
}
}
}
mtrace("Finished activity modules");
mtrace("Starting blocks");
if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
// We will need the base class.
require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
foreach ($blocks as $block) {
$blockfile = $CFG->dirroot.'/blocks/'.$block->name.'/block_'.$block->name.'.php';
if (file_exists($blockfile)) {
require_once($blockfile);
$classname = '\\block_'.$block->name;
$blockobj = new $classname;
if (method_exists($blockobj, 'cron')) {
mtrace("Processing cron function for ".$block->name.'....', '');
if ($blockobj->cron()) {
debugging("Use of legacy cron is deprecated ($classname::cron()). Please use scheduled tasks.",
DEBUG_DEVELOPER);
$DB->set_field('block', 'lastcron', $timenow, array('id' => $block->id));
}
// Reset possible changes by blocks to time_limit. MDL-11597.
\core_php_time_limit::raise();
mtrace('done.');
}
}
}
}
mtrace('Finished blocks');
mtrace('Starting admin reports');
cron_execute_plugin_type('report');
mtrace('Finished admin reports');
mtrace('Starting course reports');
cron_execute_plugin_type('coursereport');
mtrace('Finished course reports');
// Run gradebook import/export/report cron.
mtrace('Starting gradebook plugins');
cron_execute_plugin_type('gradeimport');
cron_execute_plugin_type('gradeexport');
cron_execute_plugin_type('gradereport');
mtrace('Finished gradebook plugins');
// All other plugins.
cron_execute_plugin_type('message', 'message plugins');
cron_execute_plugin_type('filter', 'filters');
cron_execute_plugin_type('editor', 'editors');
cron_execute_plugin_type('format', 'course formats');
cron_execute_plugin_type('profilefield', 'profile fields');
cron_execute_plugin_type('webservice', 'webservices');
cron_execute_plugin_type('repository', 'repository plugins');
cron_execute_plugin_type('qbehaviour', 'question behaviours');
cron_execute_plugin_type('qformat', 'question import/export formats');
cron_execute_plugin_type('qtype', 'question types');
cron_execute_plugin_type('plagiarism', 'plagiarism plugins');
cron_execute_plugin_type('theme', 'themes');
cron_execute_plugin_type('tool', 'admin tools');
cron_execute_plugin_type('local', 'local plugins');
}
}
-9
View File
@@ -131,15 +131,6 @@ $tasks = array(
'dayofweek' => '*',
'month' => '*'
),
array(
'classname' => 'core\task\legacy_plugin_cron_task',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
),
array(
'classname' => 'core\task\grade_cron_task',
'blocking' => 0,
+10 -124
View File
@@ -2657,137 +2657,23 @@ function cron_run_single_task() {
}
/**
* Executes cron functions for a specific type of plugin.
*
* @param string $plugintype Plugin type (e.g. 'report')
* @param string $description If specified, will display 'Starting (whatever)'
* and 'Finished (whatever)' lines, otherwise does not display
*
* @deprecated since Moodle 3.9 MDL-52846. Please use new task API.
* @todo MDL-61165 This will be deleted in Moodle 4.1.
*/
function cron_execute_plugin_type($plugintype, $description = null) {
global $DB;
// Get list from plugin => function for all plugins.
$plugins = get_plugin_list_with_function($plugintype, 'cron');
// Modify list for backward compatibility (different files/names).
$plugins = cron_bc_hack_plugin_functions($plugintype, $plugins);
// Return if no plugins with cron function to process.
if (!$plugins) {
return;
}
if ($description) {
mtrace('Starting '.$description);
}
foreach ($plugins as $component => $cronfunction) {
$dir = core_component::get_component_directory($component);
// Get cron period if specified in version.php, otherwise assume every cron.
$cronperiod = 0;
if (file_exists("$dir/version.php")) {
$plugin = new stdClass();
include("$dir/version.php");
if (isset($plugin->cron)) {
$cronperiod = $plugin->cron;
}
}
// Using last cron and cron period, don't run if it already ran recently.
$lastcron = get_config($component, 'lastcron');
if ($cronperiod && $lastcron) {
if ($lastcron + $cronperiod > time()) {
// Do not execute cron yet.
continue;
}
}
mtrace('Processing cron function for ' . $component . '...');
debugging("Use of legacy cron is deprecated ($cronfunction). Please use scheduled tasks.", DEBUG_DEVELOPER);
\core\cron::trace_time_and_memory();
$pre_dbqueries = $DB->perf_get_queries();
$pre_time = microtime(true);
$cronfunction();
mtrace("done. (" . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries, " .
round(microtime(true) - $pre_time, 2) . " seconds)");
set_config('lastcron', time(), $component);
core_php_time_limit::raise();
}
if ($description) {
mtrace('Finished ' . $description);
}
function cron_execute_plugin_type() {
throw new coding_exception(
'cron_execute_plugin_type() has been removed. Please, use the Task API instead: ' .
'https://moodledev.io/docs/apis/subsystems/task.'
);
}
/**
* Used to add in old-style cron functions within plugins that have not been converted to the
* new standard API. (The standard API is frankenstyle_name_cron() in lib.php; some types used
* cron.php and some used a different name.)
*
* @param string $plugintype Plugin type e.g. 'report'
* @param array $plugins Array from plugin name (e.g. 'report_frog') to function name (e.g.
* 'report_frog_cron') for plugin cron functions that were already found using the new API
* @return array Revised version of $plugins that adds in any extra plugin functions found by
* looking in the older location
*
* @deprecated since Moodle 3.9 MDL-52846. Please use new task API.
* @todo MDL-61165 This will be deleted in Moodle 4.1.
*/
function cron_bc_hack_plugin_functions($plugintype, $plugins) {
global $CFG; // Mandatory in case it is referenced by include()d PHP script.
if ($plugintype === 'report') {
// Admin reports only - not course report because course report was
// never implemented before, so doesn't need BC.
foreach (core_component::get_plugin_list($plugintype) as $pluginname => $dir) {
$component = $plugintype . '_' . $pluginname;
if (isset($plugins[$component])) {
// We already have detected the function using the new API.
continue;
}
if (!file_exists("$dir/cron.php")) {
// No old style cron file present.
continue;
}
include_once("$dir/cron.php");
$cronfunction = $component . '_cron';
if (function_exists($cronfunction)) {
$plugins[$component] = $cronfunction;
} else {
debugging("Invalid legacy cron.php detected in $component, " .
"please use lib.php instead");
}
}
} else if (strpos($plugintype, 'grade') === 0) {
// Detect old style cron function names.
// Plugin gradeexport_frog used to use grade_export_frog_cron() instead of
// new standard API gradeexport_frog_cron(). Also applies to gradeimport, gradereport.
foreach (core_component::get_plugin_list($plugintype) as $pluginname => $dir) {
$component = $plugintype.'_'.$pluginname;
if (isset($plugins[$component])) {
// We already have detected the function using the new API.
continue;
}
if (!file_exists("$dir/lib.php")) {
continue;
}
include_once("$dir/lib.php");
$cronfunction = str_replace('grade', 'grade_', $plugintype) . '_' .
$pluginname . '_cron';
if (function_exists($cronfunction)) {
$plugins[$component] = $cronfunction;
}
}
}
return $plugins;
function cron_bc_hack_plugin_functions() {
throw new coding_exception(
'cron_bc_hack_plugin_functions() has been removed. Please, use the Task API instead: ' .
'https://moodledev.io/docs/apis/subsystems/task.'
);
}
/**
+10
View File
@@ -87,6 +87,16 @@ information provided here is intended especially for developers.
to ensure in some test that the block drawer is closed. This helps with random failures due to the block drawer
being forced open in all behat tests.
* The core_useragent::get_device_type_list() function has been deprecated. Use core_useragent::devicetypes instead as a replacement.
* Final deprecation and removal of legacy cron. This includes the functions:
- cron_execute_plugin_type()
- cron_bc_hack_plugin_functions()
Please, use the Task API instead: https://moodledev.io/docs/apis/subsystems/task
* Final deprecation and removal of the following classes:
- \core\task\legacy_plugin_cron_task
- \mod_quiz\task\legacy_quiz_reports_cron
- \mod_quiz\task\legacy_quiz_accessrules_cron
- \mod_workshop\task\legacy_workshop_allocation_cron
Please, use the Task API instead: https://moodledev.io/docs/apis/subsystems/task
=== 4.2 ===
@@ -1,52 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Legacy Cron Quiz Access Rules Task
*
* @package mod_quiz
* @copyright 2017 Michael Hughes
* @author Michael Hughes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\task;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
/**
* Legacy Cron Quiz Access Rules Task
*
* @package mod_quiz
* @copyright 2017 Michael Hughes
* @author Michael Hughes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
class legacy_quiz_accessrules_cron extends \core\task\scheduled_task {
public function get_name() {
return get_string('legacyquizaccessrulescron', 'mod_quiz');
}
/**
* Execute all quizaccess subplugins legacy cron tasks.
*/
public function execute() {
cron_execute_plugin_type('quizaccess', 'quiz access rules');
}
}
@@ -1,53 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Legacy Cron Quiz Reports Task
*
* @package mod_quiz
* @copyright 2017 Michael Hughes
* @author Michael Hughes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
namespace mod_quiz\task;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
/**
* Legacy Cron Quiz Reports Task
*
* @package mod_quiz
* @copyright 2017 Michael Hughes
* @author Michael Hughes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
class legacy_quiz_reports_cron extends \core\task\scheduled_task {
public function get_name() {
return get_string('legacyquizreportscron', 'mod_quiz');
}
/**
* Execute all quizreport sub-plugins cron tasks.
*/
public function execute() {
cron_execute_plugin_type('quiz', 'quiz reports');
}
}
-18
View File
@@ -35,24 +35,6 @@ $tasks = [
'dayofweek' => '*',
'month' => '*'
],
[
'classname' => 'mod_quiz\task\legacy_quiz_reports_cron',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
],
[
'classname' => 'mod_quiz\task\legacy_quiz_accessrules_cron',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
],
[
'classname' => 'mod_quiz\task\quiz_notify_attempt_manual_grading_completed',
'blocking' => 0,
-2
View File
@@ -509,8 +509,6 @@ $string['layoutasshown'] = 'Page layout as shown.';
$string['layoutasshownwithpages'] = 'Page layout as shown. <small>(Automatic new page every {$a} questions.)</small>';
$string['layoutshuffledandpaged'] = 'Questions randomly shuffled with {$a} questions per page.';
$string['layoutshuffledsinglepage'] = 'Questions randomly shuffled, all on one page.';
$string['legacyquizaccessrulescron'] = 'Legacy cron quiz access rules';
$string['legacyquizreportscron'] = 'Legacy cron quiz reports';
$string['link'] = 'Link';
$string['listitems'] = 'Listing of items in quiz';
$string['literal'] = 'Literal';
@@ -1,47 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Legacy workshop allocation plugins cron.
*
* @package mod_workshop
* @copyright 2018 Simey Lameze <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_workshop\task;
defined('MOODLE_INTERNAL') || die();
/**
* Legacy workshop allocation plugins cron.
*
* @package mod_workshop
* @copyright 2018 Simey Lameze <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class legacy_workshop_allocation_cron extends \core\task\scheduled_task {
public function get_name() {
return get_string('legacyallocationplugincron', 'mod_workshop');
}
/**
* Execute all workshop allocation methods plugins cron tasks.
*/
public function execute() {
cron_execute_plugin_type('workshopallocation', 'workshop allocation methods');
}
}
-9
View File
@@ -33,14 +33,5 @@ $tasks = [
'day' => '*',
'month' => '*',
'dayofweek' => '*'
],
[
'classname' => '\mod_workshop\task\legacy_workshop_allocation_cron',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*'
]
];
-1
View File
@@ -206,7 +206,6 @@ $string['latesubmissions'] = 'Late submissions';
$string['latesubmissions_desc'] = 'Allow submissions after the deadline';
$string['latesubmissions_help'] = 'If enabled, an author may submit their work after the submissions deadline or during the assessment phase. Late submissions cannot be edited though.';
$string['latesubmissionsallowed'] = 'Late submissions are allowed';
$string['legacyallocationplugincron'] = 'Legacy cron workshop allocation';
$string['maxbytes'] = 'Maximum submission attachment size';
$string['modulename'] = 'Workshop';
$string['modulename_help'] = 'The workshop activity module enables the collection, review and peer assessment of students\' work.