Skip to content

update required go version to latest #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2025
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
31 changes: 15 additions & 16 deletions examples/composite/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ func (w *Worker) String() string {
// Run starts the worker's main loop.
func (w *Worker) Run(ctx context.Context) error {
logger := w.logger.WithGroup("Run")
w.mu.Lock()
runCtx, cancel := context.WithCancel(ctx)
defer cancel()

w.mu.Lock()
w.ctx = runCtx
w.cancel = cancel
w.mu.Unlock()
Expand All @@ -101,17 +103,11 @@ func (w *Worker) Run(ctx context.Context) error {
logger.Info("Starting worker")

w.startTicker(runCtx, cfg.Interval)
defer func() {
if tc := w.tickerCancel; tc != nil {
tc()
}
logger.Info("Worker stopped")
}()

for {
select {
case <-runCtx.Done():
logger.Debug("Worker context cancelled, shutting down")
logger.Info("Worker stopped")
return nil
case <-w.tickChan:
w.processTick()
Expand All @@ -125,9 +121,11 @@ func (w *Worker) Run(ctx context.Context) error {

// Stop signals the worker to gracefully shut down by cancelling its context.
func (w *Worker) Stop() {
logger := w.logger.WithGroup("Stop")
currentName := w.getConfig().JobName
logger.Info("Stopping worker...", "name", currentName)
logger := w.logger.WithGroup("Stop").With("name", currentName)
logger.Info("Stopping worker...")
w.mu.Lock()
defer w.mu.Unlock()
if c := w.cancel; c != nil {
w.cancel = nil
c()
Expand All @@ -138,16 +136,17 @@ func (w *Worker) Stop() {
// startTicker starts a new ticker goroutine that sends tick notifications to the worker.
func (w *Worker) startTicker(ctx context.Context, interval time.Duration) {
logger := w.logger.WithGroup("startTicker")

// Cancel old ticker before creating new one, all under lock
w.mu.Lock()
oldTickerCancel := w.tickerCancel
w.tickerCancel = nil
w.mu.Unlock()
if oldTickerCancel != nil {
if w.tickerCancel != nil {
logger.Debug("Cancelling previous ticker goroutine")
oldTickerCancel()
w.tickerCancel()
w.tickerCancel = nil
}

// Create new ticker context and store cancel function
tickCtx, newTickerCancel := context.WithCancel(ctx)
w.mu.Lock()
w.tickerCancel = newTickerCancel
w.mu.Unlock()
ticker := time.NewTicker(interval)
Expand Down
8 changes: 5 additions & 3 deletions examples/http/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ func TestRunServerInvalidPort(t *testing.T) {
)
require.NoError(t, err, "Failed to create supervisor")

// Run the supervisor - should fail because of invalid port
err = sv.Run()
assert.Error(t, err, "Run should fail with invalid port")
assert.Contains(t, err.Error(), "timeout waiting for runnable to start")
require.Error(t, err, "Run() should fail with invalid port")
assert.ErrorIs(t,
err, httpserver.ErrServerBoot,
"httpserver.Runner.Run() should return ErrServerBoot",
)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/robbyt/go-supervisor

go 1.24.3
go 1.24.4

require (
github.com/robbyt/go-fsm v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions runnables/httpserver/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func (r *Runner) serverReadinessProbe(ctx context.Context, addr string) error {

for {
select {
case err := <-r.serverErrors:
return fmt.Errorf("server failed to start: %w", err)
case <-probeCtx.Done():
return fmt.Errorf("%w: %w", ErrServerReadinessTimeout, probeCtx.Err())
case <-ticker.C:
Expand Down