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_seed.py · 4.1 KB · 92 lines
| 1 | """materialize_seed -- event-mode (CreateWork) dispatch handler. |
| 2 | |
| 3 | Implements plan/vocabulary/61-materialize-spec.md's Events section 2: |
| 4 | declares HANDLES for every distinct source_collection value across |
| 5 | enabled, non-blocked, trigger.mode == "event" materialize_definitions |
| 6 | rows. Registered as a Phase-5a event handler (object_handlers.py, |
| 7 | docs/event-hooks-decisions.md); the platform's post-commit dispatcher |
| 8 | executes this object's EVENT method whenever a client-facing write |
| 9 | commits to a HANDLES-listed collection, only when the operator has |
| 10 | DBBASIC_ENABLE_EVENT_HANDLERS set. With the flag unset (the default), |
| 11 | event-mode definitions simply never receive dispatches -- the record is |
| 12 | created unseeded, same as pre-materialize behavior exactly (61's |
| 13 | Degradation); materialize_run (manual) still works and can be fired by |
| 14 | hand on that record's id. |
| 15 | |
| 16 | HANDLES below is an empty static list literal, and in v1 NOTHING keeps it |
| 17 | in sync with the event-mode definition set: this codebase's HANDLES |
| 18 | mechanism (object_handlers.extract_handles) is a pure AST parse of a |
| 19 | module-level list literal, and the platform deliberately does NOT rewrite |
| 20 | an installed object's source at runtime to track the definitions |
| 21 | (rewriting an existing object under the daemon poll loop is the wrong |
| 22 | shape -- expensive, surprising, self-modifying). So automatic on-create |
| 23 | dispatch is effectively off in v1 and every event-mode definition runs via |
| 24 | the manual path (materialize_run) -- exactly 61's degrade-to-manual |
| 25 | posture, just made the always-on default rather than only the |
| 26 | DBBASIC_ENABLE_EVENT_HANDLERS-unset case. |
| 27 | |
| 28 | The EVENT method below is fully implemented and tested and would dispatch |
| 29 | correctly the moment HANDLES named a collection -- left ready for a future |
| 30 | DELIBERATE, scheduled, separately-tested HANDLES-sync job (whose input is |
| 31 | object_materialize.compute_event_handles), or a hand-edited wiring, rather |
| 32 | than a per-pass source rewrite. Until then this object is inert. |
| 33 | """ |
| 34 | from __future__ import annotations |
| 35 | |
| 36 | import os |
| 37 | |
| 38 | import object_materialize |
| 39 | import object_records |
| 40 | |
| 41 | HANDLES = [] |
| 42 | |
| 43 | DATA_DIR_ENV = "DBBASIC_DATA_DIR" |
| 44 | |
| 45 | |
| 46 | def _data_dir() -> str: |
| 47 | return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR) |
| 48 | |
| 49 | |
| 50 | def EVENT(request): |
| 51 | """Dispatch entry point. request = {"event", "collection", "record_id", "action"}. |
| 52 | |
| 53 | Post-commit, best-effort: every exception is caught here so a bad row |
| 54 | or a race with a concurrent delete never surfaces as a failed write |
| 55 | on the triggering collection -- object_server's own dispatcher already |
| 56 | wraps this call in a try/except for the same reason; this is defense |
| 57 | in depth, not redundant. |
| 58 | """ |
| 59 | collection = str((request or {}).get("collection") or "") |
| 60 | record_id = str((request or {}).get("record_id") or "") |
| 61 | action = str((request or {}).get("action") or "") |
| 62 | if not collection or not record_id or action != "created": |
| 63 | return {"ok": True, "skipped": "not a record.created event"} |
| 64 | |
| 65 | base_dir = _data_dir() |
| 66 | |
| 67 | if not object_materialize.materialize_pass_enabled(base_dir=base_dir): |
| 68 | return {"ok": True, "skipped": "materialize_enabled flag is off"} |
| 69 | |
| 70 | try: |
| 71 | definitions = object_materialize.event_definitions_for_collection(collection, base_dir=base_dir) |
| 72 | except Exception as exc: |
| 73 | return {"ok": False, "error": str(exc)} |
| 74 | |
| 75 | if not definitions: |
| 76 | return {"ok": True, "skipped": f"no enabled event-mode definition for {collection}"} |
| 77 | |
| 78 | try: |
| 79 | source_row = object_records.get_collection_record(collection, record_id, base_dir=base_dir) |
| 80 | except (object_records.RecordNotFoundError, object_records.InvalidRecordIdError): |
| 81 | return {"ok": True, "skipped": "record no longer exists"} |
| 82 | |
| 83 | results = [] |
| 84 | for definition in definitions: |
| 85 | definition_id = definition.get("id") or "<unknown>" |
| 86 | try: |
| 87 | result = object_materialize.generate_one_event(definition, source_row, base_dir=base_dir) |
| 88 | results.append({"definition_id": definition_id, "ok": True, **result}) |
| 89 | except Exception as exc: |
| 90 | results.append({"definition_id": definition_id, "ok": False, "error": str(exc)}) |
| 91 | |
| 92 | return {"ok": True, "results": results} |