39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
"""The background worker process (ADR-55).
|
|||
|
|
|
||
|
|
A1 ships the process shell only: it starts, logs, stops cleanly on SIGTERM and shows up as a
|
||
|
|
compose service. The round scheduler (A4) and the chain tasks (Phase B) plug into `tick`.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import logging
|
||
|
|
import signal
|
||
|
|
import threading
|
||
|
|
|
||
|
|
from django.core.management.base import BaseCommand
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
TICK_SECONDS = 5
|
||
|
|
|
||
|
|
|
||
|
|
def tick() -> None:
|
||
|
|
"""One pass over everything the worker is responsible for. Empty until A4."""
|
||
|
|
|
||
|
|
|
||
|
|
class Command(BaseCommand):
|
||
|
|
help = "Run the background worker (round scheduler and chain tasks)."
|
||
|
|
|
||
|
|
def add_arguments(self, parser):
|
||
|
|
parser.add_argument("--once", action="store_true", help="Run a single tick and exit.")
|
||
|
|
|
||
|
|
def handle(self, *args, **options):
|
||
|
|
stop = threading.Event()
|
||
|
|
for signum in (signal.SIGTERM, signal.SIGINT):
|
||
|
|
signal.signal(signum, lambda *_: stop.set())
|
||
|
|
|
||
|
|
logger.info("worker started")
|
||
|
|
while not stop.is_set():
|
||
|
|
tick()
|
||
|
|
if options["once"]:
|
||
|
|
break
|
||
|
|
stop.wait(TICK_SECONDS)
|
||
|
|
logger.info("worker stopped")
|