2023-03-30 18:42:40 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Queue query runs any pending tasks
|
|
|
|
|
*
|
2023-04-30 20:07:37 +01:00
|
|
|
* @var Cache $cache
|
|
|
|
|
* @var \DI\Container $container
|
|
|
|
|
* @var Navigation $cc_nav
|
|
|
|
|
* @var Navigation $navigation
|
|
|
|
|
* @var Navigation $staffcp_nav
|
|
|
|
|
* @var Pages $pages
|
|
|
|
|
* @var Smarty $smarty
|
|
|
|
|
* @var TemplateBase $template
|
|
|
|
|
* @var User $user
|
|
|
|
|
* @var Widgets $widgets
|
2023-03-30 18:42:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
header('Content-type: application/json;charset=utf-8');
|
|
|
|
|
|
2023-04-30 20:07:37 +01:00
|
|
|
define('PAGE', 'queue_query');
|
|
|
|
|
require_once(ROOT_PATH . '/core/templates/frontend_init.php');
|
|
|
|
|
|
2023-03-30 18:42:40 +01:00
|
|
|
// TODO: mixed type for $output when PHP 8.0+
|
|
|
|
|
function return_json($output, ?bool $error = false) {
|
|
|
|
|
echo json_encode(['error' => $error, 'output' => $output], JSON_PRETTY_PRINT);
|
|
|
|
|
die();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cache->setCache('queue');
|
|
|
|
|
$last_run = intval($cache->retrieve('last_run') ?? 0);
|
|
|
|
|
|
2023-06-12 10:30:49 -06:00
|
|
|
$interval = floatval(Settings::get('queue_interval') ?? 1);
|
|
|
|
|
$runner = Settings::get('queue_runner');
|
2023-03-30 18:42:40 +01:00
|
|
|
|
|
|
|
|
if ($runner == 'cron') {
|
|
|
|
|
if (!isset($_GET['cron'])) {
|
|
|
|
|
return_json('Invalid cron URL', true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 10:30:49 -06:00
|
|
|
if (Input::get('key') != Settings::get('cron_key')) {
|
2023-03-30 18:42:40 +01:00
|
|
|
return_json('Invalid cron key', true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$date = date('U');
|
|
|
|
|
$next_run = $last_run + ($interval * 60);
|
|
|
|
|
$diff = $next_run - $date;
|
|
|
|
|
|
|
|
|
|
if ($last_run && $diff > 0) {
|
|
|
|
|
return_json("Please wait $diff seconds before executing the queue", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update last run immediately
|
|
|
|
|
$cache->store('last_run', $date);
|
|
|
|
|
|
2023-04-30 20:07:37 +01:00
|
|
|
// Load modules + template
|
|
|
|
|
Module::loadPage($user, $pages, $cache, $smarty, [$navigation, $cc_nav, $staffcp_nav], $widgets, $template);
|
|
|
|
|
|
|
|
|
|
$template->onPageLoad();
|
|
|
|
|
|
2023-03-30 18:42:40 +01:00
|
|
|
// Process queue
|
2023-04-30 20:07:37 +01:00
|
|
|
$tasks = Queue::process($container);
|
2023-03-30 18:42:40 +01:00
|
|
|
|
|
|
|
|
return_json($tasks);
|