askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
materialize_run.py · 4.1 KB · 108 lines
| 1 | """materialize_run -- manual "Run now" for one materialize_definitions row. |
| 2 | |
| 3 | Implements plan/vocabulary/61-materialize-spec.md's Surfaces/Events |
| 4 | section: a system object exposed as a slot button (window.dbbasicSlots, |
| 5 | see objects/site/materialize_run_button.py) on the materialize_definitions |
| 6 | detail page. Fires the IDENTICAL per-source-row generation function the |
| 7 | daemon's scheduled pass (object_daemon.process_materializations) and the |
| 8 | event handler (materialize_seed) both call |
| 9 | (object_materialize.generate_definition) -- synchronously, scoped to ONE |
| 10 | definition -- so running it twice is safe by construction, never a |
| 11 | separate "are you sure" code path. |
| 12 | |
| 13 | Also the primary interface for trigger.mode: "event" definitions when |
| 14 | DBBASIC_ENABLE_EVENT_HANDLERS is off: generate_definition treats an |
| 15 | event-mode definition as "apply this definition to every currently |
| 16 | matching source row once each," which is exactly the catch-up behavior |
| 17 | an operator wants when dispatch isn't running. |
| 18 | |
| 19 | Degradation, per 61's own section: refuses when the block-wide |
| 20 | materialize_enabled flag is off (no bypass of an operator's explicit |
| 21 | kill switch -- "off means off, full stop, for BOTH the scheduled and |
| 22 | manual paths"), when the definition itself is block: true (a distinct |
| 23 | "blocked, not merely paused" error), or enabled: false. |
| 24 | """ |
| 25 | from __future__ import annotations |
| 26 | |
| 27 | import os |
| 28 | from datetime import datetime, timezone |
| 29 | |
| 30 | import object_materialize |
| 31 | import object_records |
| 32 | |
| 33 | DATA_DIR_ENV = "DBBASIC_DATA_DIR" |
| 34 | |
| 35 | |
| 36 | def _data_dir() -> str: |
| 37 | return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR) |
| 38 | |
| 39 | |
| 40 | def _now_iso() -> str: |
| 41 | return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") |
| 42 | |
| 43 | |
| 44 | def POST(request): |
| 45 | definition_id = str((request or {}).get("definition_id") or "").strip() |
| 46 | if not definition_id: |
| 47 | return {"status": "error", "error": "definition_id is required"} |
| 48 | |
| 49 | base_dir = _data_dir() |
| 50 | |
| 51 | if not object_materialize.materialize_pass_enabled(base_dir=base_dir): |
| 52 | return { |
| 53 | "status": "error", |
| 54 | "error": "materialize_enabled flag is off -- manual run refuses to bypass a " |
| 55 | "block-wide kill switch an operator explicitly set", |
| 56 | } |
| 57 | |
| 58 | try: |
| 59 | definition = object_records.get_collection_record( |
| 60 | object_materialize.MATERIALIZE_DEFINITIONS_COLLECTION, definition_id, base_dir=base_dir, |
| 61 | ) |
| 62 | except object_records.RecordNotFoundError: |
| 63 | return {"status": "error", "error": f"no such materialize definition: {definition_id}"} |
| 64 | except object_records.InvalidRecordIdError: |
| 65 | return {"status": "error", "error": f"invalid definition id: {definition_id}"} |
| 66 | |
| 67 | if object_materialize.is_definition_blocked(definition): |
| 68 | return { |
| 69 | "status": "error", |
| 70 | "error": "definition is blocked, not merely paused -- fix and clear block before running", |
| 71 | } |
| 72 | if not object_materialize.is_definition_enabled(definition): |
| 73 | return {"status": "error", "error": "definition is disabled (enabled: false)"} |
| 74 | |
| 75 | try: |
| 76 | result = object_materialize.generate_definition(definition, base_dir=base_dir) |
| 77 | except object_materialize.DefinitionError as exc: |
| 78 | return {"status": "error", "error": str(exc)} |
| 79 | |
| 80 | try: |
| 81 | object_records.update_collection_record( |
| 82 | object_materialize.MATERIALIZE_DEFINITIONS_COLLECTION, |
| 83 | definition_id, |
| 84 | {"last_run_at": _now_iso()}, |
| 85 | base_dir=base_dir, |
| 86 | actor=definition.get("actor") or object_materialize.DEFAULT_ACTOR, |
| 87 | preserve_read_only=True, |
| 88 | ) |
| 89 | except Exception: |
| 90 | # The generation itself already succeeded and is live in |
| 91 | # output_collection; a failed last_run_at stamp only means this |
| 92 | # definition looks "due" again next scheduled pass -- harmless. |
| 93 | pass |
| 94 | |
| 95 | return { |
| 96 | "status": "ok", |
| 97 | "checked": result["checked"], |
| 98 | "generated": result["generated"], |
| 99 | "skipped_already_generated": result["skipped_already_generated"], |
| 100 | "errors": result["errors"], |
| 101 | } |
| 102 | |
| 103 | |
| 104 | def GET(request): |
| 105 | return { |
| 106 | "status": "ok", |
| 107 | "description": "POST {\"definition_id\": \"...\"} to run one materialize_definitions row now.", |
| 108 | } |