97 lines
3.2 KiB
Python
97 lines
3.2 KiB
Python
"""The background worker process (ADR-55): runs the round lifecycle every few seconds and, on the
|
|
simulated ledger, settles the confirmations that a real chain would bring (roadmap A4). The chain tasks
|
|
of phase B plug into `tick` too. A step that fails is logged and retried at the next tick: the lifecycle
|
|
keeps no state outside the database.
|
|
"""
|
|
|
|
import logging
|
|
import signal
|
|
import threading
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
TICK_SECONDS = 5
|
|
|
|
|
|
_last_scan = 0.0
|
|
SCAN_SECONDS = 10
|
|
|
|
|
|
def scan_chain_deposits() -> None:
|
|
"""DEP-20: every few seconds, on a real ledger only."""
|
|
import time
|
|
|
|
from django.conf import settings
|
|
|
|
from apps.lottery import deposits
|
|
from apps.lottery.electrum_chain import ElectrumChain
|
|
|
|
global _last_scan
|
|
if time.monotonic() - _last_scan < SCAN_SECONDS:
|
|
return
|
|
_last_scan = time.monotonic()
|
|
chain = ElectrumChain(settings.ELECTRUM_ENDPOINTS, settings.PLM_PROFILE, settings.ELECTRUM_ALLOW_NO_QUORUM)
|
|
done = deposits.scan_deposits(chain)
|
|
if any(done.values()):
|
|
logger.info("deposit scan: %s", done)
|
|
|
|
|
|
def process_chain_bets() -> None:
|
|
"""BET-31/32: sign what the web admitted, send what was signed. On a real ledger only."""
|
|
from django.conf import settings
|
|
|
|
from apps.lottery import bets, confirmation, rbf, reconcile
|
|
from apps.lottery.electrum_chain import ElectrumChain
|
|
|
|
chain = ElectrumChain(settings.ELECTRUM_ENDPOINTS, settings.PLM_PROFILE, settings.ELECTRUM_ALLOW_NO_QUORUM)
|
|
done = bets.process_bets(chain)
|
|
if any(done.values()):
|
|
logger.info("bets: %s", done)
|
|
confirmed = confirmation.poll_confirmations(chain) # B7: TX-30
|
|
bumped = rbf.bump_due(chain) # TX-32
|
|
reconciled = reconcile.reconcile_once(chain) # TX-33
|
|
if confirmed or bumped or any(reconciled.values()):
|
|
logger.info("transactions: confirmed=%s bumped=%s reconciled=%s", confirmed, bumped, reconciled)
|
|
|
|
|
|
def tick() -> None:
|
|
"""One pass over everything the worker is responsible for."""
|
|
from django.conf import settings
|
|
|
|
from apps.lottery import console, lifecycle, simulation
|
|
|
|
console.beat() # WEB-31: proof of life, before the work that might fail
|
|
if settings.LEDGER_BACKEND == "simulated":
|
|
simulation.settle()
|
|
else:
|
|
scan_chain_deposits() # B5
|
|
process_chain_bets() # B6
|
|
lifecycle.tick()
|
|
|
|
|
|
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():
|
|
try:
|
|
tick()
|
|
except Exception: # a failed step leaves the round where it was; the next tick retries
|
|
logger.exception("worker tick failed")
|
|
if options["once"]:
|
|
raise
|
|
if options["once"]:
|
|
break
|
|
stop.wait(TICK_SECONDS)
|
|
logger.info("worker stopped")
|