From 82de36dbc904e12d6596cf259759caa3330d1276 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 30 Oct 2011 12:46:55 -0400 Subject: [PATCH] Database access lock, better kwarg usage, sync command. --- bot/commands/threads.py | 1 + bot/tasks/afc_statistics.py | 76 ++++++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/bot/commands/threads.py b/bot/commands/threads.py index f3f6a4f..b3e6c36 100644 --- a/bot/commands/threads.py +++ b/bot/commands/threads.py @@ -132,6 +132,7 @@ class Command(BaseCommand): self.connection.reply(data, msg.format(task_name)) return + data.kwargs["fromIRC"] = True tasks.start(task_name, **data.kwargs) msg = "task \x0302{0}\x0301 started.".format(task_name) self.connection.reply(data, msg) diff --git a/bot/tasks/afc_statistics.py b/bot/tasks/afc_statistics.py index d6100c3..678d794 100644 --- a/bot/tasks/afc_statistics.py +++ b/bot/tasks/afc_statistics.py @@ -2,6 +2,7 @@ import re from os.path import expanduser +from threading import Lock import oursql @@ -38,34 +39,35 @@ class Task(BaseTask): kwargs = cfg.get("sql", {}) kwargs["read_default_file"] = expanduser("~/.my.cnf") self.conn = oursql.connect(**kwargs) + self.db_access_lock = Lock() def run(self, **kwargs): self.site = wiki.get_site() - + action = kwargs.get("action") if not action: return - if action == "save": - self.save() - return - page = kwargs.get("page") - if page: - methods = { - "edit": self.process_edit, - "move": self.process_move, - "delete": self.process_delete, - "restore": self.process_restore, - } - method = methods.get(action) - if method: - method(page) - - def save(self): - self.check_integrity() - - if self.shutoff_enabled(): - return + methods = { + "save": self.save, + "sync", self.sync, + "edit": self.process_edit, + "move": self.process_move, + "delete": self.process_delete, + "restore": self.process_restore, + } + + method = methods.get(action) + if method: + method(**kwargs) + + def save(self, **kwargs): + if kwargs.get("fromIRC"): + summary = " ".join((self.summary, "(!earwigbot)")) + else: + if self.shutoff_enabled(): + return + summary = self.summary statistics = self.compile_charts() @@ -79,11 +81,11 @@ class Task(BaseTask): newtext = re.sub("()(.*?)()", "\\1~~~ at ~~~~~\\3", newtext) - page.edit(newtext, self.summary, minor=True, bot=True) + page.edit(newtext, summary, minor=True, bot=True) def compile_charts(self): stats = "" - with self.conn.cursor() as cursor: + with self.conn.cursor() as cursor, self.db_access_lock: cursor.execute("SELECT * FROM chart") for chart in cursor: stats += self.compile_chart(chart) + "\n" @@ -126,17 +128,35 @@ class Task(BaseTask): def format_time(self, timestamp): return timestamp.strftime("%H:%M, %d %B %Y") - def check_integrity(self): + def sync(self, **kwargs): + with self.conn.cursor() as cursor, self.db_access_lock: + self.sync_deleted(cursor) # Remove deleted subs + self.sync_oldids(cursor) # Make sure all subs are up to date + self.sync_pending(cursor) # Add missed pending subs + self.sync_old(cursor) # Remove old declined and accepted subs + + def sync_deleted(self, cursor): pass - def process_edit(self, page): + def sync_oldids(self, cursor): + pass + + def sync_pending(self, cursor): + pass + + def sync_old(self, cursor): + query = """DELETE FROM page, row USING page JOIN row ON page_id = row_id + WHERE row_chart IN (4, 5) AND ADDTIME(page_special_time, '36:00:00') < NOW()""" + cursor.execute(query) + + def process_edit(self, page, **kwargs): pass - def process_move(self, page): + def process_move(self, page, **kwargs): pass - def process_delete(self, page): + def process_delete(self, page, **kwargs): pass - def process_restore(self, page): + def process_restore(self, page, **kwargs): pass