@@ -10,6 +10,7 @@ import * as Path from "effect/Path";
1010import { vi } from "vite-plus/test" ;
1111
1212import {
13+ boundServiceLog ,
1314 databaseBackupBlocker ,
1415 Launcher ,
1516 main ,
@@ -28,12 +29,14 @@ import {
2829} from "./cloud/serviceProtocol.ts" ;
2930
3031const spawnedChildren = vi . hoisted ( ( ) : Array < NodeChildProcess . ChildProcess > => [ ] ) ;
32+ const spawnListeners = vi . hoisted ( ( ) => new Set < ( child : NodeChildProcess . ChildProcess ) => void > ( ) ) ;
3133
3234vi . mock ( "node:child_process" , async ( importOriginal ) => {
3335 const actual = await importOriginal < typeof NodeChildProcess > ( ) ;
3436 const spawn = ( ...args : Parameters < typeof actual . spawn > ) => {
3537 const child = actual . spawn ( ...args ) ;
3638 spawnedChildren . push ( child ) ;
39+ for ( const listener of spawnListeners ) listener ( child ) ;
3740 return child ;
3841 } ;
3942 return { ...actual , spawn : spawn as typeof actual . spawn } ;
@@ -250,6 +253,75 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
250253 } ) ,
251254 ) ;
252255
256+ it . effect ( "truncates an oversized boot-service.log when the archive copy fails" , ( ) =>
257+ Effect . gen ( function * ( ) {
258+ const fs = yield * FileSystem . FileSystem ;
259+ const path = yield * Path . Path ;
260+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-service-launcher-log-" } ) ;
261+ const logPath = path . join ( root , "userdata" , "logs" , "boot-service.log" ) ;
262+ yield * fs . makeDirectory ( `${ logPath } .1` , { recursive : true } ) ;
263+ yield * writeSparseFile ( logPath , 17 * 1024 * 1024 ) ;
264+
265+ yield * Effect . promise ( ( ) => boundServiceLog ( root ) ) ;
266+
267+ assert . equal ( Number ( ( yield * fs . stat ( logPath ) ) . size ) , 0 ) ;
268+ assert . equal ( ( yield * fs . stat ( `${ logPath } .1` ) ) . type , "Directory" ) ;
269+ } ) ,
270+ ) ;
271+
272+ it . effect ( "keeps bounding boot-service.log while the service runs" , ( ) =>
273+ Effect . gen ( function * ( ) {
274+ const fs = yield * FileSystem . FileSystem ;
275+ const path = yield * Path . Path ;
276+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-service-launcher-log-" } ) ;
277+ const statePath = path . join ( root , "runtime" , "service-state.json" ) ;
278+ const logPath = path . join ( root , "userdata" , "logs" , "boot-service.log" ) ;
279+ const grownSize = 17 * 1024 * 1024 ;
280+ // The child starts after the startup check, so this growth is only seen by recurring checks.
281+ yield * writeFakeRuntime (
282+ fs ,
283+ path ,
284+ path . join ( root , "runtime" , "versions" , "1.0.0" ) ,
285+ [
286+ 'const fs = require("node:fs");' ,
287+ `fs.writeFileSync(${ JSON . stringify ( logPath ) } , "");` ,
288+ `fs.truncateSync(${ JSON . stringify ( logPath ) } , ${ grownSize } );` ,
289+ 'process.send("log-grown");' ,
290+ "setInterval(() => {}, 1_000);" ,
291+ "" ,
292+ ] . join ( "\n" ) ,
293+ ) ;
294+ yield * Effect . promise ( ( ) =>
295+ writeServiceState ( statePath , {
296+ protocol : SERVICE_LAUNCHER_PROTOCOL ,
297+ activeVersion : "1.0.0" ,
298+ } ) ,
299+ ) ;
300+ yield * fs . makeDirectory ( path . dirname ( logPath ) , { recursive : true } ) ;
301+ vi . useFakeTimers ( { toFake : [ "setInterval" , "clearInterval" ] } ) ;
302+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => vi . useRealTimers ( ) ) ) ;
303+ const logGrown = new Promise < void > ( ( resolve ) => {
304+ const listener = ( child : NodeChildProcess . ChildProcess ) => {
305+ spawnListeners . delete ( listener ) ;
306+ child . once ( "message" , ( ) => resolve ( ) ) ;
307+ } ;
308+ spawnListeners . add ( listener ) ;
309+ } ) ;
310+
311+ const launcher = new Launcher ( root , yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ) ;
312+ const running = launcher . run ( ) ;
313+ yield * Effect . promise ( ( ) => logGrown ) ;
314+ assert . equal ( Number ( ( yield * fs . stat ( logPath ) ) . size ) , grownSize ) ;
315+
316+ yield * Effect . promise ( ( ) => vi . advanceTimersByTimeAsync ( 10 * 60_000 ) ) ;
317+ // Stop queues behind the recurring check, so the check has finished once the launcher exits.
318+ yield * Effect . promise ( ( ) => launcher . stop ( "SIGTERM" ) ) ;
319+ yield * Effect . promise ( ( ) => running ) ;
320+
321+ assert . equal ( Number ( ( yield * fs . stat ( logPath ) ) . size ) , 0 ) ;
322+ } ) ,
323+ ) ;
324+
253325 it . effect ( "stays idle instead of exiting when another server owns the T3 home" , ( ) =>
254326 Effect . gen ( function * ( ) {
255327 const fs = yield * FileSystem . FileSystem ;
0 commit comments