Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3Implements plan/vocabulary/61-materialize-spec.md's Events section 2:
4declares HANDLES for every distinct source_collection value across
5enabled, non-blocked, trigger.mode == "event" materialize_definitions
6rows. Registered as a Phase-5a event handler (object_handlers.py,
7docs/event-hooks-decisions.md); the platform's post-commit dispatcher
8executes this object's EVENT method whenever a client-facing write
9commits to a HANDLES-listed collection, only when the operator has
10DBBASIC_ENABLE_EVENT_HANDLERS set. With the flag unset (the default),
11event-mode definitions simply never receive dispatches -- the record is
12created unseeded, same as pre-materialize behavior exactly (61's
13Degradation); materialize_run (manual) still works and can be fired by
14hand on that record's id.
15
16HANDLES below is an empty static list literal, and in v1 NOTHING keeps it
17in sync with the event-mode definition set: this codebase's HANDLES
18mechanism (object_handlers.extract_handles) is a pure AST parse of a
19module-level list literal, and the platform deliberately does NOT rewrite
20an installed object's source at runtime to track the definitions
21(rewriting an existing object under the daemon poll loop is the wrong
22shape -- expensive, surprising, self-modifying). So automatic on-create
23dispatch is effectively off in v1 and every event-mode definition runs via
24the manual path (materialize_run) -- exactly 61's degrade-to-manual
25posture, just made the always-on default rather than only the
26DBBASIC_ENABLE_EVENT_HANDLERS-unset case.
27
28The EVENT method below is fully implemented and tested and would dispatch
29correctly the moment HANDLES named a collection -- left ready for a future
30DELIBERATE, scheduled, separately-tested HANDLES-sync job (whose input is
31object_materialize.compute_event_handles), or a hand-edited wiring, rather
32than a per-pass source rewrite. Until then this object is inert.
33"""
34from __future__ import annotations
35
36import os
37
38import object_materialize
39import object_records
40
41HANDLES = []
42
43DATA_DIR_ENV = "DBBASIC_DATA_DIR"
44
45
46def _data_dir() -> str:
47 return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR)
48
49
50def 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}