| Server IP : 213.255.246.8 / Your IP : 216.73.217.178 Web Server : Apache System : Linux dublin.stapolin.com 5.14.0-362.18.1.el9_3.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Jan 29 07:05:48 EST 2024 x86_64 User : stapolin ( 1019) PHP Version : 8.4.24 Disable Function : exec,passthru,shell_exec,system MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/stapolin/public_html/tracking.stapolin.com/includes/ |
Upload File : |
<?php
declare(strict_types=1);
if (PHP_SAPI !== 'cli') {
http_response_code(403);
exit('Cron scripts can only run from the command line.');
}
require_once __DIR__ . '/bootstrap.php';
function run_cron_job(string $jobName, callable $callback): void
{
$started = utc_now();
$lockKey = 'cron.' . $jobName . '.lock';
$statusKey = 'cron.' . $jobName . '.status';
$lockValue = get_system_setting($lockKey);
if ($lockValue !== null && strtotime($lockValue) > time() - 3600) {
echo $jobName . " is already running.\n";
return;
}
set_system_setting($lockKey, $started);
set_system_setting($statusKey, json_encode([
'status' => 'running',
'started_at' => $started,
'finished_at' => null,
'message' => null,
], JSON_THROW_ON_ERROR));
try {
$message = (string) $callback();
set_system_setting($statusKey, json_encode([
'status' => 'success',
'started_at' => $started,
'finished_at' => utc_now(),
'message' => $message,
], JSON_THROW_ON_ERROR));
echo $jobName . ': ' . $message . "\n";
} catch (Throwable $exception) {
error_log('Cron ' . $jobName . ' failed: ' . $exception->getMessage());
set_system_setting($statusKey, json_encode([
'status' => 'failed',
'started_at' => $started,
'finished_at' => utc_now(),
'message' => $exception->getMessage(),
], JSON_THROW_ON_ERROR));
throw $exception;
} finally {
set_system_setting($lockKey, '');
}
}