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

0 downloads

dbbasic-object-server/object_daemon_status.py
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
object_daemon_status.py · 11.1 KB · 335 lines
1"""Read-only status summaries for the DBBASIC daemon primitives.
2
3The daemon owns delivery and scheduling. This module only reads the same TSV
4state so Scroll and admin dashboards can see what is due, pending, failing, and
5retained without executing objects.
6"""
7
8from __future__ import annotations
9
10import importlib.util
11import json
12import time
13from collections import Counter
14from collections.abc import Iterable, Mapping
15from datetime import datetime, timezone
16from pathlib import Path
17from typing import Any
18
19import object_events
20import object_state
21from object_namespace import find_trigger_file, get_object_roots
22from object_versions import DEFAULT_DATA_DIR
23
24
25def daemon_status(
26 *,
27 base_dir: Path | str = DEFAULT_DATA_DIR,
28 object_roots: Iterable[Path | str] | None = None,
29 rate_limit_dir: Path | str | None = None,
30 event_keep_count: int | None = object_events.DEFAULT_EVENT_KEEP_COUNT,
31 event_keep_seconds: int | None = object_events.DEFAULT_EVENT_KEEP_SECONDS,
32 now: int | None = None,
33) -> dict[str, Any]:
34 """Return a read-only scheduler/queue/event summary for operators."""
35 data_dir = Path(base_dir)
36 roots = [Path(root) for root in (object_roots if object_roots is not None else get_object_roots())]
37 current_time = int(time.time() if now is None else now)
38
39 scheduler_source = find_trigger_file("scheduler", roots) is not None
40 queue_source = find_trigger_file("queue", roots) is not None
41 events_source = find_trigger_file("events", roots) is not None
42
43 scheduler = _scheduler_status(data_dir, scheduler_source, current_time)
44 queue = _queue_status(data_dir, queue_source, current_time)
45 events = _events_status(data_dir, events_source)
46 cleanup = _cleanup_status(
47 rate_limit_dir=rate_limit_dir,
48 event_keep_count=event_keep_count,
49 event_keep_seconds=event_keep_seconds,
50 )
51
52 invalid_rows = (
53 scheduler["tasks"]["invalid"]
54 + queue["messages"]["invalid"]
55 + events["events"]["invalid"]
56 + events["subscriptions"]["invalid"]
57 )
58 failed_deliveries = events["subscriptions"]["by_delivery_status"].get("failed", 0)
59
60 return {
61 "status": "degraded" if invalid_rows or failed_deliveries else "ok",
62 "timestamp": _utc_timestamp(current_time),
63 "daemon": {
64 "mode": "polling",
65 "croniter_available": importlib.util.find_spec("croniter") is not None,
66 "object_roots": {"count": len(roots)},
67 "triggers": {
68 "scheduler": {"object_id": "scheduler", "source_present": scheduler_source},
69 "queue": {"object_id": "queue", "source_present": queue_source},
70 "events": {"object_id": object_events.EVENTS_OBJECT_ID, "source_present": events_source},
71 },
72 },
73 "scheduler": scheduler,
74 "queue": queue,
75 "events": events,
76 "cleanup": cleanup,
77 }
78
79
80def _scheduler_status(base_dir: Path, source_present: bool, now: int) -> dict[str, Any]:
81 rows, invalid = _prefixed_json_rows(base_dir, "scheduler", "task_")
82 by_status = _status_counts(rows)
83 active_rows = [row for row in rows if _status_name(row.get("status")) == "active"]
84 next_runs = [_coerce_epoch(row.get("next_run")) for row in active_rows]
85 next_runs = [value for value in next_runs if value is not None]
86 due_runs = [value for value in next_runs if value <= now]
87 future_runs = [value for value in next_runs if value > now]
88 earliest = min(next_runs) if next_runs else None
89
90 return {
91 "object_id": "scheduler",
92 "source_present": source_present,
93 "state_present": _state_present(base_dir, "scheduler"),
94 "tasks": {
95 "total": len(rows) + invalid,
96 "valid": len(rows),
97 "invalid": invalid,
98 "by_status": by_status,
99 "active": len(active_rows),
100 "due": len(due_runs),
101 "future": len(future_runs),
102 "missing_next_run": len(active_rows) - len(next_runs),
103 "next_run": earliest,
104 "next_run_iso": _epoch_iso(earliest),
105 },
106 }
107
108
109def _queue_status(base_dir: Path, source_present: bool, now: int) -> dict[str, Any]:
110 rows, invalid = _prefixed_json_rows(base_dir, "queue", "msg_")
111 by_status = _status_counts(rows)
112 pending = [row for row in rows if _status_name(row.get("status")) == "pending"]
113 visible = []
114 delayed = []
115 expired = []
116
117 for row in pending:
118 expires_at = _coerce_epoch(row.get("expires_at"))
119 visible_after = _coerce_epoch(row.get("visible_after")) or 0
120 if expires_at is not None and expires_at < now:
121 expired.append(row)
122 elif visible_after > now:
123 delayed.append((row, visible_after))
124 else:
125 visible.append(row)
126
127 next_visible = min((visible_after for _row, visible_after in delayed), default=None)
128
129 return {
130 "object_id": "queue",
131 "source_present": source_present,
132 "state_present": _state_present(base_dir, "queue"),
133 "messages": {
134 "total": len(rows) + invalid,
135 "valid": len(rows),
136 "invalid": invalid,
137 "by_status": by_status,
138 "pending_visible": len(visible),
139 "pending_delayed": len(delayed),
140 "expired_pending": len(expired),
141 "next_visible_at": next_visible,
142 "next_visible_at_iso": _epoch_iso(next_visible),
143 },
144 }
145
146
147def _events_status(base_dir: Path, source_present: bool) -> dict[str, Any]:
148 event_rows, invalid_events = _prefixed_json_rows(base_dir, object_events.EVENTS_OBJECT_ID, "event_")
149 subscription_rows, invalid_subscriptions = _prefixed_json_rows(
150 base_dir,
151 object_events.EVENTS_OBJECT_ID,
152 "sub_",
153 )
154 sorted_events = sorted(
155 event_rows,
156 key=lambda item: (_coerce_epoch(item.get("timestamp")) or 0, str(item.get("id", ""))),
157 reverse=True,
158 )
159 latest = _sanitize_event(sorted_events[0]) if sorted_events else None
160 by_event_type = _counts_by_field(event_rows, "event_type")
161 by_delivery_status = _delivery_status_counts(subscription_rows)
162
163 return {
164 "object_id": object_events.EVENTS_OBJECT_ID,
165 "source_present": source_present,
166 "state_present": _state_present(base_dir, object_events.EVENTS_OBJECT_ID),
167 "events": {
168 "total": len(event_rows) + invalid_events,
169 "valid": len(event_rows),
170 "invalid": invalid_events,
171 "by_event_type": by_event_type,
172 "latest": latest,
173 },
174 "subscriptions": {
175 "total": len(subscription_rows) + invalid_subscriptions,
176 "valid": len(subscription_rows),
177 "invalid": invalid_subscriptions,
178 "by_event_type": _counts_by_field(subscription_rows, "event_type"),
179 "by_delivery_status": by_delivery_status,
180 "pending_deliveries": sum(
181 _pending_delivery_count(subscription, event_rows)
182 for subscription in subscription_rows
183 ),
184 },
185 }
186
187
188def _cleanup_status(
189 *,
190 rate_limit_dir: Path | str | None,
191 event_keep_count: int | None,
192 event_keep_seconds: int | None,
193) -> dict[str, Any]:
194 return {
195 "event_retention": {
196 "keep_count": event_keep_count,
197 "keep_seconds": event_keep_seconds,
198 },
199 "rate_limit_files": _rate_limit_file_count(rate_limit_dir),
200 }
201
202
203def _prefixed_json_rows(
204 base_dir: Path,
205 object_id: str,
206 prefix: str,
207) -> tuple[list[dict[str, Any]], int]:
208 rows: list[dict[str, Any]] = []
209 invalid = 0
210
211 for key, value in object_state.get_object_state(object_id, base_dir).items():
212 if not key.startswith(prefix):
213 continue
214 item = _json_mapping(value)
215 if item is None:
216 invalid += 1
217 else:
218 rows.append(item)
219
220 return rows, invalid
221
222
223def _json_mapping(value: Any) -> dict[str, Any] | None:
224 if isinstance(value, Mapping):
225 return dict(value)
226 if not isinstance(value, str):
227 return None
228 try:
229 loaded = json.loads(value)
230 except (TypeError, json.JSONDecodeError):
231 return None
232 if not isinstance(loaded, Mapping):
233 return None
234 return dict(loaded)
235
236
237def _status_counts(rows: Iterable[Mapping[str, Any]]) -> dict[str, int]:
238 return dict(sorted(Counter(_status_name(row.get("status")) for row in rows).items()))
239
240
241def _counts_by_field(rows: Iterable[Mapping[str, Any]], field: str) -> dict[str, int]:
242 return dict(sorted(Counter(_text_or_unknown(row.get(field)) for row in rows).items()))
243
244
245def _delivery_status_counts(subscriptions: Iterable[Mapping[str, Any]]) -> dict[str, int]:
246 counts = Counter()
247 for subscription in subscriptions:
248 delivery = subscription.get("delivery")
249 if isinstance(delivery, Mapping):
250 counts[_status_name(delivery.get("status"), default="idle")] += 1
251 else:
252 counts["idle"] += 1
253 return dict(sorted(counts.items()))
254
255
256def _pending_delivery_count(
257 subscription: Mapping[str, Any],
258 events: Iterable[Mapping[str, Any]],
259) -> int:
260 event_type = subscription.get("event_type")
261 matching = [
262 event for event in events
263 if event.get("event_type") == event_type
264 ]
265 matching.sort(
266 key=lambda item: (_coerce_epoch(item.get("timestamp")) or 0, str(item.get("id", "")))
267 )
268 if not matching:
269 return 0
270
271 last_event_id = subscription.get("last_event_id")
272 if not last_event_id:
273 return len(matching)
274
275 for index, event in enumerate(matching):
276 if event.get("id") == last_event_id:
277 return max(0, len(matching) - index - 1)
278 return len(matching)
279
280
281def _sanitize_event(event: Mapping[str, Any]) -> dict[str, Any]:
282 return {
283 "id": event.get("id"),
284 "event_type": event.get("event_type"),
285 "source": event.get("source"),
286 "actor": event.get("actor"),
287 "timestamp": _coerce_epoch(event.get("timestamp")),
288 "created_at": event.get("created_at"),
289 }
290
291
292def _status_name(value: Any, *, default: str = "unknown") -> str:
293 if not isinstance(value, str):
294 return default
295 stripped = value.strip().lower()
296 return stripped or default
297
298
299def _text_or_unknown(value: Any) -> str:
300 if not isinstance(value, str):
301 return "unknown"
302 stripped = value.strip()
303 return stripped or "unknown"
304
305
306def _coerce_epoch(value: Any) -> int | None:
307 if value is None or isinstance(value, bool):
308 return None
309 try:
310 return int(float(value))
311 except (TypeError, ValueError):
312 return None
313
314
315def _epoch_iso(value: int | None) -> str | None:
316 if value is None:
317 return None
318 return _utc_timestamp(value)
319
320
321def _utc_timestamp(epoch: int) -> str:
322 return datetime.fromtimestamp(epoch, tz=timezone.utc).isoformat()
323
324
325def _state_present(base_dir: Path, object_id: str) -> bool:
326 return object_state.object_state_file(object_id, base_dir).exists()
327
328
329def _rate_limit_file_count(rate_limit_dir: Path | str | None) -> int:
330 if rate_limit_dir is None:
331 return 0
332 directory = Path(rate_limit_dir)
333 if not directory.exists():
334 return 0
335 return sum(1 for path in directory.glob("*.txt") if path.is_file())