Skip to content

Commit

Permalink
chore(refactor): Access the spiderqueues via the Scheduler rather tha…
Browse files Browse the repository at this point in the history
…n the Poller

In principle, the Scheduler is more related to "pending" jobs than the Poller.
Both use the same SQLite databases by default, which support concurrency.
  • Loading branch information
jpmckinney committed Jul 19, 2024
1 parent bb523fd commit 76887a7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions scrapyd/webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def render_OPTIONS(self, txrequest):

class DaemonStatus(WsResource):
def render_GET(self, txrequest):
pending = sum(q.count() for q in self.root.poller.queues.values())
pending = sum(q.count() for q in self.root.scheduler.queues.values())
running = len(self.root.launcher.processes)
finished = len(self.root.launcher.finished)

Expand Down Expand Up @@ -135,12 +135,12 @@ class Cancel(WsResource):
# https://github.com/scrapy/scrapy/blob/06f9c28/tests/test_crawler.py#L886
@param("signal", required=False, default="INT" if sys.platform != "win32" else "BREAK")
def render_POST(self, txrequest, project, job, signal):
if project not in self.root.poller.queues:
if project not in self.root.scheduler.queues:
raise error.Error(code=http.OK, message=b"project '%b' not found" % project.encode())

prevstate = None

if self.root.poller.queues[project].remove(lambda x: x["_job"] == job):
if self.root.scheduler.queues[project].remove(lambda x: x["_job"] == job):
prevstate = "pending"

spiders = self.root.launcher.processes.values()
Expand Down Expand Up @@ -208,7 +208,7 @@ class Status(WsResource):
@param("project", required=False)
def render_GET(self, txrequest, job, project):
spiders = self.root.launcher.processes.values()
queues = self.root.poller.queues
queues = self.root.scheduler.queues

if project is not None and project not in queues:
raise error.Error(code=http.OK, message=b"project '%b' not found" % project.encode())
Expand Down Expand Up @@ -238,7 +238,7 @@ class ListJobs(WsResource):
@param("project", required=False)
def render_GET(self, txrequest, project):
spiders = self.root.launcher.processes.values()
queues = self.root.poller.queues
queues = self.root.scheduler.queues

if project is not None and project not in queues:
raise error.Error(code=http.OK, message=b"project '%b' not found" % project.encode())
Expand Down
2 changes: 1 addition & 1 deletion scrapyd/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def prep_tab_pending(self):
"Cancel": self.cancel_button(project=project, jobid=m["_job"], base_path=self.base_path),
}
)
for project, queue in self.root.poller.queues.items()
for project, queue in self.root.scheduler.queues.items()
for m in queue.list()
)

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FakeScheduler:
def __init__(self, config):
self.config = config
self.calls = []
self.queues = {}

def schedule(self, project, spider_name, priority=0.0, **spider_args):
self.calls.append([project, spider_name])
Expand Down

0 comments on commit 76887a7

Please sign in to comment.