From: Michael Tremer Date: Tue, 7 May 2019 16:44:37 +0000 (+0100) Subject: Cleanup sessions, accounts and the message queue once an hour X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8e69850a9f9a948d5e5f0034213859a5a6afdbd2;p=ipfire.org.git Cleanup sessions, accounts and the message queue once an hour Signed-off-by: Michael Tremer --- diff --git a/src/backend/accounts.py b/src/backend/accounts.py index f6b7dc6c..74b5ff00 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -212,12 +212,7 @@ class Accounts(Object): # Session stuff - def _cleanup_expired_sessions(self): - self.db.execute("DELETE FROM sessions WHERE time_expires <= NOW()") - def create_session(self, account, host): - self._cleanup_expired_sessions() - res = self.db.get("INSERT INTO sessions(host, uid) VALUES(%s, %s) \ RETURNING session_id, time_expires", host, account.uid) @@ -234,7 +229,6 @@ class Accounts(Object): self.db.execute("DELETE FROM sessions \ WHERE session_id = %s AND host = %s", session_id, host) - self._cleanup_expired_sessions() def get_by_session(self, session_id, host): logging.debug("Looking up session %s" % session_id) @@ -253,6 +247,13 @@ class Accounts(Object): return self.get_by_uid(res.uid) + def cleanup(self): + # Cleanup expired sessions + self.db.execute("DELETE FROM sessions WHERE time_expires <= NOW()") + + # Cleanup expired account activations + self.db.execute("DELETE FROM account_activations WHERE expires_at <= NOW()") + class Account(Object): def __init__(self, backend, dn, attrs=None): diff --git a/src/backend/base.py b/src/backend/base.py index 6c79d8bf..f11b5ad5 100644 --- a/src/backend/base.py +++ b/src/backend/base.py @@ -90,7 +90,7 @@ class Backend(object): def run_task(self, task, *args, **kwargs): tasks = { "check-mirrors" : self.mirrors.check_all, - "cleanup-messages" : self.messages.queue.cleanup, + "cleanup" : self.cleanup, "scan-files" : self.releases.scan_files, "send-all-messages" : self.messages.queue.send_all, "tweet" : self.tweets.tweet, @@ -117,3 +117,13 @@ class Backend(object): @lazy_property def tweets(self): return tweets.Tweets(self) + + @tornado.gen.coroutine + def cleanup(self): + # Cleanup message queue + with self.db.transaction(): + self.messages.queue.cleanup() + + # Cleanup in accounts + with self.db.transaction(): + self.accounts.cleanup() diff --git a/src/backend/messages.py b/src/backend/messages.py index 1cf90690..59942c5a 100644 --- a/src/backend/messages.py +++ b/src/backend/messages.py @@ -219,7 +219,6 @@ class Queue(misc.Object): self.db.execute("UPDATE messages SET time_sent = NOW() \ WHERE id = %s", message.id) - @tornado.gen.coroutine def cleanup(self): logging.debug("Cleaning up message queue") diff --git a/src/crontab/ipfire.org b/src/crontab/ipfire.org index b5b74598..5c02a6e4 100644 --- a/src/crontab/ipfire.org +++ b/src/crontab/ipfire.org @@ -7,8 +7,8 @@ # Send messages * * * * * nobody ipfire.org send-all-messages -# Cleanup once a an hour -30 * * * * nobody ipfire.org cleanup-messages +# Cleanup once an hour +30 * * * * nobody ipfire.org cleanup # Check mirrors once every 30 min */30 * * * * nobody ipfire.org check-mirrors