From fc059fc0fc85d0bfbc74f6984fc10e857d21df6c Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 4 May 2025 17:50:03 +0300 Subject: expose scheduled tasks to plugins, switch cache_starred_images plugin to use them instead of housekeeping hook --- classes/Scheduler.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'classes/Scheduler.php') 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 */ 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."); } /** -- cgit v1.2.3-54-g00ecf From 5263a07f61698eb26e0482129d66c5ab4be1c9c5 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 4 May 2025 18:06:43 +0300 Subject: record last cron expression (and stub owner_uid) used by scheduled task --- classes/Config.php | 2 +- classes/Pref_System.php | 3 +++ classes/Scheduler.php | 2 ++ sql/pgsql/migrations/151.sql | 6 ++++++ sql/pgsql/schema.sql | 4 +++- 5 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 sql/pgsql/migrations/151.sql (limited to 'classes/Scheduler.php') diff --git a/classes/Config.php b/classes/Config.php index c413317be..a4d2eb6b3 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -6,7 +6,7 @@ class Config { const T_STRING = 2; const T_INT = 3; - const SCHEMA_VERSION = 150; + const SCHEMA_VERSION = 151; /** override default values, defined below in _DEFAULTS[], prefixing with _ENVVAR_PREFIX: * diff --git a/classes/Pref_System.php b/classes/Pref_System.php index c5d7ab606..7946dc293 100644 --- a/classes/Pref_System.php +++ b/classes/Pref_System.php @@ -33,6 +33,7 @@ class Pref_System extends Handler_Administrative { + @@ -40,6 +41,7 @@ class Pref_System extends Handler_Administrative { order_by_asc(['last_cron_expression', 'task_name']) ->find_many(); foreach ($task_records as $task) { @@ -48,6 +50,7 @@ class Pref_System extends Handler_Administrative { ?> + diff --git a/classes/Scheduler.php b/classes/Scheduler.php index ce6a2f67d..77e858b56 100644 --- a/classes/Scheduler.php +++ b/classes/Scheduler.php @@ -117,6 +117,7 @@ class Scheduler { $task_record->last_run = Db::NOW(); $task_record->last_duration = $task_duration; $task_record->last_rc = $rc; + $task_record->last_cron_expression = $task['cron']->getExpression(); $task_record->save(); } else { @@ -127,6 +128,7 @@ class Scheduler { 'last_duration' => $task_duration, 'last_rc' => $rc, 'last_run' => Db::NOW(), + 'last_cron_expression' => $task['cron']->getExpression() ]); $task_record->save(); diff --git a/sql/pgsql/migrations/151.sql b/sql/pgsql/migrations/151.sql new file mode 100644 index 000000000..c3d9c159b --- /dev/null +++ b/sql/pgsql/migrations/151.sql @@ -0,0 +1,6 @@ +alter table ttrss_scheduled_tasks add column owner_uid integer default null references ttrss_users(id) ON DELETE CASCADE; +alter table ttrss_scheduled_tasks add column last_cron_expression varchar(250); + +update ttrss_scheduled_tasks set last_cron_expression = ''; + +alter table ttrss_scheduled_tasks alter column last_cron_expression set not null; diff --git a/sql/pgsql/schema.sql b/sql/pgsql/schema.sql index 3145629fc..21e3fd83a 100644 --- a/sql/pgsql/schema.sql +++ b/sql/pgsql/schema.sql @@ -400,6 +400,8 @@ create table ttrss_scheduled_tasks( task_name varchar(250) unique not null, last_duration integer not null, last_rc integer not null, - last_run timestamp not null default NOW()); + last_run timestamp not null default NOW(), + last_cron_expression varchar(250) not null, + owner_uid integer default null references ttrss_users(id) ON DELETE CASCADE); commit; -- cgit v1.2.3-54-g00ecf From 4cc40ddaa4552b2be9b696dc7b74e5c5c64fa372 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 4 May 2025 20:23:39 +0300 Subject: scheduler - only register built-in purge_orphaned_scheduled_tasks if running as default name --- classes/Scheduler.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'classes/Scheduler.php') diff --git a/classes/Scheduler.php b/classes/Scheduler.php index 77e858b56..b5df8f3fe 100644 --- a/classes/Scheduler.php +++ b/classes/Scheduler.php @@ -3,20 +3,23 @@ class Scheduler { private static ?Scheduler $instance = null; const TASK_RC_EXCEPTION = -100; + const DEFAULT_NAME = 'Default Scheduler'; /** @var array */ private array $scheduled_tasks = []; private string $name; - function __construct(string $name = 'Default Scheduler') { + function __construct(string $name = self::DEFAULT_NAME) { $this->set_name($name); - $this->add_scheduled_task('purge_orphaned_scheduled_tasks', '@weekly', - function() { - return $this->purge_orphaned_tasks(); - } - ); + if ($name === self::DEFAULT_NAME) { + $this->add_scheduled_task('purge_orphaned_scheduled_tasks', '@weekly', + function() { + return $this->purge_orphaned_tasks(); + } + ); + } } public static function getInstance(): Scheduler { -- cgit v1.2.3-54-g00ecf
task_name ?>last_cron_expression ?> last_run) ?> last_duration ?> last_rc ?>