PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/classes/task/legacy_plugin_cron_task.php

https://bitbucket.org/moodle/moodle
PHP | 176 lines | 114 code | 14 blank | 48 comment | 17 complexity | 12726d49c6dd76a6e0ab2c6f38b77b77 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Scheduled task class.
  18. *
  19. * @package core
  20. * @copyright 2013 onwards Martin Dougiamas http://dougiamas.com
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace core\task;
  24. /**
  25. * Simple task to run cron for all plugins.
  26. * Note - this is only for plugins using the legacy cron method,
  27. * plugins can also now just add their own scheduled tasks which is the preferred method.
  28. * @deprecated since Moodle 3.9 MDL-52846. Please use new task API.
  29. * @todo MDL-61165 This will be deleted in Moodle 4.1
  30. */
  31. class legacy_plugin_cron_task extends scheduled_task {
  32. /**
  33. * Get a descriptive name for this task (shown to admins).
  34. *
  35. * @return string
  36. */
  37. public function get_name() {
  38. return get_string('tasklegacycron', 'admin');
  39. }
  40. /**
  41. * Do the job.
  42. * Throw exceptions on errors (the job will be retried).
  43. */
  44. public function execute() {
  45. global $CFG, $DB;
  46. $timenow = time();
  47. // Run the auth cron, if any before enrolments
  48. // because it might add users that will be needed in enrol plugins.
  49. $auths = get_enabled_auth_plugins();
  50. mtrace("Running auth crons if required...");
  51. foreach ($auths as $auth) {
  52. $authplugin = get_auth_plugin($auth);
  53. if (method_exists($authplugin, 'cron')) {
  54. mtrace("Running cron for auth/$auth...");
  55. $authplugin->cron();
  56. debugging("Use of legacy cron is deprecated (auth/$auth). Please use scheduled tasks.",
  57. DEBUG_DEVELOPER);
  58. if (!empty($authplugin->log)) {
  59. mtrace($authplugin->log);
  60. }
  61. }
  62. unset($authplugin);
  63. }
  64. // It is very important to run enrol early
  65. // because other plugins depend on correct enrolment info.
  66. mtrace("Running enrol crons if required...");
  67. $enrols = enrol_get_plugins(true);
  68. foreach ($enrols as $ename => $enrol) {
  69. // Do this for all plugins, disabled plugins might want to cleanup stuff such as roles.
  70. if (!$enrol->is_cron_required()) {
  71. continue;
  72. }
  73. mtrace("Running cron for enrol_$ename...");
  74. $enrol->cron();
  75. debugging("Use of legacy cron is deprecated (enrol_$ename). Please use scheduled tasks.",
  76. DEBUG_DEVELOPER);
  77. $enrol->set_config('lastcron', time());
  78. }
  79. // Run all cron jobs for each module.
  80. mtrace("Starting activity modules");
  81. if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
  82. foreach ($mods as $mod) {
  83. $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
  84. if (file_exists($libfile)) {
  85. include_once($libfile);
  86. $cronfunction = $mod->name."_cron";
  87. if (function_exists($cronfunction)) {
  88. mtrace("Processing module function $cronfunction ...\n", '');
  89. $predbqueries = null;
  90. $predbqueries = $DB->perf_get_queries();
  91. $pretime = microtime(1);
  92. if ($cronfunction()) {
  93. debugging("Use of legacy cron is deprecated ($cronfunction). Please use scheduled tasks.",
  94. DEBUG_DEVELOPER);
  95. $DB->set_field("modules", "lastcron", $timenow, array("id" => $mod->id));
  96. }
  97. if (isset($predbqueries)) {
  98. mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
  99. mtrace("... used " . (microtime(1) - $pretime) . " seconds");
  100. }
  101. // Reset possible changes by modules to time_limit. MDL-11597.
  102. \core_php_time_limit::raise();
  103. mtrace("done.");
  104. }
  105. }
  106. }
  107. }
  108. mtrace("Finished activity modules");
  109. mtrace("Starting blocks");
  110. if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
  111. // We will need the base class.
  112. require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
  113. foreach ($blocks as $block) {
  114. $blockfile = $CFG->dirroot.'/blocks/'.$block->name.'/block_'.$block->name.'.php';
  115. if (file_exists($blockfile)) {
  116. require_once($blockfile);
  117. $classname = '\\block_'.$block->name;
  118. $blockobj = new $classname;
  119. if (method_exists($blockobj, 'cron')) {
  120. mtrace("Processing cron function for ".$block->name.'....', '');
  121. if ($blockobj->cron()) {
  122. debugging("Use of legacy cron is deprecated ($classname::cron()). Please use scheduled tasks.",
  123. DEBUG_DEVELOPER);
  124. $DB->set_field('block', 'lastcron', $timenow, array('id' => $block->id));
  125. }
  126. // Reset possible changes by blocks to time_limit. MDL-11597.
  127. \core_php_time_limit::raise();
  128. mtrace('done.');
  129. }
  130. }
  131. }
  132. }
  133. mtrace('Finished blocks');
  134. mtrace('Starting admin reports');
  135. cron_execute_plugin_type('report');
  136. mtrace('Finished admin reports');
  137. mtrace('Starting course reports');
  138. cron_execute_plugin_type('coursereport');
  139. mtrace('Finished course reports');
  140. // Run gradebook import/export/report cron.
  141. mtrace('Starting gradebook plugins');
  142. cron_execute_plugin_type('gradeimport');
  143. cron_execute_plugin_type('gradeexport');
  144. cron_execute_plugin_type('gradereport');
  145. mtrace('Finished gradebook plugins');
  146. // All other plugins.
  147. cron_execute_plugin_type('message', 'message plugins');
  148. cron_execute_plugin_type('filter', 'filters');
  149. cron_execute_plugin_type('editor', 'editors');
  150. cron_execute_plugin_type('format', 'course formats');
  151. cron_execute_plugin_type('profilefield', 'profile fields');
  152. cron_execute_plugin_type('webservice', 'webservices');
  153. cron_execute_plugin_type('repository', 'repository plugins');
  154. cron_execute_plugin_type('qbehaviour', 'question behaviours');
  155. cron_execute_plugin_type('qformat', 'question import/export formats');
  156. cron_execute_plugin_type('qtype', 'question types');
  157. cron_execute_plugin_type('plagiarism', 'plagiarism plugins');
  158. cron_execute_plugin_type('theme', 'themes');
  159. cron_execute_plugin_type('tool', 'admin tools');
  160. cron_execute_plugin_type('local', 'local plugins');
  161. }
  162. }