diff --git a/service.go b/service.go index 1c3c5b4..d0c1f14 100644 --- a/service.go +++ b/service.go @@ -217,18 +217,25 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce if err := cmd.Start(); err != nil { proc.mu.Lock() proc.Status = StatusFailed + proc.ExitCode = -1 + proc.Duration = time.Since(startedAt) proc.mu.Unlock() + s.mu.Lock() + s.processes[id] = proc + s.mu.Unlock() + + close(proc.done) cancel() if c := s.coreApp(); c != nil { _ = c.ACTION(ActionProcessExited{ ID: id, ExitCode: -1, - Duration: time.Since(startedAt), + Duration: proc.Duration, Error: err, }) } - return nil, coreerr.E("Service.StartWithOptions", "failed to start process", err) + return proc, coreerr.E("Service.StartWithOptions", "failed to start process", err) } proc.mu.Lock() diff --git a/service_test.go b/service_test.go index 3390e71..9482a55 100644 --- a/service_test.go +++ b/service_test.go @@ -78,8 +78,18 @@ func TestService_Start(t *testing.T) { t.Run("non-existent command", func(t *testing.T) { svc, _ := newTestService(t) - _, err := svc.Start(context.Background(), "nonexistent_command_xyz") + proc, err := svc.Start(context.Background(), "nonexistent_command_xyz") assert.Error(t, err) + require.NotNil(t, proc) + assert.Equal(t, StatusFailed, proc.Status) + assert.Equal(t, -1, proc.ExitCode) + assert.NotNil(t, proc.Done()) + <-proc.Done() + + got, getErr := svc.Get(proc.ID) + require.NoError(t, getErr) + assert.Equal(t, proc.ID, got.ID) + assert.Equal(t, StatusFailed, got.Status) }) t.Run("empty command is rejected", func(t *testing.T) {