saq: Heartbeat-based task interruption not working as expected

Heartbeat-based task interruption not working as expected

I’ve noticed that tasks with a heartbeat are not being interrupted properly in the saq library. Here’s a minimal example demonstrating the issue:

import asyncio
from redis.asyncio import Redis
from saq import Queue, Worker

should_executed_once = 0

async def handler(ctx):
    global should_executed_once
    await asyncio.sleep(5)
    should_executed_once += 1

async def main():
    queue = Queue(Redis())
    worker = Worker(queue, [handler], timers={"sweep": 1})
    asyncio.create_task(worker.start())
    await asyncio.sleep(1)

    job1 = await queue.enqueue("handler", heartbeat=1)
    await job1.refresh(0)
    assert job1.status == "aborted"
    assert should_executed_once == 0

    job2 = await queue.enqueue("handler")
    await job2.refresh(0)

    assert should_executed_once == 2  # Should be 1

    await worker.stop()
    await worker.queue.disconnect()

asyncio.run(main())

The first job is set to abort due to a heartbeat timeout, but the handler still executes. I think the issue might be in the Queue.sweep function. Instead of just updating the status with await job.finish(Status.ABORTED, error="swept"), it should actually abort the task with await job.abort("swept") (or, possibly use a combination of these methods).

Why was the current implementation chosen? Are there any potential side effects of changing to job.abort()?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (11 by maintainers)

Most upvoted comments

02fb13c

so we try to abort it, if it does, the status can change, and then it will retry. you can check the retryable flag