Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions chancy/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ def __init__(

# Set once the notifications listener is ready.
self._notifications_ready_event = asyncio.Event()
# Set when a queue may have changed. Unlike waiting on the hub, it
# remembers a change announced while the queues are being polled.
self._queues_changed = asyncio.Event()
# The queues that the worker is currently processing.
self._queues: dict[str, Queue] = {}
# The executors that the worker is currently using.
Expand Down Expand Up @@ -259,6 +262,8 @@ async def start(self):

self.hub.on("job.cancelled", self._handle_cancellation)
self.hub.on("queue.pushed", self._handle_queue_pushed)
for event in ("queue.declared", "queue.paused", "queue.resumed"):
self.hub.on(event, self._handle_queues_changed)

for plugin in self.chancy.plugins.values():
if plugin.get_scope() != PluginScope.WORKER:
Expand Down Expand Up @@ -343,6 +348,7 @@ async def _maintain_queues(self):
while True:
try:
self.chancy.log.debug("Polling for queue changes.")
self._queues_changed.clear()
tags = self.worker_tags()
# Tags in the database is a list of regexes, while the
# tags in the worker are a set of strings. We need to
Expand Down Expand Up @@ -397,10 +403,8 @@ async def _maintain_queues(self):
continue

try:
await self.hub.wait_for(
["queue.declared", "queue.paused", "queue.resumed"],
timeout=self.queue_change_poll_interval,
)
async with asyncio.timeout(self.queue_change_poll_interval):
await self._queues_changed.wait()
except TimeoutError:
pass

Expand Down Expand Up @@ -1128,6 +1132,9 @@ async def _handle_cancellation(self, event: Event):
for executor in self._executors.values():
await executor.cancel(Reference(event.body["j"]))

async def _handle_queues_changed(self, event: Event):
self._queues_changed.set()

async def _handle_queue_pushed(self, event: Event):
q = event.body["q"]
if q not in self._queues:
Expand Down
38 changes: 38 additions & 0 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,44 @@ async def broken_flush():
assert task.cancelled()


@pytest.mark.asyncio
async def test_queue_declared_during_poll_is_not_missed(
chancy: Chancy, worker_no_start: Worker
):
"""
A queue declared while the worker is polling for queue changes must be
picked up right away, not at the next ``queue_change_poll_interval``.
"""
original = chancy.get_all_queues
polled = asyncio.Event()
release = asyncio.Event()

async def slow_get_all_queues():
queues = await original()
if not polled.is_set():
polled.set()
await release.wait()
return queues

chancy.get_all_queues = slow_get_all_queues
seen = []
worker_no_start.hub.on("queue.declared", lambda e: seen.append(e.body["q"]))

async with worker_no_start as worker:
await polled.wait()
await chancy.declare(Queue("late"))
# The notification arrives while the worker still holds the result of
# a poll that predates the declaration.
async with asyncio.timeout(5):
while "late" not in seen:
await asyncio.sleep(0.01)
release.set()

async with asyncio.timeout(3):
while "late" not in worker._queues:
await asyncio.sleep(0.05)


@pytest.mark.asyncio
async def test_immediate_processing(chancy: Chancy, worker: Worker):
"""
Expand Down
Loading