ArchiveBox/archivebox/services/machine_service.py
Nick Sweeting 6ff3d344ea
Some checks failed
Build Docker image / buildx (push) Waiting to run
Run linters / lint (push) Waiting to run
Build Pip package / build (push) Waiting to run
Release State / release-state (push) Waiting to run
Parallel Tests / Discover test files (push) Waiting to run
Parallel Tests / ${{ matrix.test.name }} (push) Blocked by required conditions
Parallel Tests / ${{ matrix.plugin.name }} (push) Blocked by required conditions
Run tests / python_tests (ubuntu-22.04, 3.13) (push) Waiting to run
Run tests / docker_tests (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, python) (push) Has been cancelled
Fix crawl event flow and UI archiving
2026-05-15 23:01:45 -07:00

37 lines
1.1 KiB
Python

from __future__ import annotations
from asgiref.sync import sync_to_async
from abx_dl.events import MachineEvent
from abx_dl.services.base import BaseService
class MachineService(BaseService):
LISTENS_TO = [MachineEvent]
EMITS = []
def __init__(self, bus):
super().__init__(bus)
self.bus.on(MachineEvent, self.on_MachineEvent__save_to_db)
async def on_MachineEvent__save_to_db(self, event: MachineEvent) -> None:
from archivebox.machine.models import Machine, _sanitize_machine_config
if event.config_type != "derived":
return
machine = await sync_to_async(Machine.current, thread_sensitive=True)()
config = dict(machine.config or {})
if event.config is not None:
config.update(_sanitize_machine_config(event.config))
elif event.method == "update":
key = event.key.replace("config/", "", 1).strip()
if key:
config[key] = event.value
else:
return
machine.config = _sanitize_machine_config(config)
await machine.asave(update_fields=["config", "modified_at"])