remove stale threads, fix db-purge tasks and change signature(s)
This commit is contained in:
parent
95dba92d46
commit
c9ff66e172
@ -281,9 +281,7 @@ class Controller(object):
|
|||||||
"""
|
"""
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
|
||||||
self.db.engine.execute(
|
return self.db.engine.execute(self.db.comments
|
||||||
self.db.comments.delete()
|
.delete()
|
||||||
.where(self.db.comments.c.mode == 2)
|
.where(self.db.comments.c.mode == 2)
|
||||||
.where(now - self.db.comments.c.created > delta))
|
.where(now - self.db.comments.c.created > delta)).rowcount
|
||||||
|
|
||||||
return
|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from sqlalchemy.sql import select, not_
|
||||||
|
|
||||||
from isso.models import Thread
|
from isso.models import Thread
|
||||||
|
|
||||||
|
|
||||||
@ -34,3 +36,10 @@ class Controller(object):
|
|||||||
|
|
||||||
self.db.engine.execute(
|
self.db.engine.execute(
|
||||||
self.db.threads.delete().where(self.db.threads.c.id == thread.id))
|
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):
|
def run(self, data):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __subclasshook__(cls, subclass):
|
||||||
|
return cls is Cron
|
||||||
|
|
||||||
|
|
||||||
class Cron(Task):
|
class Cron(Task):
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
from isso.controllers import comments
|
import logging
|
||||||
|
log = logging.getLogger("isso")
|
||||||
|
|
||||||
|
from isso.controllers import threads, comments
|
||||||
|
|
||||||
from . import Cron
|
from . import Cron
|
||||||
|
|
||||||
@ -9,8 +12,16 @@ class Purge(Cron):
|
|||||||
|
|
||||||
def __init__(self, db, after):
|
def __init__(self, db, after):
|
||||||
super(Purge, self).__init__(hours=1)
|
super(Purge, self).__init__(hours=1)
|
||||||
self.comments = comments.Controller(db)
|
|
||||||
self.after = after
|
self.after = after
|
||||||
|
|
||||||
|
self.threads = threads.Controller(db)
|
||||||
|
self.comments = comments.Controller(db)
|
||||||
|
|
||||||
def run(self, data):
|
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):
|
def test_prune(self):
|
||||||
|
|
||||||
c = self.controller.new(IP, TH, dict(text="..."), moderated=True)
|
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.assertIsNotNone(self.controller.get(c.id))
|
||||||
|
|
||||||
self.controller.prune(0)
|
self.assertEqual(self.controller.prune(0), 1)
|
||||||
self.assertIsNone(self.controller.get(c.id))
|
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(th), [0])
|
||||||
self.assertEqual(self.comments.count(cg), [3])
|
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…
Reference in New Issue
Block a user