diff --git a/scrapyd/sqlite.py b/scrapyd/sqlite.py index 7629b9c0..fb140170 100644 --- a/scrapyd/sqlite.py +++ b/scrapyd/sqlite.py @@ -98,8 +98,8 @@ def __init__(self, database=None, table="queue"): q = "create table if not exists %s (id integer primary key, " \ "priority real key, message blob, insert_time TIMESTAMP)" % table self.conn.execute(q) - # Backward compatibility for scrapyd<1.3.0 - self.ensure_insert_time_column() + self.ensure_insert_time_column() # Backward compatibility for scrapyd<1.3.0 + self.create_triggers() self.update_project_priority_map() def put(self, message, priority=0.0): @@ -108,14 +108,12 @@ def put(self, message, priority=0.0): % self.table self.conn.execute(q, args) self.conn.commit() - self.update_project_priority_map() def pop(self): q = "select id, message from %s order by priority desc limit 1" \ % self.table idmsg = self.conn.execute(q).fetchone() if idmsg is None: - self.update_project_priority_map() return id, msg = idmsg q = "delete from %s where id=?" % self.table @@ -124,7 +122,6 @@ def pop(self): self.conn.rollback() return self.pop() self.conn.commit() - self.update_project_priority_map() return self.decode(msg) def remove(self, func): @@ -139,13 +136,11 @@ def remove(self, func): return self.remove(func) n += 1 self.conn.commit() - self.update_project_priority_map() return n def clear(self): self.conn.execute("delete from %s" % self.table) self.conn.commit() - self.update_project_priority_map() def ensure_insert_time_column(self): q = "SELECT sql FROM sqlite_master WHERE type='table' AND name='%s'" % self.table @@ -156,6 +151,17 @@ def ensure_insert_time_column(self): self.conn.execute(q) self.conn.commit() + def create_triggers(self): + self.conn.create_function("update_project_priority_map", 0, self.update_project_priority_map) + for action in ['INSERT', 'UPDATE', 'DELETE']: + name = 'trigger_on_%s' % action.lower() + self.conn.execute(""" + CREATE TRIGGER IF NOT EXISTS %s AFTER %s ON %s + BEGIN + SELECT update_project_priority_map(); + END; + """ % (name, action, self.table)) + def update_project_priority_map(self): q = "select priority, strftime('%%s', insert_time) from %s order by priority desc limit 1" \ % self.table