1+ import { spawnSync } from "node:child_process" ;
12import fs from "node:fs/promises" ;
23import os from "node:os" ;
34import path from "node:path" ;
4- import { setTimeout as delay } from "node:timers/promises" ;
55import { describe , expect , it } from "vitest" ;
66import { runCronCommandJob } from "./command-runner.js" ;
77import type { CronJob } from "./types.js" ;
@@ -22,6 +22,23 @@ function makeCommandJob(payload: Extract<CronJob["payload"], { kind: "command" }
2222 } ;
2323}
2424
25+ function isProcessRunning ( pid : number ) : boolean {
26+ const result = spawnSync ( "ps" , [ "-o" , "state=" , "-p" , String ( pid ) ] , { encoding : "utf8" } ) ;
27+ if ( result . error ) {
28+ throw result . error ;
29+ }
30+ const state = result . stdout . trim ( ) ;
31+ if ( result . status === 0 ) {
32+ return ! state . startsWith ( "Z" ) ;
33+ }
34+ if ( result . status === 1 && state === "" && result . stderr . trim ( ) === "" ) {
35+ return false ;
36+ }
37+ throw new Error (
38+ `ps failed with status ${ result . status ?? "unknown" } : ${ result . stderr . trim ( ) || "no output" } ` ,
39+ ) ;
40+ }
41+
2542describe ( "runCronCommandJob" , ( ) => {
2643 it ( "runs command argv and returns stdout as the deliverable summary" , async ( ) => {
2744 const result = await runCronCommandJob ( {
@@ -121,26 +138,29 @@ describe("runCronCommandJob", () => {
121138
122139 it . skipIf ( process . platform === "win32" ) ( "kills shell process groups on timeout" , async ( ) => {
123140 const tempDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-cron-command-" ) ) ;
124- const markerPath = path . join ( tempDir , "survived" ) ;
125- const childScript = [
126- `setTimeout(() => require("node:fs").writeFileSync(${ JSON . stringify ( markerPath ) } , "alive"), 350)` ,
127- "setInterval(() => {}, 1000)" ,
128- ] . join ( ";" ) ;
129- const shellCommand = `${ JSON . stringify ( process . execPath ) } -e ${ JSON . stringify ( childScript ) } ` ;
141+ const childPidPath = path . join ( tempDir , "child.pid" ) ;
142+ const childScript = "setInterval(() => {}, 1000)" ;
143+ const shellCommand = [
144+ `${ JSON . stringify ( process . execPath ) } -e ${ JSON . stringify ( childScript ) } &` ,
145+ "child_pid=$!" ,
146+ `printf '%s' "$child_pid" > ${ JSON . stringify ( childPidPath ) } ` ,
147+ 'wait "$child_pid"' ,
148+ ] . join ( "\n" ) ;
130149
131150 const result = await runCronCommandJob ( {
132151 job : makeCommandJob ( {
133152 kind : "command" ,
134153 argv : [ "sh" , "-lc" , shellCommand ] ,
135- timeoutSeconds : 0.05 ,
154+ timeoutSeconds : 0.2 ,
136155 } ) ,
137156 } ) ;
138157
139158 expect ( result . status ) . toBe ( "error" ) ;
140159 expect ( result . error ) . toBe ( "command timed out" ) ;
141160
142- await delay ( 700 ) ;
143- await expect ( fs . access ( markerPath ) ) . rejects . toThrow ( ) ;
161+ const childPid = Number . parseInt ( await fs . readFile ( childPidPath , "utf8" ) , 10 ) ;
162+ expect ( Number . isSafeInteger ( childPid ) ) . toBe ( true ) ;
163+ await expect . poll ( ( ) => isProcessRunning ( childPid ) ) . toBe ( false ) ;
144164 await fs . rm ( tempDir , { recursive : true , force : true } ) ;
145165 } ) ;
146166
0 commit comments