diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2025-05-04 17:50:03 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2025-05-04 17:50:03 +0300 |
| commit | fc059fc0fc85d0bfbc74f6984fc10e857d21df6c (patch) | |
| tree | ea08c8be7b8d24f0f3023cc5f4eaa64710860269 /classes/Scheduler.php | |
| parent | d4faf2d3690592e64bd40c2d15d69897a63600a0 (diff) | |
expose scheduled tasks to plugins, switch cache_starred_images plugin to use them instead of housekeeping hook
Diffstat (limited to 'classes/Scheduler.php')
| -rw-r--r-- | classes/Scheduler.php | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/classes/Scheduler.php b/classes/Scheduler.php index 13d8e94ea..ce6a2f67d 100644 --- a/classes/Scheduler.php +++ b/classes/Scheduler.php @@ -7,7 +7,11 @@ class Scheduler { /** @var array<string, mixed> */ private array $scheduled_tasks = []; - function __construct() { + private string $name; + + function __construct(string $name = 'Default Scheduler') { + $this->set_name($name); + $this->add_scheduled_task('purge_orphaned_scheduled_tasks', '@weekly', function() { return $this->purge_orphaned_tasks(); @@ -22,6 +26,11 @@ class Scheduler { return self::$instance; } + /** Sets specific identifier for this instance of Scheduler used in debug logging */ + public function set_name(string $name) : void { + $this->name = $name; + } + /** * Adds a backend scheduled task which will be executed by updater (if due) during housekeeping. * @@ -42,13 +51,13 @@ class Scheduler { $task_name = strtolower($task_name); if (isset($this->scheduled_tasks[$task_name])) { - user_error("Attempted to override already registered scheduled task $task_name", E_USER_WARNING); + user_error("[$this->name] Attempted to override already registered scheduled task $task_name", E_USER_WARNING); return false; } else { try { $cron = new Cron\CronExpression($cron_expression); } catch (InvalidArgumentException $e) { - user_error("Attempt to register scheduled task $task_name failed: " . $e->getMessage(), E_USER_WARNING); + user_error("[$this->name] Attempt to register scheduled task $task_name failed: " . $e->getMessage(), E_USER_WARNING); return false; } @@ -64,7 +73,7 @@ class Scheduler { * Execute scheduled tasks which are due to run and record last run timestamps. */ function run_due_tasks() : void { - Debug::log('Processing all scheduled tasks...'); + Debug::log("[$this->name] Processing all scheduled tasks..."); $tasks_succeeded = 0; $tasks_failed = 0; @@ -89,7 +98,7 @@ class Scheduler { try { $rc = (int) $task['callback'](); } catch (Exception $e) { - user_error("Scheduled task $task_name failed with exception: " . $e->getMessage(), E_USER_WARNING); + user_error("[$this->name] Scheduled task $task_name failed with exception: " . $e->getMessage(), E_USER_WARNING); $rc = self::TASK_RC_EXCEPTION; } @@ -125,7 +134,7 @@ class Scheduler { } } - Debug::log("Processing scheduled tasks finished with $tasks_succeeded tasks succeeded and $tasks_failed tasks failed."); + Debug::log("[$this->name] Processing scheduled tasks finished with $tasks_succeeded tasks succeeded and $tasks_failed tasks failed."); } /** |