feat(process): add cleanup tasks to core service

This commit is contained in:
Virgil 2026-04-04 05:49:58 +00:00
parent f717fc66c3
commit dcf20c78c8
3 changed files with 67 additions and 0 deletions

View file

@ -142,6 +142,23 @@ type TaskProcessList struct {
RunningOnly bool
}
// TaskProcessRemove removes a completed managed process through Core.PERFORM.
//
// Example:
//
// c.PERFORM(process.TaskProcessRemove{ID: "proc-1"})
type TaskProcessRemove struct {
// ID identifies a managed process started by this service.
ID string
}
// TaskProcessClear removes all completed managed processes through Core.PERFORM.
//
// Example:
//
// c.PERFORM(process.TaskProcessClear{})
type TaskProcessClear struct{}
// ActionProcessStarted is broadcast when a process begins execution.
//
// Example:

View file

@ -783,6 +783,19 @@ func (s *Service) handleTask(c *core.Core, task core.Task) core.Result {
}
return core.Result{Value: infos, OK: true}
case TaskProcessRemove:
if m.ID == "" {
return core.Result{Value: coreerr.E("Service.handleTask", "task process remove requires an id", nil), OK: false}
}
if err := s.Remove(m.ID); err != nil {
return core.Result{Value: err, OK: false}
}
return core.Result{OK: true}
case TaskProcessClear:
s.Clear()
return core.Result{OK: true}
default:
return core.Result{}
}

View file

@ -957,6 +957,43 @@ func TestService_OnStartup(t *testing.T) {
assert.Equal(t, proc.Info().PID, info.PID)
})
t.Run("registers process.remove task", func(t *testing.T) {
svc, c := newTestService(t)
err := svc.OnStartup(context.Background())
require.NoError(t, err)
proc, err := svc.Start(context.Background(), "echo", "remove-through-core")
require.NoError(t, err)
<-proc.Done()
result := c.PERFORM(TaskProcessRemove{ID: proc.ID})
require.True(t, result.OK)
_, err = svc.Get(proc.ID)
assert.ErrorIs(t, err, ErrProcessNotFound)
})
t.Run("registers process.clear task", func(t *testing.T) {
svc, c := newTestService(t)
err := svc.OnStartup(context.Background())
require.NoError(t, err)
first, err := svc.Start(context.Background(), "echo", "clear-through-core-1")
require.NoError(t, err)
second, err := svc.Start(context.Background(), "echo", "clear-through-core-2")
require.NoError(t, err)
<-first.Done()
<-second.Done()
require.Len(t, svc.List(), 2)
result := c.PERFORM(TaskProcessClear{})
require.True(t, result.OK)
assert.Len(t, svc.List(), 0)
})
t.Run("registers process.output task", func(t *testing.T) {
svc, c := newTestService(t)