refactor: enhance webhook delivery queue processing with configurable blocking
This commit is contained in:
43
scripts/rq
Executable file
43
scripts/rq
Executable file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python
|
||||
"""Top-level RQ helper entrypoint."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT_DIR = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT_DIR))
|
||||
|
||||
from scripts.rq_webhook import cmd_worker
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="Generic background worker helpers.")
|
||||
subparsers = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
worker_parser = subparsers.add_parser(
|
||||
"worker",
|
||||
help="Run background queue worker loop.",
|
||||
)
|
||||
worker_parser.add_argument(
|
||||
"--interval-seconds",
|
||||
type=float,
|
||||
default=float(os.environ.get("WEBHOOK_WORKER_INTERVAL_SECONDS", "2")),
|
||||
help="Seconds to wait for queue work before returning when idle.",
|
||||
)
|
||||
worker_parser.set_defaults(func=cmd_worker)
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args()
|
||||
sys.exit(args.func(args))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
57
scripts/rq_webhook.py
Normal file
57
scripts/rq_webhook.py
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python
|
||||
"""RQ helpers for background queue processing."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT_DIR = Path(__file__).resolve().parent.parent
|
||||
BACKEND_ROOT = ROOT_DIR / "backend"
|
||||
sys.path.insert(0, str(BACKEND_ROOT))
|
||||
|
||||
from app.services.webhooks.dispatch import flush_webhook_delivery_queue
|
||||
|
||||
|
||||
def cmd_worker(args: argparse.Namespace) -> int:
|
||||
interval_seconds = max(args.interval_seconds, 0.0)
|
||||
try:
|
||||
while True:
|
||||
asyncio.run(
|
||||
flush_webhook_delivery_queue(
|
||||
block=True,
|
||||
block_timeout=interval_seconds,
|
||||
),
|
||||
)
|
||||
except KeyboardInterrupt:
|
||||
return 0
|
||||
return 0
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="Background RQ helper commands.")
|
||||
subparsers = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
worker_parser = subparsers.add_parser("worker", help="Continuously process queued work.")
|
||||
worker_parser.add_argument(
|
||||
"--interval-seconds",
|
||||
type=float,
|
||||
default=float(os.environ.get("WEBHOOK_WORKER_INTERVAL_SECONDS", "2")),
|
||||
help="Seconds to wait for queue work before returning when idle.",
|
||||
)
|
||||
worker_parser.set_defaults(func=cmd_worker)
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args()
|
||||
sys.exit(args.func(args))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user