diff options
| -rw-r--r-- | classes/RSSUtils.php | 6 | ||||
| -rw-r--r-- | classes/Scheduler.php | 29 |
2 files changed, 33 insertions, 2 deletions
diff --git a/classes/RSSUtils.php b/classes/RSSUtils.php index a051a7dc2..66bbd766e 100644 --- a/classes/RSSUtils.php +++ b/classes/RSSUtils.php @@ -1717,6 +1717,12 @@ class RSSUtils { $scheduler = Scheduler::getInstance(); + $scheduler->add_scheduled_task('purge_orphaned_scheduled_tasks', '@weekly', + function() use ($scheduler) { + return $scheduler->purge_orphaned_tasks(); + } + ); + $scheduler->add_scheduled_task('purge_orphans', '@daily', function() { Article::_purge_orphans(); diff --git a/classes/Scheduler.php b/classes/Scheduler.php index fd6301641..f86feed65 100644 --- a/classes/Scheduler.php +++ b/classes/Scheduler.php @@ -120,5 +120,30 @@ class Scheduler { Debug::log("Processing scheduled tasks finished with $tasks_succeeded tasks succeeded and $tasks_failed tasks failed."); } - // TODO implement some sort of automatic cleanup for orphan task execution records -}
\ No newline at end of file + /** + * Purge records of scheduled tasks that aren't currently registered + * and haven't ran for a long time. + * + * @return int 0 if successful, 1 on failure + */ + function purge_orphaned_tasks(): int { + if (!$this->scheduled_tasks) { + Debug::log(__METHOD__ . ' was invoked before scheduled tasks have been registered. This should never happen.'); + return 1; + } + + $result = ORM::for_table('ttrss_scheduled_tasks') + ->where_not_in('task_name', array_keys($this->scheduled_tasks)) + ->where_raw("last_run < NOW() - INTERVAL '5 weeks'") + ->delete_many(); + + if ($result) { + $deleted_count = ORM::get_last_statement()->rowCount(); + + if ($deleted_count) + Debug::log("Purged {$deleted_count} orphaned scheduled tasks."); + } + + return $result ? 0 : 1; + } +} |