Skip to content

Commit

Permalink
feat(scheduler): add error logging on run interruption
Browse files Browse the repository at this point in the history
  • Loading branch information
karol-kokoszka committed Mar 29, 2024
1 parent 9a8ca9f commit 4a19989
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
34 changes: 30 additions & 4 deletions pkg/scheduler/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package scheduler
import (
"context"
"time"

"github.com/scylladb/go-log"
)

// Listener specifies pluggable hooks for scheduler events.
Expand All @@ -14,8 +16,8 @@ type Listener[K comparable] interface {
OnSchedulerStop(context.Context)
OnRunStart(ctx *RunContext[K])
OnRunSuccess(ctx *RunContext[K])
OnRunStop(ctx *RunContext[K])
OnRunWindowEnd(ctx *RunContext[K])
OnRunStop(ctx *RunContext[K], err error)
OnRunWindowEnd(ctx *RunContext[K], err error)
OnRunError(ctx *RunContext[K], err error)
OnSchedule(ctx context.Context, key K, begin, end time.Time, retno int8)
OnUnschedule(ctx context.Context, key K)
Expand All @@ -40,10 +42,10 @@ func (l nopListener[K]) OnRunStart(*RunContext[K]) {
func (l nopListener[K]) OnRunSuccess(*RunContext[K]) {
}

func (l nopListener[K]) OnRunStop(*RunContext[K]) {
func (l nopListener[K]) OnRunStop(*RunContext[K], error) {
}

func (l nopListener[K]) OnRunWindowEnd(*RunContext[K]) {
func (l nopListener[K]) OnRunWindowEnd(*RunContext[K], error) {
}

func (l nopListener[K]) OnRunError(*RunContext[K], error) {
Expand Down Expand Up @@ -74,3 +76,27 @@ func (l nopListener[K]) OnSleep(context.Context, K, time.Duration) {
func NopListener[K comparable]() Listener[K] {
return nopListener[K]{}
}

type errorLogListener[K comparable] struct {
nopListener[K]
logger log.Logger
}

func (l errorLogListener[K]) OnRunError(ctx *RunContext[K], err error) {
l.logger.Error(ctx, "OnRunError", "key", ctx.Key, "retry", ctx.Retry, "error", err)
}

func (l errorLogListener[K]) OnRunWindowEnd(ctx *RunContext[K], err error) {
l.logger.Info(ctx, "OnRunWindowEnd", "key", ctx.Key, "retry", ctx.Retry, "error", err)
}

func (l errorLogListener[K]) OnRunStop(ctx *RunContext[K], err error) {
l.logger.Info(ctx, "OnRunStop", "key", ctx.Key, "retry", ctx.Retry, "error", err)
}

// ErrorLogListener returns listener that logs errors.
func ErrorLogListener[K comparable](logger log.Logger) Listener[K] {
return errorLogListener[K]{
logger: logger,
}
}
4 changes: 2 additions & 2 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ func (s *Scheduler[K]) onRunEnd(ctx *RunContext[K]) {
case err == nil:
s.listener.OnRunSuccess(ctx)
case errors.Is(err, context.Canceled):
s.listener.OnRunStop(ctx)
s.listener.OnRunStop(ctx, err)
case errors.Is(err, context.DeadlineExceeded):
s.listener.OnRunWindowEnd(ctx)
s.listener.OnRunWindowEnd(ctx, err)
default:
s.listener.OnRunError(ctx, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ func (l logListener) OnRunSuccess(ctx *testRunContext) {
l.logger.Info(ctx, "OnRunSuccess", "key", ctx.Key, "retry", ctx.Retry)
}

func (l logListener) OnRunStop(ctx *testRunContext) {
func (l logListener) OnRunStop(ctx *testRunContext, err error) {
l.logger.Info(ctx, "OnRunStop", "key", ctx.Key, "retry", ctx.Retry)
}

func (l logListener) OnRunWindowEnd(ctx *testRunContext) {
func (l logListener) OnRunWindowEnd(ctx *testRunContext, err error) {
l.logger.Info(ctx, "OnRunWindowEnd", "key", ctx.Key, "retry", ctx.Retry)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/service/scheduler/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type schedulerListener struct {

func newSchedulerListener(find func(key Key) (taskInfo, bool), logger log.Logger) schedulerListener {
return schedulerListener{
Listener: scheduler.NopListener[Key](),
Listener: scheduler.ErrorLogListener[Key](logger),
find: find,
logger: logger,
}
Expand Down

0 comments on commit 4a19989

Please sign in to comment.