summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2025-05-04 14:10:56 +0000
committerAndrew Dolgov <fox@fakecake.org>2025-05-04 14:10:56 +0000
commit07eb34529f27e410fb14f7ab394beb9923cc50d4 (patch)
treef1e856cdb81a930cb70d5014fe16f7d53cd11e68 /classes
parent8f9f06e7c07bab3459d765224826db1f8b2a728e (diff)
parent868385442a7444def86823e8f52eac1d526c34e0 (diff)
Merge branch 'feature/purge-orphaned-scheduled-tasks' into 'master'
Periodically purge orphaned scheduled task records See merge request tt-rss/tt-rss!126
Diffstat (limited to 'classes')
-rw-r--r--classes/RSSUtils.php6
-rw-r--r--classes/Scheduler.php29
2 files changed, 33 insertions, 2 deletions
diff --git a/classes/RSSUtils.php b/classes/RSSUtils.php
index b0cfc4823..6ef4860c6 100644
--- a/classes/RSSUtils.php
+++ b/classes/RSSUtils.php
@@ -1702,6 +1702,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;
+ }
+}