@@ -21,10 +21,13 @@ import {
2121 resolveWorkflowScriptFromPathOrNameResult ,
2222} from "./workflow-files.js" ;
2323import { createWorkflowReplay } from "./workflow-replay.js" ;
24+ import {
25+ cancelWorkflowRun ,
26+ reapStaleWorkflows ,
27+ } from "./workflow-lifecycle.js" ;
2428import { parseWorkflowScript } from "./workflow-script.js" ;
2529import { createWorkflowStore , type WorkflowStore } from "./workflow-store.js" ;
2630import {
27- WORKFLOW_CANCEL_HARD_MS ,
2831 WORKFLOW_HEARTBEAT_MS ,
2932 WORKFLOW_LIMITS ,
3033 resolveWorkflowConcurrency ,
@@ -234,6 +237,7 @@ async function runWorkflowStatus(args: string[], config: ServerConfig): Promise<
234237
235238 const store = createWorkflowStore ( config ) ;
236239 try {
240+ reapStaleWorkflows ( store ) ;
237241 const runResult = store . getRunResult ( runId ) ;
238242 if ( runResult . isErr ( ) ) throw runResult . error ;
239243 const run = runResult . value ;
@@ -256,36 +260,8 @@ async function runWorkflowCancel(args: string[], config: ServerConfig): Promise<
256260 if ( ! runId ) throw new Error ( "Usage: devspace workflow cancel <runId>" ) ;
257261 const store = createWorkflowStore ( config ) ;
258262 try {
259- const requested = store . requestCancelResult ( runId ) ;
260- if ( requested . isErr ( ) ) throw requested . error ;
261- const run = requested . value ;
262- console . log ( formatRunLine ( run ) ) ;
263- if ( run . pid && ( run . status === "running" || run . status === "starting" ) ) {
264- try {
265- process . kill ( run . pid , "SIGTERM" ) ;
266- } catch {
267- // already dead
268- }
269- await sleep ( WORKFLOW_CANCEL_HARD_MS ) ;
270- const again = store . getRun ( runId ) ;
271- if ( again && ( again . status === "running" || again . status === "starting" ) && again . pid ) {
272- try {
273- process . kill ( - again . pid , "SIGKILL" ) ;
274- } catch {
275- try {
276- process . kill ( again . pid , "SIGKILL" ) ;
277- } catch {
278- // gone
279- }
280- }
281- const latest = store . getRun ( runId ) ;
282- if ( latest && ( latest . status === "running" || latest . status === "starting" ) ) {
283- const cancelled = store . cancelRunResult ( runId , "cancelled (hard kill)" ) ;
284- if ( cancelled . isErr ( ) ) throw cancelled . error ;
285- }
286- }
287- }
288- console . log ( formatRunLine ( store . getRun ( runId ) ! ) ) ;
263+ reapStaleWorkflows ( store ) ;
264+ console . log ( formatRunLine ( await cancelWorkflowRun ( store , runId ) ) ) ;
289265 } finally {
290266 store . close ( ) ;
291267 }
@@ -294,6 +270,7 @@ async function runWorkflowCancel(args: string[], config: ServerConfig): Promise<
294270async function runWorkflowList ( config : ServerConfig ) : Promise < void > {
295271 const store = createWorkflowStore ( config ) ;
296272 try {
273+ reapStaleWorkflows ( store ) ;
297274 const runs = store . listRuns ( 50 ) ;
298275 if ( runs . length === 0 ) {
299276 console . log ( "No workflow runs." ) ;
0 commit comments