1+ import type { DatabaseStorageStatus } from "@t3tools/contracts" ;
12import * as Context from "effect/Context" ;
23import * as Effect from "effect/Effect" ;
4+ import * as Equal from "effect/Equal" ;
35import * as Layer from "effect/Layer" ;
6+ import * as Path from "effect/Path" ;
47import * as Schedule from "effect/Schedule" ;
8+ import * as Stream from "effect/Stream" ;
9+ import * as SubscriptionRef from "effect/SubscriptionRef" ;
510import * as SqlClient from "effect/sql/SqlClient" ;
611
12+ import { resolveHostCliCommand } from "../cli/invocation.ts" ;
13+ import * as ServerConfig from "../config.ts" ;
714import * as ProjectionMaintenance from "../orchestration-v2/ProjectionMaintenance.ts" ;
815import { forkParked } from "../serverActivation.ts" ;
916
@@ -15,6 +22,8 @@ export class DatabaseStorage extends Context.Service<
1522 {
1623 /** Return free pages to the filesystem on databases with incremental auto_vacuum. */
1724 readonly reclaim : Effect . Effect < void > ;
25+ /** Current file accounting, re-published after each maintenance pass. Null until first read. */
26+ readonly status : Stream . Stream < DatabaseStorageStatus | null > ;
1827 }
1928> ( ) ( "t3/persistence/DatabaseStorage" ) { }
2029
@@ -27,6 +36,42 @@ export class DatabaseStorage extends Context.Service<
2736const make = Effect . gen ( function * ( ) {
2837 const sql = yield * SqlClient . SqlClient ;
2938 const maintenance = yield * ProjectionMaintenance . ProjectionMaintenanceV2 ;
39+ const config = yield * ServerConfig . ServerConfig ;
40+ const path = yield * Path . Path ;
41+ const statusRef = yield * SubscriptionRef . make < DatabaseStorageStatus | null > ( null ) ;
42+
43+ // The notice shows this verbatim; quote for the host's shell. Desktop
44+ // installs have no `t3` on PATH, so the command names their launcher.
45+ const { command : compactCommand } = yield * resolveHostCliCommand ( "doctor" , [
46+ "--base-dir" ,
47+ config . baseDir ,
48+ ] ) ;
49+ // `t3 doctor --base-dir` repairs <baseDir>/userdata. A dev server without
50+ // an explicit home keeps its state in <baseDir>/dev instead, so there the
51+ // command would target the live install; publish no status at all.
52+ const doctorTargetsThisDatabase =
53+ path . resolve ( config . dbPath ) ===
54+ path . resolve ( config . baseDir , "userdata" , path . basename ( config . dbPath ) ) ;
55+ const publishStatus = Effect . gen ( function * ( ) {
56+ if ( ! doctorTargetsThisDatabase ) return ;
57+ const pages = yield * sql < { readonly page_count : number } > `PRAGMA page_count` ;
58+ const free = yield * sql < { readonly freelist_count : number } > `PRAGMA freelist_count` ;
59+ const size = yield * sql < { readonly page_size : number } > `PRAGMA page_size` ;
60+ const mode = yield * sql < { readonly auto_vacuum : number } > `PRAGMA auto_vacuum` ;
61+ const pageSize = size [ 0 ] ?. page_size ?? 0 ;
62+ const next : DatabaseStorageStatus = {
63+ databaseBytes : ( pages [ 0 ] ?. page_count ?? 0 ) * pageSize ,
64+ reclaimableBytes : ( free [ 0 ] ?. freelist_count ?? 0 ) * pageSize ,
65+ incrementalVacuumEnabled : mode [ 0 ] ?. auto_vacuum === 2 ,
66+ compactCommand,
67+ } ;
68+ // Every set reaches each open Storage page; most passes change nothing.
69+ if ( ! Equal . equals ( yield * SubscriptionRef . get ( statusRef ) , next ) ) {
70+ yield * SubscriptionRef . set ( statusRef , next ) ;
71+ }
72+ } ) . pipe (
73+ Effect . catch ( ( cause ) => Effect . logWarning ( "Database storage inspection failed" , { cause } ) ) ,
74+ ) ;
3075
3176 const reclaim = Effect . gen ( function * ( ) {
3277 const mode = yield * sql < { readonly auto_vacuum : number } > `PRAGMA auto_vacuum` ;
@@ -57,16 +102,21 @@ const make = Effect.gen(function* () {
57102
58103 yield * forkParked (
59104 Effect . sleep ( "1 minute" ) . pipe (
60- Effect . andThen ( compact . pipe ( Effect . repeat ( Schedule . spaced ( "6 hours" ) ) ) ) ,
105+ Effect . andThen (
106+ compact . pipe ( Effect . andThen ( publishStatus ) , Effect . repeat ( Schedule . spaced ( "6 hours" ) ) ) ,
107+ ) ,
61108 ) ,
62109 ) ;
63110 yield * forkParked (
64- Effect . sleep ( "1 minute" ) . pipe (
65- Effect . andThen ( reclaim . pipe ( Effect . repeat ( Schedule . spaced ( "1 minute" ) ) ) ) ,
111+ publishStatus . pipe (
112+ Effect . andThen ( Effect . sleep ( "1 minute" ) ) ,
113+ Effect . andThen (
114+ reclaim . pipe ( Effect . andThen ( publishStatus ) , Effect . repeat ( Schedule . spaced ( "1 minute" ) ) ) ,
115+ ) ,
66116 ) ,
67117 ) ;
68118
69- return DatabaseStorage . of ( { reclaim } ) ;
119+ return DatabaseStorage . of ( { reclaim, status : SubscriptionRef . changes ( statusRef ) } ) ;
70120} ) ;
71121
72122export const layer = Layer . effect ( DatabaseStorage , make ) ;
0 commit comments