Skip to content

Commit

Permalink
MDL-77186 core: Move all uses of cronlib to new class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 13, 2023
1 parent 99a67b6 commit 9ec46c2
Show file tree
Hide file tree
Showing 36 changed files with 73 additions and 92 deletions.
5 changes: 2 additions & 3 deletions admin/cli/adhoc_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

require(__DIR__ . '/../../config.php');
require_once("{$CFG->libdir}/clilib.php");
require_once("{$CFG->libdir}/cronlib.php");

list($options, $unrecognized) = cli_get_params(
[
Expand Down Expand Up @@ -120,12 +119,12 @@
raise_memory_limit(MEMORY_EXTRA);

// Emulate normal session - we use admin account by default.
cron_setup_user();
\core\cron::setup_user();

$humantimenow = date('r', time());
$keepalive = (int)$options['keep-alive'];

\core\local\cli\shutdown::script_supports_graceful_exit();

mtrace("Server Time: {$humantimenow}\n");
cron_run_adhoc_tasks(time(), $keepalive, $checklimits);
\core\cron::run_adhoc_tasks(time(), $keepalive, $checklimits);
9 changes: 4 additions & 5 deletions admin/cli/automated_backups.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

require(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/clilib.php'); // cli only functions
require_once($CFG->libdir.'/cronlib.php');

// now get cli options
list($options, $unrecognized) = cli_get_params(array('help'=>false),
Expand Down Expand Up @@ -80,10 +79,10 @@

$starttime = microtime();

/// emulate normal session
cron_setup_user();
// Emulate normal session.
\core\cron::setup_user();

/// Start output log
// Start output log.
$timenow = time();

mtrace("Server Time: ".date('r',$timenow)."\n\n");
Expand All @@ -96,4 +95,4 @@
mtrace("Automated cron backups completed correctly");

$difftime = microtime_diff($starttime, microtime());
mtrace("Execution took ".$difftime." seconds");
mtrace("Execution took ".$difftime." seconds");
3 changes: 1 addition & 2 deletions admin/cli/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->libdir . '/cronlib.php');

// Now get cli option.
[$options, $unrecognized] = cli_get_params(
Expand Down Expand Up @@ -184,4 +183,4 @@


$keepalive = $options['keep-alive'];
cron_run($keepalive);
\core\cron::run_main_process($keepalive);
1 change: 0 additions & 1 deletion admin/cli/delete_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->libdir . '/cronlib.php');

list($options, $unrecognized) = cli_get_params(
[
Expand Down
5 changes: 2 additions & 3 deletions admin/cli/scheduled_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

require(__DIR__ . '/../../config.php');
require_once("$CFG->libdir/clilib.php");
require_once("$CFG->libdir/cronlib.php");

list($options, $unrecognized) = cli_get_params(
[
Expand Down Expand Up @@ -142,7 +141,7 @@
raise_memory_limit(MEMORY_EXTRA);

// Emulate normal session - we use admin account by default.
cron_setup_user();
\core\cron::setup_user();

// Execute the task.
\core\local\cli\shutdown::script_supports_graceful_exit();
Expand All @@ -164,5 +163,5 @@
$task->set_cron_lock($cronlock);
}

cron_run_inner_scheduled_task($task);
\core\cron::run_inner_scheduled_task($task);
}
3 changes: 1 addition & 2 deletions admin/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@

require('../config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->libdir . '/cronlib.php');

// extra safety
\core\session\manager::write_close();
Expand Down Expand Up @@ -78,4 +77,4 @@
@ini_set('html_errors', 'off');

// Execute the cron, disabling keepalive.
cron_run(0);
\core\cron::run_main_process(0);
2 changes: 1 addition & 1 deletion admin/settings/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@
);

// Set an upper limit of 15 minutes.
$setting->set_max_duration(15 * MINSEC);
$setting->set_max_duration(15 * MINSECS);

$temp->add($setting);

Expand Down
2 changes: 1 addition & 1 deletion admin/tool/customlang/cli/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
}

// Emulate normal session - we use admin account by default.
cron_setup_user();
\core\cron::setup_user();

// Get the file list.
$files = [];
Expand Down
12 changes: 6 additions & 6 deletions admin/tool/messageinbound/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public function process_message(
}

// Record the user that this script is currently being run as. This is important when re-processing existing
// messages, as cron_setup_user is called multiple times.
// messages, as \core\cron::setup_user is called multiple times.
$originaluser = $USER;

$envelope = $message->getEnvelope();
Expand Down Expand Up @@ -433,7 +433,7 @@ public function process_message(
// Process the message as the user.
$user = $this->addressmanager->get_data()->user;
mtrace("-- Processing the message as user {$user->id} ({$user->username}).");
cron_setup_user($user);
\core\cron::setup_user($user);

// Process and retrieve the message data for this message.
// This includes fetching the full content, as well as all headers, and attachments.
Expand All @@ -459,7 +459,7 @@ public function process_message(

// Returning to normal cron user.
mtrace("-- Returning to the original user.");
cron_setup_user($originaluser);
\core\cron::setup_user($originaluser);
return;
}

Expand All @@ -478,7 +478,7 @@ public function process_message(

// Returning to normal cron user.
mtrace("-- Returning to the original user.");
cron_setup_user($originaluser);
\core\cron::setup_user($originaluser);
return;
} catch (\Exception $e) {
// An unknown error occurred. The user is not informed, but the administrator is.
Expand All @@ -487,7 +487,7 @@ public function process_message(

// Returning to normal cron user.
mtrace("-- Returning to the original user.");
cron_setup_user($originaluser);
\core\cron::setup_user($originaluser);
return;
}

Expand All @@ -501,7 +501,7 @@ public function process_message(

// Returning to normal cron user.
mtrace("-- Returning to the original user.");
cron_setup_user($originaluser);
\core\cron::setup_user($originaluser);

mtrace("-- Finished processing " . $message->getUid());

Expand Down
2 changes: 0 additions & 2 deletions admin/tool/task/clear_fail_delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

require('../../../config.php');

require_once($CFG->libdir.'/cronlib.php');

// Basic security checks.
require_admin();
$context = context_system::instance();
Expand Down
2 changes: 0 additions & 2 deletions admin/tool/task/schedule_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

require('../../../config.php');

require_once($CFG->libdir.'/cronlib.php');

/**
* Function used to handle mtrace by outputting the text to normal browser window.
*
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploadcourse/cli/uploadcourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
$processoroptions['restorefile'] = $options['restorefile'];

// Emulate normal session.
cron_setup_user();
\core\cron::setup_user();

// Let's get started!
$content = file_get_contents($options['file']);
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploaduser/cli/uploaduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
raise_memory_limit(MEMORY_EXTRA);

// Emulate normal session - we use admin account by default, set language to the site language.
cron_setup_user();
\core\cron::setup_user();
$USER->lang = $CFG->lang;

$clihelper = new \tool_uploaduser\cli_helper();
Expand Down
1 change: 0 additions & 1 deletion backup/tests/automated_backup_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

global $CFG;
require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php');
require_once($CFG->libdir.'/cronlib.php');
require_once($CFG->libdir . '/completionlib.php');

/**
Expand Down
2 changes: 1 addition & 1 deletion backup/util/helper/backup_cron_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
mtrace("Skipping deleted courses", '...');
mtrace(sprintf("%d courses", self::remove_deleted_courses_from_schedule()));
mtrace('Running required automated backups...');
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();

mtrace("Getting admin info");
$admin = get_admin();
Expand Down
2 changes: 1 addition & 1 deletion course/classes/task/content_notification_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function execute() {
from user with id {$userfrom->id}.");
foreach ($users as $user) {

cron_setup_user($user, $course);
\core\cron::setup_user($user, $course);

$cm = get_fast_modinfo($course)->cms[$cm->id];

Expand Down
4 changes: 2 additions & 2 deletions course/classes/task/course_delete_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function execute() {
// Set the proper user.
if ($this->get_custom_data()->userid !== $this->get_custom_data()->realuserid) {
$realuser = \core_user::get_user($this->get_custom_data()->realuserid, '*', MUST_EXIST);
cron_setup_user($realuser);
\core\cron::setup_user($realuser);
\core\session\manager::loginas($this->get_custom_data()->userid, \context_system::instance(), false);
} else {
$user = \core_user::get_user($this->get_custom_data()->userid, '*', MUST_EXIST);
cron_setup_user($user);
\core\cron::setup_user($user);
}

$cms = $this->get_custom_data()->cms;
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/task/plagiarism_cron_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function execute() {
$plagiarismplugin = new $plagiarismclass;
if (method_exists($plagiarismplugin, 'cron')) {
mtrace('Processing cron function for plagiarism_plugin_' . $plugin . '...', '');
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();
mtrace('It has been detected the class ' . $plagiarismclass . ' has a legacy cron method
implemented. Plagiarism plugins should implement their own schedule tasks.', '');
$plagiarismplugin->cron();
Expand Down
2 changes: 1 addition & 1 deletion lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2838,7 +2838,7 @@ function cron_execute_plugin_type($plugintype, $description = null) {

mtrace('Processing cron function for ' . $component . '...');
debugging("Use of legacy cron is deprecated ($cronfunction). Please use scheduled tasks.", DEBUG_DEVELOPER);
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();
$pre_dbqueries = $DB->perf_get_queries();
$pre_time = microtime(true);

Expand Down
9 changes: 4 additions & 5 deletions lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2250,12 +2250,11 @@ public static function mimetype_from_file($fullpath) {
*/
public function cron() {
global $CFG, $DB;
require_once($CFG->libdir.'/cronlib.php');

// find out all stale draft areas (older than 4 days) and purge them
// those are identified by time stamp of the /. root dir
mtrace('Deleting old draft files... ', '');
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();
$old = time() - 60*60*24*4;
$sql = "SELECT *
FROM {files}
Expand All @@ -2272,7 +2271,7 @@ public function cron() {
// * preview files in the core preview filearea without the existing original file.
// * document converted files in core documentconversion filearea without the existing original file.
mtrace('Deleting orphaned preview, and document conversion files... ', '');
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();
$sql = "SELECT p.*
FROM {files} p
LEFT JOIN {files} o ON (p.filename = o.contenthash)
Expand All @@ -2299,7 +2298,7 @@ public function cron() {
require_once($CFG->libdir.'/filelib.php');
// Delete files that are associated with a context that no longer exists.
mtrace('Cleaning up files from deleted contexts... ', '');
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();
$sql = "SELECT DISTINCT f.contextid
FROM {files} f
LEFT OUTER JOIN {context} c ON f.contextid = c.id
Expand All @@ -2315,7 +2314,7 @@ public function cron() {
mtrace('done.');

mtrace('Call filesystem cron tasks.', '');
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();
$this->filesystem->cron();
mtrace('done.');
}
Expand Down
5 changes: 2 additions & 3 deletions lib/phpunit/classes/advanced_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,7 @@ public function waitForSecond() {
* @param int $matchuserid The userid to match.
*/
protected function runAdhocTasks($matchclass = '', $matchuserid = null) {
global $CFG, $DB;
require_once($CFG->libdir.'/cronlib.php');
global $DB;

$params = [];
if (!empty($matchclass)) {
Expand Down Expand Up @@ -729,7 +728,7 @@ protected function runAdhocTasks($matchclass = '', $matchuserid = null) {
$task->set_cron_lock($cronlock);
}

cron_prepare_core_renderer();
\core\cron::prepare_core_renderer();
$this->setUser($user);

$task->execute();
Expand Down
8 changes: 4 additions & 4 deletions lib/statslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function stats_cron_daily($maxdays=1) {
$defaultfproleid = (int)$CFG->defaultfrontpageroleid;

mtrace("Running daily statistics gathering, starting at $timestart:");
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();

$days = 0;
$total = 0;
Expand Down Expand Up @@ -664,7 +664,7 @@ function stats_cron_weekly() {
$DB->delete_records_select('stats_user_weekly', "timeend > $timestart");

mtrace("Running weekly statistics gathering, starting at $timestart:");
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();

$weeks = 0;
while ($now > $nextstartweek) {
Expand Down Expand Up @@ -807,7 +807,7 @@ function stats_cron_monthly() {


mtrace("Running monthly statistics gathering, starting at $timestart:");
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();

$months = 0;
while ($now > $nextstartmonth) {
Expand Down Expand Up @@ -1068,7 +1068,7 @@ function stats_get_next_month_start($time) {
function stats_clean_old() {
global $DB;
mtrace("Running stats cleanup tasks...");
cron_trace_time_and_memory();
\core\cron::trace_time_and_memory();
$deletebefore = stats_get_base_monthly();

// delete dailies older than 3 months (to be safe)
Expand Down
Loading

0 comments on commit 9ec46c2

Please sign in to comment.