Skip to content

Commit

Permalink
Introduce create_triggers()
Browse files Browse the repository at this point in the history
  • Loading branch information
my8100 committed Jul 3, 2019
1 parent 0343475 commit 200066b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions scrapyd/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 200066b

Please sign in to comment.