From c9ff66e1724c3201e172ad6c7c026408d4daa4c4 Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Wed, 23 Jul 2014 22:43:19 +0200 Subject: [PATCH] remove stale threads, fix db-purge tasks and change signature(s) --- isso/controllers/comments.py | 8 +++----- isso/controllers/threads.py | 9 +++++++++ isso/queue/__init__.py | 4 ++++ isso/queue/tasks/db.py | 17 ++++++++++++++--- isso/tests/controllers/test_comments.py | 4 ++-- isso/tests/controllers/test_threads.py | 11 +++++++++++ 6 files changed, 43 insertions(+), 10 deletions(-) diff --git a/isso/controllers/comments.py b/isso/controllers/comments.py index 0e48d00..2aa905f 100644 --- a/isso/controllers/comments.py +++ b/isso/controllers/comments.py @@ -281,9 +281,7 @@ class Controller(object): """ now = time.time() - self.db.engine.execute( - self.db.comments.delete() + return self.db.engine.execute(self.db.comments + .delete() .where(self.db.comments.c.mode == 2) - .where(now - self.db.comments.c.created > delta)) - - return + .where(now - self.db.comments.c.created > delta)).rowcount diff --git a/isso/controllers/threads.py b/isso/controllers/threads.py index 9479084..904a614 100644 --- a/isso/controllers/threads.py +++ b/isso/controllers/threads.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals +from sqlalchemy.sql import select, not_ + from isso.models import Thread @@ -34,3 +36,10 @@ class Controller(object): self.db.engine.execute( self.db.threads.delete().where(self.db.threads.c.id == thread.id)) + + def prune(self): + + return self.db.engine.execute(self.db.threads + .delete() + .where(not_(self.db.threads.c.id.in_( + select([self.db.comments.c.thread]))))).rowcount diff --git a/isso/queue/__init__.py b/isso/queue/__init__.py index 02b547e..c88e14a 100644 --- a/isso/queue/__init__.py +++ b/isso/queue/__init__.py @@ -197,6 +197,10 @@ class Task(object): def run(self, data): return + @classmethod + def __subclasshook__(cls, subclass): + return cls is Cron + class Cron(Task): diff --git a/isso/queue/tasks/db.py b/isso/queue/tasks/db.py index 4145dbd..181557b 100644 --- a/isso/queue/tasks/db.py +++ b/isso/queue/tasks/db.py @@ -1,6 +1,9 @@ # -*- encoding: utf-8 -*- -from isso.controllers import comments +import logging +log = logging.getLogger("isso") + +from isso.controllers import threads, comments from . import Cron @@ -9,8 +12,16 @@ class Purge(Cron): def __init__(self, db, after): super(Purge, self).__init__(hours=1) - self.comments = comments.Controller(db) self.after = after + self.threads = threads.Controller(db) + self.comments = comments.Controller(db) + def run(self, data): - self.comments.purge(self.after) + rows = self.comments.prune(self.after) + if rows: + log.info("removed %s comment(s)", rows) + + rows = self.threads.prune() + if rows: + log.info("removed %s thread(s)", rows) diff --git a/isso/tests/controllers/test_comments.py b/isso/tests/controllers/test_comments.py index b9141a2..cc72d5e 100644 --- a/isso/tests/controllers/test_comments.py +++ b/isso/tests/controllers/test_comments.py @@ -229,8 +229,8 @@ class TestController(unittest.TestCase): def test_prune(self): c = self.controller.new(IP, TH, dict(text="..."), moderated=True) - self.controller.prune(42) + self.assertEqual(self.controller.prune(42), 0) self.assertIsNotNone(self.controller.get(c.id)) - self.controller.prune(0) + self.assertEqual(self.controller.prune(0), 1) self.assertIsNone(self.controller.get(c.id)) diff --git a/isso/tests/controllers/test_threads.py b/isso/tests/controllers/test_threads.py index 632f9bb..360b2b9 100644 --- a/isso/tests/controllers/test_threads.py +++ b/isso/tests/controllers/test_threads.py @@ -54,3 +54,14 @@ class TestController(unittest.TestCase): self.assertEqual(self.comments.count(th), [0]) self.assertEqual(self.comments.count(cg), [3]) + + def test_prune_empty_threads(self): + th = self.threads.new("/", None) + comment = self.comments.new("127.0.0.1", th, dict(text="...")) + + self.assertEqual(self.threads.prune(), 0) + self.assertIsNotNone(self.threads.get(th.uri)) + + self.comments.delete(comment.id) + self.assertEqual(self.threads.prune(), 1) + self.assertIsNone(self.threads.get(th.uri))