Skip to content

Commit

Permalink
internal/runner: recover on panic, fail task
Browse files Browse the repository at this point in the history
This makes sure the task is not left in an active state if the task
handlers panic. Going ahead I'd like to move this into the controller library, for
now this can be here.
  • Loading branch information
joelrebel committed Apr 18, 2024
1 parent ef1343a commit ce92796
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"context"
"runtime/debug"
"time"

"github.com/metal-toolbox/flasher/internal/model"
Expand Down Expand Up @@ -66,6 +67,15 @@ func (r *Runner) RunTask(ctx context.Context, task *model.Task, handler Handler)
return nil
}

defer func() error {
if rec := recover(); rec != nil {
r.logger.Printf("!!panic %s: %s", rec, debug.Stack())
r.logger.Error("Panic occurred while running task")
return taskFailed(errors.New("Task fatal error, check logs for details"))
}
return nil
}() // nolint:errcheck // nope

// no error returned
_ = task.SetState(model.StateActive)
handler.Publish()
Expand Down

0 comments on commit ce92796

Please sign in to comment.