Move webhook delivery to background task queue #1690

Open
AI-Manager wants to merge 1 commits from feature/webhook-task-queue into main
Owner

Summary

  • Introduces a lightweight in-process task queue (SPARC/task_queue.py) using a daemon thread and queue.Queue for non-blocking webhook delivery
  • Adds enqueue_notify, enqueue_job_completed, and enqueue_alert to webhooks.py — these return immediately after placing tasks on the queue
  • Updates _run_batch_job in api.py and the scheduler to use the non-blocking enqueue variants
  • Worker thread preserves existing exponential-backoff retry logic (_send_with_retry)
  • Worker is started/stopped via the FastAPI lifespan (startup/shutdown events)
  • No new external dependencies required (no Redis)

Changes

File Change
SPARC/task_queue.py NewWebhookTask, worker lifecycle, enqueue(), drain()
SPARC/webhooks.py Add enqueue_notify, enqueue_job_completed, enqueue_alert
SPARC/api.py Start/stop webhook worker in lifespan; use enqueue_job_completed in _run_batch_job
SPARC/scheduler.py Fire enqueue_alert when patent count change threshold is exceeded
tests/test_task_queue.py New — 13 tests covering worker lifecycle, async delivery, retry in worker context, integration

Test plan

  • All 22 existing webhook unit tests pass unchanged
  • 13 new tests verify: worker start/stop, FIFO processing, non-blocking enqueue, retry logic in worker thread, enqueue helpers, Slack formatting, no-op when no URLs configured
  • Integration tests verify queued tasks are eventually delivered (mocked HTTP)

Closes #1676

🤖 Generated with Claude Code

## Summary - Introduces a lightweight in-process task queue (`SPARC/task_queue.py`) using a daemon thread and `queue.Queue` for non-blocking webhook delivery - Adds `enqueue_notify`, `enqueue_job_completed`, and `enqueue_alert` to `webhooks.py` — these return immediately after placing tasks on the queue - Updates `_run_batch_job` in `api.py` and the scheduler to use the non-blocking enqueue variants - Worker thread preserves existing exponential-backoff retry logic (`_send_with_retry`) - Worker is started/stopped via the FastAPI lifespan (startup/shutdown events) - No new external dependencies required (no Redis) ## Changes | File | Change | |------|--------| | `SPARC/task_queue.py` | **New** — `WebhookTask`, worker lifecycle, `enqueue()`, `drain()` | | `SPARC/webhooks.py` | Add `enqueue_notify`, `enqueue_job_completed`, `enqueue_alert` | | `SPARC/api.py` | Start/stop webhook worker in lifespan; use `enqueue_job_completed` in `_run_batch_job` | | `SPARC/scheduler.py` | Fire `enqueue_alert` when patent count change threshold is exceeded | | `tests/test_task_queue.py` | **New** — 13 tests covering worker lifecycle, async delivery, retry in worker context, integration | ## Test plan - [x] All 22 existing webhook unit tests pass unchanged - [x] 13 new tests verify: worker start/stop, FIFO processing, non-blocking enqueue, retry logic in worker thread, enqueue helpers, Slack formatting, no-op when no URLs configured - [x] Integration tests verify queued tasks are eventually delivered (mocked HTTP) Closes #1676 🤖 Generated with [Claude Code](https://claude.com/claude-code)
AI-Manager added 1 commit 2026-05-19 15:26:31 +00:00
Introduce a lightweight in-process task queue (thread + queue.Queue) so
that webhook HTTP delivery no longer blocks the scheduler or batch-job
background tasks.  The worker thread preserves the existing exponential-
backoff retry logic from _send_with_retry.

- Add SPARC/task_queue.py: WebhookTask, start/stop worker, enqueue, drain
- Add enqueue_notify / enqueue_job_completed / enqueue_alert to webhooks.py
- Update api.py lifespan to start/stop the webhook worker
- Update _run_batch_job to use enqueue_job_completed (non-blocking)
- Update scheduler to fire enqueue_alert on patent count changes
- Add 13 tests covering worker lifecycle, async delivery, retry in worker
  context, and integration via enqueue helpers
- All 22 existing webhook tests continue to pass unchanged

Closes leeworks-agents/SPARC#1676

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feature/webhook-task-queue:feature/webhook-task-queue
git checkout feature/webhook-task-queue
Sign in to join this conversation.