Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cmd/supervisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ func realMain() (code int) {
apiSrv := api.NewServer(*addr, super, sys, *glassBin, *dataDir, log)
webSrv := web.NewServer()

if err = super.Start(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not start supervisor: %v\n", err)
return 1
}
superCh := make(chan struct{})
go func() {
defer close(superCh)

super.Run(ctx)
}()

mux := http.NewServeMux()
mux.Handle("GET /{$}", webSrv)
Expand All @@ -102,6 +104,9 @@ func realMain() (code int) {
_, _ = fmt.Fprintf(os.Stderr, "Could not run server: %v\n", err)
return 1
}

<-superCh

return 0
}

Expand Down
43 changes: 19 additions & 24 deletions proc/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,25 @@ func New(exe *exec.Executable, opts ...Option) *Supervisor {
return s
}

// Start begins the supervision loop in a background goroutine.
func (s *Supervisor) Start(ctx context.Context) error {
go s.loop(ctx)
return nil
// Run runs the supervision loop.
func (s *Supervisor) Run(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
}

if !s.waitForCondition(ctx) {
return
}

if err := s.run(ctx); err != nil && !errors.Is(err, context.Canceled) {
_, _ = s.ring.Write([]byte("Process exited with error: " + err.Error() + "\n"))
}

s.restarts.Add(1)
}
}

// Restart sends SIGTERM to the running process. The supervision loop restarts it.
Expand Down Expand Up @@ -109,26 +124,6 @@ func (s *Supervisor) Follow(ctx context.Context) <-chan string {
return s.ring.Stream(ctx)
}

func (s *Supervisor) loop(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
}

if !s.waitForCondition(ctx) {
return
}

if err := s.run(ctx); err != nil && !errors.Is(err, context.Canceled) {
_, _ = s.ring.Write([]byte("Process exited with error: " + err.Error() + "\n"))
}

s.restarts.Add(1)
}
}

func (s *Supervisor) waitForCondition(ctx context.Context) bool {
if s.cond == nil {
return true
Expand Down
18 changes: 9 additions & 9 deletions proc/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSupervisor_Info(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

require.Eventually(t, func() bool {
return sup.Info().PID > 0
Expand All @@ -56,7 +56,7 @@ func TestSupervisor_Lines(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

require.Eventually(t, func() bool {
lines := sup.Lines()
Expand All @@ -73,7 +73,7 @@ func TestSupervisor_Follow(t *testing.T) {
exe := exectest.New(t, "echo", "streamed line")
sup := proc.New(exe)

require.NoError(t, sup.Start(t.Context()))
go sup.Run(t.Context())

ch := sup.Follow(t.Context())

Expand Down Expand Up @@ -107,7 +107,7 @@ func TestSupervisor_Restart(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

require.Eventually(t, func() bool {
return sup.Info().PID > 0
Expand All @@ -132,7 +132,7 @@ func TestSupervisor_RestartIncrementsCount(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

require.Eventually(t, func() bool {
return sup.Info().Restarts > 0
Expand All @@ -149,7 +149,7 @@ func TestSupervisor_ConditionHoldsProcess(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

// Give the supervisor time to attempt a start; the process must not start.
time.Sleep(100 * time.Millisecond)
Expand All @@ -167,7 +167,7 @@ func TestSupervisor_ConditionWritesReasonToBuffer(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

require.Eventually(t, func() bool {
for _, line := range sup.Lines() {
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestSupervisor_StartsWhenConditionPasses(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

time.Sleep(100 * time.Millisecond)
assert.Equal(t, 0, sup.Info().PID, "process should not have started yet")
Expand All @@ -218,7 +218,7 @@ func TestSupervisor_StopsOnContextCancel(t *testing.T) {

ctx, cancel := context.WithCancel(t.Context())

require.NoError(t, sup.Start(ctx))
go sup.Run(ctx)

require.Eventually(t, func() bool {
return sup.Info().PID > 0
Expand Down
Loading