-
Notifications
You must be signed in to change notification settings - Fork 569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix polling order in contrib modules #349
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from six import iteritems | ||
from twisted.internet.defer import inlineCallbacks, maybeDeferred, returnValue | ||
|
||
from scrapyd.poller import QueuePoller | ||
|
||
|
||
class FixQueuePoller(QueuePoller): | ||
|
||
@inlineCallbacks | ||
def poll(self): | ||
if not self.dq.waiting: | ||
return | ||
project_with_highest_priority = None | ||
for p, q in iteritems(self.queues): | ||
project_with_highest_priority = q.get_project_with_highest_priority() | ||
break | ||
if project_with_highest_priority: | ||
q = self.queues[project_with_highest_priority] | ||
msg = yield maybeDeferred(q.pop) | ||
if msg is not None: # In case of a concurrently accessed queue | ||
returnValue(self.dq.put(self._message(msg, project_with_highest_priority))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from scrapyd.spiderqueue import SqliteSpiderQueue | ||
from scrapyd.contrib.fix_poll_order.sqlite import FixJsonSqlitePriorityQueue | ||
|
||
|
||
class FixSqliteSpiderQueue(SqliteSpiderQueue): | ||
|
||
def __init__(self, database=None, table='spider_queue_with_triggers'): | ||
self.q = FixJsonSqlitePriorityQueue(database, table) | ||
|
||
def get_project_with_highest_priority(self): | ||
if self.q.project_priority_map: | ||
return sorted(self.q.project_priority_map, | ||
key=lambda x: self.q.project_priority_map[x], reverse=True)[0] | ||
else: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
import sqlite3 | ||
|
||
from scrapyd.sqlite import JsonSqlitePriorityQueue | ||
|
||
|
||
class FixJsonSqlitePriorityQueue(JsonSqlitePriorityQueue): | ||
"""SQLite priority queue. It relies on SQLite concurrency support for | ||
providing atomic inter-process operations. | ||
""" | ||
project_priority_map = {} | ||
|
||
def __init__(self, database=None, table="queue"): | ||
self.database = database or ':memory:' | ||
self.table = table | ||
if database: | ||
dbname = os.path.split(database)[-1] | ||
self.project = os.path.splitext(dbname)[0] | ||
else: | ||
self.project = self.database | ||
# about check_same_thread: http://twistedmatrix.com/trac/ticket/4040 | ||
self.conn = sqlite3.connect(self.database, check_same_thread=False) | ||
q = "create table if not exists %s (id integer primary key, " \ | ||
"priority real key, message blob, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP)" % table | ||
self.conn.execute(q) | ||
self.create_triggers() | ||
self.update_project_priority_map() | ||
|
||
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 | ||
result = self.conn.execute(q).fetchone() | ||
if result is None: | ||
self.project_priority_map.pop(self.project, None) | ||
else: | ||
self.project_priority_map[self.project] = (result[0], -int(result[-1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should do something similar to Launcher, etc. so that users can select any module (including their own), not just between these two modules.