MDL-51330 Cron: In log, scheduled tasks don't show component
[moodle.git] / admin / tool / task / cli / schedule_task.php
CommitLineData
7a82dcec
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/**
18 * CLI task execution.
19 *
20 * @package tool_task
21 * @copyright 2014 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25define('CLI_SCRIPT', true);
26
27require(__DIR__ . '/../../../../config.php');
28require_once("$CFG->libdir/clilib.php");
29require_once("$CFG->libdir/cronlib.php");
30
31list($options, $unrecognized) = cli_get_params(
32 array('help' => false, 'list' => false, 'execute' => false),
33 array('h' => 'help')
34);
35
36if ($unrecognized) {
37 $unrecognized = implode("\n ", $unrecognized);
38 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
39}
40
41if ($options['help'] or (!$options['list'] and !$options['execute'])) {
42 $help =
43"Scheduled cron tasks.
44
45Options:
46--execute=\\\\some\\\\task Execute scheduled task manually
47--list List all scheduled tasks
48-h, --help Print out this help
49
50Example:
51\$sudo -u www-data /usr/bin/php admin/tool/task/cli/scheduled_task.php --execute=\\\\core\\\\task\\\\session_cleanup_task
52
53";
54
55 echo $help;
56 die;
57}
58
59if ($options['list']) {
60 cli_heading("List of scheduled tasks ($CFG->wwwroot)");
61
62 $shorttime = get_string('strftimedatetimeshort');
63
64 $tasks = \core\task\manager::get_all_scheduled_tasks();
65 foreach ($tasks as $task) {
66 $class = '\\' . get_class($task);
67 $schedule = $task->get_minute() . ' '
68 . $task->get_hour() . ' '
69 . $task->get_day() . ' '
70 . $task->get_day_of_week() . ' '
71 . $task->get_month() . ' '
72 . $task->get_day_of_week();
0a5aa65b
73 $nextrun = $task->get_next_run_time();
74
18247eb0
FM
75 $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());
76 $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled();
77
78 if ($plugindisabled) {
79 $nextrun = get_string('plugindisabled', 'tool_task');
80 } else if ($task->get_disabled()) {
81 $nextrun = get_string('taskdisabled', 'tool_task');
0a5aa65b
82 } else if ($nextrun > time()) {
83 $nextrun = userdate($nextrun);
84 } else {
85 $nextrun = get_string('asap', 'tool_task');
86 }
7a82dcec
87
88 echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') . ' ' . $nextrun . "\n";
89 }
90 exit(0);
91}
92
93if ($execute = $options['execute']) {
94 if (!$task = \core\task\manager::get_scheduled_task($execute)) {
95 mtrace("Task '$execute' not found");
96 exit(1);
97 }
98
99 if (moodle_needs_upgrading()) {
100 mtrace("Moodle upgrade pending, cannot execute tasks.");
101 exit(1);
102 }
103
104 // Increase memory limit.
105 raise_memory_limit(MEMORY_EXTRA);
106
107 // Emulate normal session - we use admin account by default.
108 cron_setup_user();
109
110 $predbqueries = $DB->perf_get_queries();
111 $pretime = microtime(true);
894f36ab 112
b9dba4e3 113 $fullname = $task->get_name() . ' (' . get_class($task) . ')';
114 mtrace('Execute scheduled task: ' . $fullname);
894f36ab
BH
115 // NOTE: it would be tricky to move this code to \core\task\manager class,
116 // because we want to do detailed error reporting.
117 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
118 if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
119 mtrace('Cannot obtain cron lock');
120 exit(129);
121 }
122 if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
123 $cronlock->release();
124 mtrace('Cannot obtain task lock');
125 exit(130);
126 }
127
128 $task->set_lock($lock);
129 if (!$task->is_blocking()) {
130 $cronlock->release();
131 } else {
132 $task->set_cron_lock($cronlock);
133 }
134
7a82dcec 135 try {
371bfca1 136 get_mailer('buffer');
7a82dcec
137 $task->execute();
138 if (isset($predbqueries)) {
139 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
140 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
141 }
b9dba4e3 142 mtrace('Scheduled task complete: ' . $fullname);
7a82dcec 143 \core\task\manager::scheduled_task_complete($task);
371bfca1 144 get_mailer('close');
7a82dcec
145 exit(0);
146 } catch (Exception $e) {
147 if ($DB->is_transaction_started()) {
148 $DB->force_transaction_rollback();
149 }
150 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
151 mtrace("... used " . (microtime(true) - $pretime) . " seconds");
b9dba4e3 152 mtrace('Scheduled task failed: ' . $fullname . ',' . $e->getMessage());
a6e9c230
DP
153 if ($CFG->debugdeveloper) {
154 if (!empty($e->debuginfo)) {
155 mtrace("Debug info:");
156 mtrace($e->debuginfo);
157 }
158 mtrace("Backtrace:");
159 mtrace(format_backtrace($e->getTrace(), true));
160 }
7a82dcec 161 \core\task\manager::scheduled_task_failed($task);
371bfca1 162 get_mailer('close');
7a82dcec
163 exit(1);
164 }
165}