diff --git a/global_test.go b/global_test.go index f024eb0..aef2035 100644 --- a/global_test.go +++ b/global_test.go @@ -36,6 +36,12 @@ func TestGlobal_DefaultNotInitialized(t *testing.T) { assert.Nil(t, List()) assert.Nil(t, Running()) + err = Remove("proc-1") + assert.ErrorIs(t, err, ErrServiceNotInitialized) + + // Clear is a no-op without a default service. + Clear() + err = Kill("proc-1") assert.ErrorIs(t, err, ErrServiceNotInitialized) @@ -290,3 +296,33 @@ func TestGlobal_Running(t *testing.T) { running = Running() assert.Len(t, running, 0) } + +func TestGlobal_RemoveAndClear(t *testing.T) { + svc, _ := newTestService(t) + + old := defaultService.Swap(svc) + defer func() { + if old != nil { + defaultService.Store(old) + } + }() + + proc, err := Start(context.Background(), "echo", "remove-me") + require.NoError(t, err) + <-proc.Done() + + err = Remove(proc.ID) + require.NoError(t, err) + + _, err = Get(proc.ID) + require.ErrorIs(t, err, ErrProcessNotFound) + + proc2, err := Start(context.Background(), "echo", "clear-me") + require.NoError(t, err) + <-proc2.Done() + + Clear() + + _, err = Get(proc2.ID) + require.ErrorIs(t, err, ErrProcessNotFound) +} diff --git a/process_global.go b/process_global.go index 8cf1022..a89580e 100644 --- a/process_global.go +++ b/process_global.go @@ -191,6 +191,32 @@ func Running() []*Process { return svc.Running() } +// Remove removes a completed process from the default service. +// +// Example: +// +// _ = process.Remove("proc-1") +func Remove(id string) error { + svc := Default() + if svc == nil { + return ErrServiceNotInitialized + } + return svc.Remove(id) +} + +// Clear removes all completed processes from the default service. +// +// Example: +// +// process.Clear() +func Clear() { + svc := Default() + if svc == nil { + return + } + svc.Clear() +} + // Errors var ( // ErrServiceNotInitialized is returned when the service is not initialized.