forked from joomla/joomla-cms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScheduleRunner.php
More file actions
358 lines (311 loc) · 12.3 KB
/
Copy pathScheduleRunner.php
File metadata and controls
358 lines (311 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* @package Joomla.Plugin
* @subpackage System.schedulerunner
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\System\ScheduleRunner\Extension;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\DynamicSubscriberInterface;
use Joomla\CMS\Event\Model;
use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Table\Extension;
use Joomla\CMS\User\UserHelper;
use Joomla\Component\Scheduler\Administrator\Model\TasksModel;
use Joomla\Component\Scheduler\Administrator\Scheduler\Scheduler;
use Joomla\Component\Scheduler\Administrator\Task\Task;
use Joomla\Event\Event;
use Joomla\Event\EventInterface;
use Joomla\Registry\Registry;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* This plugin implements listeners to support a visitor-triggered lazy-scheduling pattern.
* If `com_scheduler` is installed/enabled and its configuration allows unprotected lazy scheduling, this plugin
* injects into each response with an HTML context a JS file {@see PlgSystemScheduleRunner::injectScheduleRunner()} that
* sets up an AJAX callback to trigger the scheduler {@see PlgSystemScheduleRunner::runScheduler()}. This is achieved
* through a call to the `com_ajax` component.
* Also supports the scheduler component configuration form through auto-generation of the webcron key and injection
* of JS of usability enhancement.
*
* @since 4.1.0
*/
final class ScheduleRunner extends CMSPlugin implements DynamicSubscriberInterface
{
/**
* Length of auto-generated webcron key.
*
* @var integer
* @since 4.1.0
*/
private const WEBCRON_KEY_LENGTH = 20;
/**
* @inheritDoc
*
* @return string[]
*
* @since 4.1.0
*
* @throws \Exception
*/
public function getSubscribedEvents(): array
{
$app = $this->getApplication();
$isSite = $app->isClient('site');
$isAdmin = $app->isClient('administrator');
$mapping = [];
if ($isSite || $isAdmin) {
$config = ComponentHelper::getParams('com_scheduler');
$mapping['onBeforeCompileHead'] = 'injectLazyJS';
$mapping['onAjaxRunSchedulerLazy'] = 'runLazyCron';
if ($isAdmin) {
// Only allowed in the administrator
$mapping['onContentPrepareForm'] = 'enhanceSchedulerConfig';
$mapping['onExtensionBeforeSave'] = 'generateWebcronKey';
$mapping['onAjaxRunSchedulerTest'] = 'runTestCron';
} elseif ($isSite && $config->get('webcron.enabled')) {
// Only allowed in the frontend
$mapping['onAjaxRunSchedulerWebcron'] = 'runWebCron';
}
}
return $mapping;
}
/**
* Inject JavaScript to trigger the scheduler in HTML contexts.
*
* @param EventInterface $event The onBeforeCompileHead event.
*
* @return void
*
* @since 4.1.0
*/
public function injectLazyJS(EventInterface $event): void
{
// Only inject in HTML documents
if ($this->getApplication()->getDocument()->getType() !== 'html') {
return;
}
$config = ComponentHelper::getParams('com_scheduler');
if (!$config->get('lazy_scheduler.enabled', true)) {
return;
}
/** @var TasksModel $model */
$model = $this->getApplication()->bootComponent('com_scheduler')
->getMVCFactory()->createModel('Tasks', 'Administrator', ['ignore_request' => true]);
$now = Factory::getDate('now', 'UTC');
if (!$model->hasDueTasks($now)) {
return;
}
// Add configuration options
$triggerInterval = $config->get('lazy_scheduler.interval', 300);
$this->getApplication()->getDocument()->addScriptOptions('plg_system_schedulerunner', ['interval' => $triggerInterval]);
// Load and injection directive
$wa = $this->getApplication()->getDocument()->getWebAssetManager();
$wa->getRegistry()->addExtensionRegistryFile('plg_system_schedulerunner');
$wa->useScript('plg_system_schedulerunner.run-schedule');
}
/**
* Acts on the LazyCron trigger from the frontend when Lazy Cron is enabled in the Scheduler component
* configuration. The lazy cron trigger is implemented in client-side JavaScript which is injected on every page
* load with an HTML context when the component configuration allows it. This method then triggers the Scheduler,
* which effectively runs the next Task in the Scheduler's task queue.
*
* @param EventInterface $e The onAjaxRunSchedulerLazy event.
*
* @return void
*
* @since 4.1.0
*
* @throws \Exception
*/
public function runLazyCron(EventInterface $e)
{
$config = ComponentHelper::getParams('com_scheduler');
if (!$config->get('lazy_scheduler.enabled', true)) {
return;
}
// Since the the request from the frontend may time out, try allowing execution after disconnect.
if (\function_exists('ignore_user_abort')) {
ignore_user_abort(true);
}
// Prevent PHP from trying to output to the user pipe. PHP may kill the script otherwise if the pipe is not accessible.
ob_start();
// Suppress all errors to avoid any output
try {
$this->runScheduler();
} catch (\Exception $e) {
}
ob_end_clean();
}
/**
* This method is responsible for the WebCron functionality of the Scheduler component.<br/>
* Acting on a `com_ajax` call, this method can work in two ways:
* 1. If no Task ID is specified, it triggers the Scheduler to run the next task in
* the task queue.
* 2. If a Task ID is specified, it fetches the task (if it exists) from the Scheduler API and executes it.<br/>
*
* URL query parameters:
* - `hash` string (required) Webcron hash (from the Scheduler component configuration).
* - `id` int (optional) ID of the task to trigger.
*
* @param Event $event The onAjaxRunSchedulerWebcron event.
*
* @return void
*
* @since 4.1.0
*
* @throws \Exception
*/
public function runWebCron(Event $event)
{
$config = ComponentHelper::getParams('com_scheduler');
$hash = $config->get('webcron.key', '');
if (!$config->get('webcron.enabled', false)) {
Log::add($this->getApplication()->getLanguage()->_('PLG_SYSTEM_SCHEDULE_RUNNER_WEBCRON_DISABLED'));
throw new \Exception($this->getApplication()->getLanguage()->_('JERROR_ALERTNOAUTHOR'), 403);
}
if (!\strlen($hash) || $hash !== $this->getApplication()->getInput()->get('hash')) {
throw new \Exception($this->getApplication()->getLanguage()->_('JERROR_ALERTNOAUTHOR'), 403);
}
$id = (int) $this->getApplication()->getInput()->getInt('id', 0);
$task = $this->runScheduler($id);
if (!empty($task) && !empty($task->getContent()['exception'])) {
throw $task->getContent()['exception'];
}
}
/**
* This method is responsible for the "test run" functionality in the Scheduler administrator backend interface.
* Acting on a `com_ajax` call, this method requires the URL to have a `id` query parameter (corresponding to an
* existing Task ID).
*
* @param Event $event The onAjaxRunScheduler event.
*
* @return void
*
* @since 4.1.0
*
* @throws \Exception
*/
public function runTestCron(Event $event)
{
if (!Session::checkToken('GET')) {
return;
}
$id = (int) $this->getApplication()->getInput()->getInt('id');
$allowConcurrent = $this->getApplication()->getInput()->getBool('allowConcurrent', false);
$user = $this->getApplication()->getIdentity();
if (empty($id) || !$user->authorise('core.testrun', 'com_scheduler.task.' . $id)) {
throw new \Exception($this->getApplication()->getLanguage()->_('JERROR_ALERTNOAUTHOR'), 403);
}
/**
* ?: About allow simultaneous, how do we detect if it failed because of pre-existing lock?
*
* We will allow CLI exclusive tasks to be fetched and executed, it's left to routines to do a runtime check
* if they want to refuse normal operation.
*/
$task = (new Scheduler())->getTask(
[
'id' => $id,
'allowDisabled' => true,
'bypassScheduling' => true,
'allowConcurrent' => $allowConcurrent,
]
);
if ($task) {
$task->run();
$event->addArgument('result', $task->getContent());
} else {
/**
* Placeholder result, but the idea is if we failed to fetch the task, it's likely because another task was
* already running. This is a fair assumption if this test run was triggered through the administrator backend,
* so we know the task probably exists and is either enabled/disabled (not trashed).
*/
// @todo language constant + review if this is done right.
$event->addArgument('result', ['message' => 'could not acquire lock on task. retry or allow concurrency.']);
}
}
/**
* Run the scheduler, allowing execution of a single due task.
* Does not bypass task scheduling, meaning that even if an ID is passed the task is only
* triggered if it is due.
*
* @param integer $id The optional ID of the task to run
*
* @return ?Task
*
* @since 4.1.0
* @throws \RuntimeException
*/
private function runScheduler(int $id = 0): ?Task
{
return (new Scheduler())->runTask(['id' => $id]);
}
/**
* Enhance the scheduler config form by dynamically populating or removing display fields.
*
* @param Model\PrepareFormEvent $event The onContentPrepareForm event.
*
* @return void
*
* @since 4.1.0
* @throws \UnexpectedValueException|\RuntimeException
*
* @todo Move to another plugin?
*/
public function enhanceSchedulerConfig(Model\PrepareFormEvent $event): void
{
$form = $event->getForm();
$data = $event->getData();
if (
$form->getName() !== 'com_config.component'
|| $this->getApplication()->getInput()->get('component') !== 'com_scheduler'
) {
return;
}
if (!empty($data['webcron']['key'])) {
$form->removeField('generate_key_on_save', 'webcron');
$relative = 'index.php?option=com_ajax&plugin=RunSchedulerWebcron&group=system&format=json&hash=' . $data['webcron']['key'];
$link = Route::link('site', $relative, false, Route::TLS_IGNORE, true);
$form->setValue('base_link', 'webcron', $link);
} else {
$form->removeField('base_link', 'webcron');
$form->removeField('reset_key', 'webcron');
}
}
/**
* Auto-generate a key/hash for the webcron functionality.
* This method acts on table save, when a hash doesn't already exist or a reset is required.
* @todo Move to another plugin?
*
* @param EventInterface $event The onExtensionBeforeSave event.
*
* @return void
*
* @since 4.1.0
*/
public function generateWebcronKey(EventInterface $event): void
{
/** @var Extension $table */
[$context, $table] = array_values($event->getArguments());
if ($context !== 'com_config.component' || $table->name !== 'com_scheduler') {
return;
}
$params = new Registry($table->params ?? '');
if (
empty($params->get('webcron.key'))
|| $params->get('webcron.reset_key') === 1
) {
$params->set('webcron.key', UserHelper::genRandomPassword(self::WEBCRON_KEY_LENGTH));
}
$params->remove('webcron.base_link');
$params->remove('webcron.reset_key');
$table->params = $params->toString();
}
}