113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763 | class DatabaseService(Service):
"""Manages the SQLite database for operational telemetry.
Handles PRAGMA user_version migrations, heartbeat updates, and retention cleanup
of old execution records.
"""
restart_spec: ClassVar[RestartSpec] = RestartSpec(
restart_type=RestartType.TRANSIENT,
budget_intensity=3,
budget_period_seconds=120,
fatal_error_names=("SchemaVersionError",),
)
_db: aiosqlite.Connection | None
"""The aiosqlite write connection, set during on_initialize."""
_read_db: aiosqlite.Connection | None
"""Dedicated read-only connection for TelemetryQueryService. Opened on a separate
WAL snapshot so reads never block the write worker."""
_db_path: Path
"""Resolved path to the SQLite database file."""
_consecutive_heartbeat_failures: int
"""Counter for consecutive heartbeat failures; triggers RuntimeError after threshold."""
_db_write_queue: asyncio.Queue[_WriteQueueItem] | None
"""Bounded queue of pending write coroutines; each paired with an optional Future for result delivery."""
_db_worker_task: asyncio.Task[None] | None
"""Background task that drains _db_write_queue sequentially."""
_consecutive_size_triggers: int
"""Counter for consecutive hourly size failsafe triggers; logged as a warning."""
def __init__(self, hassette: "Hassette", *, parent: "Resource | None" = None) -> None:
super().__init__(hassette, parent=parent)
self._db = None
self._read_db = None
self._db_path = Path()
self._consecutive_heartbeat_failures = 0
self._consecutive_size_triggers = 0
self._db_write_queue = None
self._db_worker_task = None
@property
def config_log_level(self) -> LOG_LEVEL_TYPE:
return self.hassette.config.logging.database_service
@property
def is_db_ready(self) -> bool:
"""Whether the write database connection is open and usable."""
return self._db is not None
@property
def is_accepting_writes(self) -> bool:
"""Whether the write queue is live and accepting submissions.
False before ``on_initialize()`` creates the queue and after shutdown drains it.
"""
return self._db_write_queue is not None
@property
def db(self) -> aiosqlite.Connection:
"""Return the active write database connection.
Raises:
RuntimeError: If the database connection is not initialized.
"""
if self._db is None:
raise RuntimeError("Database connection is not initialized")
return self._db
@property
def read_db(self) -> aiosqlite.Connection:
"""Return the dedicated read-only database connection.
Uses a separate WAL snapshot so reads never block the write worker.
Raises:
RuntimeError: If the read connection is not initialized.
"""
if self._read_db is None:
raise RuntimeError("Read database connection is not initialized")
return self._read_db
async def on_initialize(self) -> None:
"""Set up the database: check schema version, run migrations and open connection."""
self._consecutive_heartbeat_failures = 0
self._consecutive_size_triggers = 0
self._db_path = self._resolve_db_path()
self._db_path.parent.mkdir(parents=True, exist_ok=True)
await self._handle_schema_version(self._db_path)
self.logger.info("Running database migrations for %s", self._db_path)
timeout = self.hassette.config.database.migration_timeout_seconds
await asyncio.wait_for(asyncio.to_thread(self._run_migrations), timeout=timeout)
self._db = await _connect_daemon(self._db_path, isolation_level=None)
self._db.row_factory = aiosqlite.Row
# Open a dedicated read connection on a separate WAL snapshot (F1).
# This ensures read queries never block the write worker.
self._read_db = await _connect_daemon(self._db_path, isolation_level=None)
self._read_db.row_factory = aiosqlite.Row
await self._read_db.execute("PRAGMA query_only = ON")
await self._read_db.execute("PRAGMA busy_timeout = 5000")
await self._set_pragmas()
try:
await self._check_size_failsafe()
except Exception:
self.logger.warning("Startup size failsafe check failed; continuing without cleanup", exc_info=True)
self._db_write_queue = asyncio.Queue(maxsize=self.hassette.config.database.write_queue_max)
self._db_worker_task = asyncio.create_task(self._db_write_worker())
async def serve(self) -> None:
"""Run the heartbeat, retention, and size failsafe loop until shutdown."""
self.mark_ready(reason="Database service started")
last_retention_run = time.monotonic()
last_size_failsafe_run = time.monotonic()
while True:
try:
await asyncio.wait_for(self.shutdown_event.wait(), timeout=_HEARTBEAT_INTERVAL_SECONDS)
# shutdown_event was set — exit
self.mark_not_ready(reason="Shutting down")
return
except TimeoutError:
pass
await self._update_heartbeat()
if self._consecutive_heartbeat_failures >= _MAX_CONSECUTIVE_HEARTBEAT_FAILURES:
raise RuntimeError(f"Heartbeat failed {self._consecutive_heartbeat_failures} consecutive times")
time_since_retention = time.monotonic() - last_retention_run
if time_since_retention >= _RETENTION_INTERVAL_SECONDS:
await self._run_retention_cleanup()
last_retention_run = time.monotonic()
time_since_size_failsafe = time.monotonic() - last_size_failsafe_run
if time_since_size_failsafe >= _SIZE_FAILSAFE_INTERVAL_SECONDS:
await self._run_size_failsafe()
last_size_failsafe_run = time.monotonic()
async def on_shutdown(self) -> None:
"""Drain the write queue, cancel the worker, then close the database connection."""
queue: asyncio.Queue[_WriteQueueItem] | None = None
try:
if self._db_worker_task is not None:
queue, self._db_write_queue = self._db_write_queue, None
if queue is not None:
await queue.join()
self._db_worker_task.cancel()
await asyncio.gather(self._db_worker_task, return_exceptions=True)
self._db_worker_task = None
except Exception:
self.logger.exception("Error draining write queue during shutdown")
finally:
if self._db_worker_task is not None:
self._db_worker_task.cancel()
await asyncio.gather(self._db_worker_task, return_exceptions=True)
self._db_worker_task = None
self._close_remaining_queue_items(queue)
await self._close_connections()
def _close_remaining_queue_items(self, queue: asyncio.Queue[_WriteQueueItem] | None) -> None:
"""Close any coroutines left on the write queue without executing them.
Called during shutdown to prevent unawaited-coroutine warnings from GC.
"""
if queue is None:
return
closed = 0
while True:
try:
coro, future = queue.get_nowait()
except asyncio.QueueEmpty:
break
coro.close()
if future is not None and not future.done():
future.cancel()
queue.task_done()
closed += 1
if closed:
self.logger.debug("Closed %d remaining coroutine(s) from write queue during shutdown", closed)
async def _close_connections(self) -> None:
"""Close both database connections. Idempotent — safe to call multiple times.
Always attempts both connections even if the first close raises CancelledError.
aiosqlite's worker threads are set to daemon in on_initialize() as a safety net,
but this method still does a best-effort close to avoid resource warnings and
ensure clean WAL checkpoints.
"""
first_cancel: BaseException | None = None
for attr in ("_read_db", "_db"):
conn: aiosqlite.Connection | None = getattr(self, attr)
if conn is None:
continue
try:
await conn.close()
except asyncio.CancelledError as exc: # noqa: ASYNC103 — re-raised after both connections are handled
conn.stop()
if first_cancel is None:
first_cancel = exc
except Exception:
self.logger.exception("Failed to close %s — falling back to sync stop()", attr)
conn.stop()
finally:
thread = getattr(conn, "_thread", None)
if thread is not None and thread.is_alive():
await asyncio.to_thread(thread.join, 5.0)
if thread.is_alive():
self.logger.warning("aiosqlite background thread for %s did not exit within 5s", attr)
setattr(self, attr, None)
if first_cancel is not None:
raise first_cancel
async def cleanup(self, timeout: int | None = None) -> None:
"""Close database connections if on_shutdown was interrupted or never ran."""
await self._close_connections()
await super().cleanup(timeout)
async def _db_write_worker(self) -> None:
"""Drain _db_write_queue sequentially.
Each item is a (coroutine, future) pair. If future is not None, the
coroutine's result (or exception) is delivered through it. If future is
None, any exception is logged and the worker continues.
The loop runs until cancelled by on_shutdown().
"""
if self._db_write_queue is None:
raise RuntimeError("_db_write_worker() started before on_initialize() set _db_write_queue")
queue = self._db_write_queue
while True:
coro, future = await queue.get()
try:
result = await coro
if future is not None and not future.done():
future.set_result(result)
except Exception as exc:
if future is not None and not future.done():
future.set_exception(exc)
else:
self.logger.exception("Unhandled error in enqueued DB write")
finally:
queue.task_done()
async def submit(self, coro: Coroutine[Any, Any, Any]) -> Any:
"""Submit a coroutine for serialized execution and await its result.
The coroutine is placed on the write queue and executed by the single-writer
worker. The caller is suspended until the coroutine completes.
Args:
coro: The coroutine to execute.
Returns:
The return value of the coroutine.
Raises:
Exception: Whatever exception the coroutine raises.
"""
if self._db_write_queue is None:
coro.close()
raise RuntimeError("DatabaseService.submit() called before on_initialize()")
future: asyncio.Future[Any] = asyncio.get_running_loop().create_future()
try:
await self._db_write_queue.put((coro, future))
except BaseException:
coro.close()
future.cancel()
raise
return await future
def enqueue(self, coro: Coroutine[Any, Any, Any]) -> bool:
"""Submit a coroutine for fire-and-forget execution.
Returns immediately. The coroutine is placed on the write queue and
executed by the single-writer worker. Any exception is logged; the worker
continues processing subsequent items.
Args:
coro: The coroutine to execute.
Returns:
True if enqueued successfully, False if dropped due to a full queue.
"""
if self._db_write_queue is None:
coro.close()
raise RuntimeError("DatabaseService.enqueue() called before on_initialize()")
try:
self._db_write_queue.put_nowait((coro, None))
except asyncio.QueueFull:
coro.close()
self.logger.error(
"DB write queue full (%d items) — dropping fire-and-forget task",
self._db_write_queue.qsize(),
)
return False
qsize = self._db_write_queue.qsize()
if qsize > 0 and qsize % 100 == 0:
self.logger.warning("DB write queue depth at %d items — potential backlog", qsize)
return True
def _resolve_db_path(self) -> Path:
"""Resolve the database path from config or use default."""
if self.hassette.config.database.path is not None:
return self.hassette.config.database.path.resolve()
return self.hassette.config.data_dir / "hassette.db"
def _get_expected_head_version(self) -> int:
"""Return the highest migration version number from migrations_sql/ (synchronous).
Scans the migrations_sql/ directory for *.sql files with numeric stems
and returns the largest version number found.
"""
sql_files = _collect_migrations(None)
if not sql_files:
raise RuntimeError("No migration files found in migrations_sql/")
return max(sql_files)
def _get_current_db_version(self, db_path: Path) -> int:
"""Return PRAGMA user_version from the on-disk DB (synchronous). Returns 0 for fresh databases."""
return _read_user_version(db_path)
async def _handle_schema_version(self, db_path: Path) -> None:
"""Check schema version and handle mismatches.
If the DB file does not exist yet, does nothing (migrations will create it).
If the DB version matches the expected head, does nothing.
If the DB version is older than head, logs a WARNING and deletes the DB file
so that migrations recreate it cleanly.
If the DB version is *ahead* of head (newer DB on older binary), logs an ERROR
and raises SchemaVersionError — auto-delete is refused in this case.
Args:
db_path: Path to the SQLite database file.
Raises:
SchemaVersionError: When the DB version is ahead of the expected head version.
RuntimeError: When the DB file cannot be deleted due to permissions.
"""
if not db_path.exists():
return
expected_head = await asyncio.to_thread(self._get_expected_head_version)
current_version = await asyncio.to_thread(self._get_current_db_version, db_path)
if current_version == expected_head:
return
if current_version == 0:
# PRAGMA user_version = 0 on an existing file means either a fresh DB
# (no migrations applied) or a pre-PRAGMA-era Alembic-managed DB.
# Treat as stale schema needing recreation.
self.logger.warning(
"Database has no schema version (expected %d) — recreating database (no production data to preserve).",
expected_head,
)
elif current_version > expected_head:
self.logger.error(
"Database schema version %d is ahead of the code's expected head %d. "
"This usually means a newer binary created this database. "
"Refusing to auto-delete — upgrade the binary or remove the database manually.",
current_version,
expected_head,
)
raise SchemaVersionError(
f"Database schema version {current_version} is ahead of expected head "
f"{expected_head}. Cannot start safely."
)
else:
self.logger.warning(
"Database schema version mismatch (current=%d, expected=%d) — "
"recreating database (no production data to preserve).",
current_version,
expected_head,
)
try:
db_path.unlink(missing_ok=True)
# Also remove WAL and SHM side-car files if present
for suffix in ("-wal", "-shm"):
Path(str(db_path) + suffix).unlink(missing_ok=True)
except PermissionError as exc:
raise RuntimeError(
f"Cannot delete stale database file {db_path}: {exc}. Please remove it manually and restart."
) from exc
def _run_migrations(self) -> None:
"""Run PRAGMA user_version migrations to the latest version (synchronous, called via to_thread).
auto_vacuum = INCREMENTAL is set by the runner before any tables are created.
"""
run_migrations(self._db_path)
async def _set_pragmas(self) -> None:
"""Configure SQLite PRAGMAs for performance and safety."""
db = self.db
await db.execute("PRAGMA journal_mode = WAL")
await db.execute("PRAGMA wal_autocheckpoint = 1000")
# NORMAL is an intentional performance tradeoff: in WAL mode, the last committed
# writes before an OS crash (not app crash) may be lost if not yet checkpointed.
# This is acceptable for operational telemetry — the orphan-session mechanism
# compensates for session rows but not for individual telemetry records.
await db.execute("PRAGMA synchronous = NORMAL")
await db.execute("PRAGMA busy_timeout = 5000")
await db.execute("PRAGMA foreign_keys = ON")
# Intentionally a no-op — auto_vacuum is set by the migration runner before table creation.
# This line documents intent only.
await db.execute("PRAGMA auto_vacuum = INCREMENTAL")
async def _update_heartbeat(self) -> None:
"""Await a heartbeat update for the current session.
Early-return guards run inline; the DB write is awaited via submit()
so that _consecutive_heartbeat_failures is updated before returning.
"""
if self._db is None:
return
if self._db_write_queue is None:
return
try:
_ = self.hassette.session_id
except RuntimeError:
return
await self.submit(self._do_update_heartbeat())
async def _do_update_heartbeat(self) -> None:
"""Execute the heartbeat DB write; called by the write-queue worker."""
try:
session_id = self.hassette.session_id
now = time.time()
await self.db.execute(
"UPDATE sessions SET last_heartbeat_at = ? WHERE id = ?",
(now, session_id),
)
await self.db.commit()
self.logger.debug("Heartbeat updated for session %d", session_id)
if self._consecutive_heartbeat_failures > 0:
self.logger.info("Heartbeat recovered after %d failure(s)", self._consecutive_heartbeat_failures)
self._consecutive_heartbeat_failures = 0
except (sqlite3.Error, OSError, ValueError):
self._consecutive_heartbeat_failures += 1
self.logger.exception(
"Failed to update heartbeat (failure %d/%d)",
self._consecutive_heartbeat_failures,
_MAX_CONSECUTIVE_HEARTBEAT_FAILURES,
)
async def _run_retention_cleanup(self) -> None:
"""Enqueue a retention cleanup; fire-and-forget via enqueue()."""
if self._db is None:
return
if self._db_write_queue is None:
return
self.enqueue(self._do_run_retention_cleanup())
async def _do_run_retention_cleanup(self) -> None:
"""Execute the retention DELETE queries; called by the write-queue worker.
Iterates _RETENTION_TABLES for simple age-based deletes, then applies
NOT EXISTS guard deletes for parent tables (listeners, scheduled_jobs).
"""
try:
config = self.hassette.config
now = time.time()
deleted_by_table: dict[str, int] = {}
# Explicit BEGIN — aiosqlite opens connections with isolation_level=None (autocommit),
# so without this BEGIN each DELETE commits individually and the rollback() in the
# except clause is a no-op. The BEGIN makes the whole cleanup one atomic transaction.
await self.db.execute("BEGIN")
for target in _RETENTION_TABLES:
cutoff = now - (target.retention_days_getter(config) * SECONDS_PER_DAY)
cursor = await self.db.execute(
f"DELETE FROM {target.table} WHERE {target.timestamp_col} < ?",
(cutoff,),
)
deleted_by_table[target.table] = cursor.rowcount or 0
# Use the standard retention window for parent-guard deletes.
cutoff = now - (config.database.retention_days * SECONDS_PER_DAY)
# Only delete retired listeners when ALL their child executions have also aged out.
# This prevents orphaning recent executions whose parent row would be
# deleted because retired_at (set at restart time) diverges from last execution time.
cursor_rl = await self.db.execute(
"""
DELETE FROM listeners
WHERE retired_at IS NOT NULL AND retired_at < ?
AND NOT EXISTS (
SELECT 1 FROM executions
WHERE listener_id = listeners.id
AND execution_start_ts >= ?
)
""",
(cutoff, cutoff),
)
# Same guard for scheduled_jobs.
cursor_rj = await self.db.execute(
"""
DELETE FROM scheduled_jobs
WHERE retired_at IS NOT NULL AND retired_at < ?
AND NOT EXISTS (
SELECT 1 FROM executions
WHERE job_id = scheduled_jobs.id
AND execution_start_ts >= ?
)
""",
(cutoff, cutoff),
)
await self.db.commit()
listeners_deleted = cursor_rl.rowcount or 0
jobs_deleted = cursor_rj.rowcount or 0
deleted_summary = {table: count for table, count in deleted_by_table.items() if count > 0}
if deleted_summary:
parts = ", ".join(f"{count} {table}" for table, count in deleted_summary.items())
self.logger.info("Retention cleanup: deleted %s", parts)
if listeners_deleted or jobs_deleted:
self.logger.info(
"Retention cleanup: deleted %d retired listeners, %d retired scheduled_jobs",
listeners_deleted,
jobs_deleted,
)
except Exception:
await self.db.rollback()
self.logger.exception("Failed to run retention cleanup")
def _get_db_size_mb(self) -> float:
"""Return total database size (main + WAL + SHM) in megabytes."""
total = 0
for suffix in ("", "-wal", "-shm"):
path = Path(str(self._db_path) + suffix)
if path.exists():
total += path.stat().st_size
return total / (1024 * 1024)
async def _check_size_failsafe(self) -> None:
"""Delete oldest records if database exceeds the configured size limit.
Iterates _RETENTION_TABLES grouped by priority (lower priority number = deleted
first). Within each priority tier, all tables in the group are deleted together
per iteration. After each iteration a vacuum+checkpoint reclaims disk space.
The process stops as soon as the database falls within the size limit.
"""
max_size_mb = self.hassette.config.database.max_size_mb
if max_size_mb == 0:
return
current_size = self._get_db_size_mb()
if current_size <= max_size_mb:
self._consecutive_size_triggers = 0
return
self._consecutive_size_triggers += 1
if self._consecutive_size_triggers > 1:
self.logger.warning(
"Size failsafe triggered %d consecutive times (%.1f MB > %.1f MB limit)",
self._consecutive_size_triggers,
current_size,
max_size_mb,
)
db = self.db
total_deleted_by_table: dict[str, int] = {t.table: 0 for t in _RETENTION_TABLES}
priorities = sorted({t.priority for t in _RETENTION_TABLES})
for priority in priorities:
group = [t for t in _RETENTION_TABLES if t.priority == priority]
group_label = ", ".join(t.failsafe_label for t in group)
for iteration in range(_SIZE_FAILSAFE_MAX_ITERATIONS):
group_deleted = 0
for target in group:
cursor = await db.execute(
f"DELETE FROM {target.table} WHERE id IN "
f"(SELECT id FROM {target.table} ORDER BY {target.timestamp_col} ASC LIMIT ?)",
(_SIZE_FAILSAFE_DELETE_BATCH,),
)
n = cursor.rowcount or 0
total_deleted_by_table[target.table] += n
group_deleted += n
# Commit the batch before vacuuming. PRAGMA wal_checkpoint(TRUNCATE) below
# cannot run while the delete statements hold a write lock — without this
# commit it fails with "database table is locked".
await db.commit()
if group_deleted == 0:
break
vacuum_cursor = await db.execute(f"PRAGMA incremental_vacuum({_SIZE_FAILSAFE_VACUUM_PAGES})")
await vacuum_cursor.close()
await db.execute("PRAGMA wal_checkpoint(TRUNCATE)")
current_size = self._get_db_size_mb()
if current_size <= max_size_mb:
break
if iteration == _SIZE_FAILSAFE_MAX_ITERATIONS - 1:
self.logger.warning(
"Size failsafe %s capped at %d iterations; database still %.1f MB (limit %.1f MB)",
group_label,
_SIZE_FAILSAFE_MAX_ITERATIONS,
current_size,
max_size_mb,
)
current_size = self._get_db_size_mb()
if current_size <= max_size_mb:
break
deleted_summary = {table: count for table, count in total_deleted_by_table.items() if count > 0}
if deleted_summary:
parts = ", ".join(f"{count} {table}" for table, count in deleted_summary.items())
self.logger.info("Size failsafe: deleted %s (%.1f MB remaining)", parts, current_size)
async def _run_size_failsafe(self) -> None:
"""Enqueue a size failsafe check; fire-and-forget via enqueue()."""
if self._db is None:
return
if self._db_write_queue is None:
return
self.enqueue(self._check_size_failsafe())
async def _insert_log_records(self, records: list[dict]) -> None:
"""Batch-insert log records into the log_records table.
Must only be called via enqueue() — never await directly.
"""
if not records:
return
db = self.db
try:
await db.execute("BEGIN")
await db.executemany(_LOG_INSERT_SQL, records)
await db.commit()
except Exception:
await db.rollback()
raise
|