Skip to content
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(python backoff): fix bug when using builtin backoff type #2265

Merged
merged 3 commits into from
Nov 8, 2023
Merged
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
5 changes: 2 additions & 3 deletions python/bullmq/backoffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ def normalize(backoff: int | BackoffOptions):
async def calculate(backoff: BackoffOptions, attempts_made: int, err, job, customStrategy):
if backoff:
strategy = lookup_strategy(backoff, customStrategy)

return strategy(attempts_made, backoff.get("type"), err, job)


def lookup_strategy(backoff: BackoffOptions, custom_strategy):
backoff_type = backoff.get("type")
if backoff_type in Backoffs.builtin_strategies:
Backoffs.builtin_strategies[backoff.type](backoff.delay)
HalfdanJ marked this conversation as resolved.
Show resolved Hide resolved
return Backoffs.builtin_strategies[backoff_type](backoff.get("delay"))
elif custom_strategy:
return custom_strategy
else:
raise Exception(f"Unknown backoff strategy {backoff_type}.If a custom backoff strategy is used, specify it when the queue is created.")
raise Exception(f"Unknown backoff strategy {backoff_type}. If a custom backoff strategy is used, specify it when the queue is created.")
28 changes: 28 additions & 0 deletions python/tests/worker_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,34 @@ async def process2(job: Job, token: str):
await worker2.close(force=True)
await queue.close()

async def test_retry_job_after_delay_with_fixed_backoff(self):
HalfdanJ marked this conversation as resolved.
Show resolved Hide resolved
queue = Queue(queueName)

async def process1(job: Job, token: str):
if job.attemptsMade < 3:
raise Exception("Not yet!")
return None

worker = Worker(queueName, process1)

start = round(time.time() * 1000)
await queue.add("test", { "foo": "bar" },
{"attempts": 3, "backoff": {"type": "fixed", "delay": 1000}})

completed_events = Future()

def completing(job: Job, result):
elapse = round(time.time() * 1000) - start
self.assertGreater(elapse, 2000)
completed_events.set_result(None)

worker.on("completed", completing)

await completed_events

await queue.close()
await worker.close()

async def test_retry_job_after_delay_with_custom_backoff(self):
queue = Queue(queueName)

Expand Down