remove stale threads, fix db-purge tasks and change signature(s)

pull/108/head
Martin Zimmermann 10 years ago
parent 95dba92d46
commit c9ff66e172

@ -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

@ -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

@ -197,6 +197,10 @@ class Task(object):
def run(self, data):
return
@classmethod
def __subclasshook__(cls, subclass):
return cls is Cron
class Cron(Task):

@ -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)

@ -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))

@ -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))

Loading…
Cancel
Save