Skip to content

Commit 4b85992

Browse files
Copilotalexec
andauthored
Print "all requested tasks are running" message only once per ready transition (#115)
* Initial plan * Print ready message only once per ready transition Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
1 parent 32ae71e commit 4b85992

2 files changed

Lines changed: 78 additions & 6 deletions

File tree

internal/run.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB
200200
})
201201
}
202202

203+
allRunning := false
204+
203205
for {
204206
select {
205207
case <-ctx.Done():
@@ -275,15 +277,20 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB
275277
logger.Println("✅ exiting because all requested tasks completed and none should be restarted")
276278
cancel()
277279
} else if len(remainingTasks) == 0 {
278-
logger.Println("🔵 all requested tasks are running:")
279-
// print a list of running tasks, and their ports
280-
for _, node := range subgraph.Nodes {
281-
if (node.Phase == "running" || node.Phase == "stalled") && node.Task.Ports != nil {
282-
for _, port := range node.Task.Ports {
283-
logger.Printf(" - %s: http://localhost:%d\n", node.Name, port.HostPort)
280+
if !allRunning {
281+
allRunning = true
282+
logger.Println("🔵 all requested tasks are running:")
283+
// print a list of running tasks, and their ports
284+
for _, node := range subgraph.Nodes {
285+
if (node.Phase == "running" || node.Phase == "stalled") && node.Task.Ports != nil {
286+
for _, port := range node.Task.Ports {
287+
logger.Printf(" - %s: http://localhost:%d\n", node.Name, port.HostPort)
288+
}
284289
}
285290
}
286291
}
292+
} else {
293+
allRunning = false
287294
}
288295

289296
// if the event is a string, it is the name of the task to run
@@ -308,6 +315,7 @@ func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openB
308315
node := subgraph.Nodes[taskName]
309316

310317
node.cancel()
318+
allRunning = false
311319

312320
// each task is executed in a separate goroutine
313321
wg.Add(1)

internal/run_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,70 @@ sleep 30
437437
err := RunSubgraph(ctx, cancel, 0, false, logger, wf, []string{"job"}, nil)
438438
assert.NoError(t, err)
439439
})
440+
441+
t.Run("Ready message printed only once when service stays running", func(t *testing.T) {
442+
ctx, cancel, logger, buffer := setup(t)
443+
defer cancel()
444+
445+
wf := &types.Workflow{
446+
Tasks: map[string]types.Task{
447+
"service": {Command: []string{"sleep", "30"}, Type: types.TaskTypeService},
448+
},
449+
}
450+
wg := &sync.WaitGroup{}
451+
wg.Add(1)
452+
go func() {
453+
defer wg.Done()
454+
err := RunSubgraph(ctx, cancel, 0, false, logger, wf, []string{"service"}, nil)
455+
assert.NoError(t, err)
456+
}()
457+
458+
sleep(t)
459+
cancel()
460+
wg.Wait()
461+
462+
// The ready message should appear exactly once
463+
count := strings.Count(buffer.String(), "🔵 all requested tasks are running:")
464+
assert.Equal(t, 1, count)
465+
})
466+
467+
t.Run("Ready message printed again after service restarts", func(t *testing.T) {
468+
ctx, cancel, logger, buffer := setup(t)
469+
defer cancel()
470+
471+
wf := &types.Workflow{
472+
Tasks: map[string]types.Task{
473+
"service": {
474+
Command: []string{"sleep", "30"},
475+
Watch: []string{"testdata/marker"},
476+
Type: types.TaskTypeService,
477+
},
478+
},
479+
}
480+
481+
wg := &sync.WaitGroup{}
482+
wg.Add(1)
483+
go func() {
484+
defer wg.Done()
485+
err := RunSubgraph(ctx, cancel, 0, false, logger, wf, []string{"service"}, nil)
486+
assert.NoError(t, err)
487+
}()
488+
489+
sleep(t)
490+
491+
// Trigger a restart by modifying the watched file
492+
err := os.WriteFile("testdata/marker", nil, 0644)
493+
assert.NoError(t, err)
494+
495+
sleep(t)
496+
497+
cancel()
498+
wg.Wait()
499+
500+
// The ready message should appear twice: once initially, once after restart
501+
count := strings.Count(buffer.String(), "🔵 all requested tasks are running:")
502+
assert.Equal(t, 2, count)
503+
})
440504
}
441505

442506
func sleep(t *testing.T) {

0 commit comments

Comments
 (0)