github.com/ngicks/gokugen@v0.0.5/context_builder.go (about) 1 package gokugen 2 3 import "time" 4 5 type Option func(ctx SchedulerContext) SchedulerContext 6 7 // BuildContext builds a new SchedulerContext. 8 // workFn, valMap can be nil. 9 // Add options to set additional values to the ctx. 10 func BuildContext(scheduledTime time.Time, workFn WorkFn, valMap map[any]any, options ...Option) SchedulerContext { 11 var ctx SchedulerContext = NewPlainContext(scheduledTime, workFn, valMap) 12 return WrapContext(ctx, options...) 13 } 14 15 // WrapContext wrapps parent with options. 16 func WrapContext(parent SchedulerContext, options ...Option) (ctx SchedulerContext) { 17 ctx = parent 18 for _, opt := range options { 19 ctx = opt(ctx) 20 } 21 return ctx 22 } 23 24 func WithParam(param any) Option { 25 return func(ctx SchedulerContext) SchedulerContext { 26 return WrapWithParam(ctx, param) 27 } 28 } 29 func WithParamLoader(loader func() (any, error)) Option { 30 return func(ctx SchedulerContext) SchedulerContext { 31 return WrapWithParamLoader(ctx, loader) 32 } 33 } 34 func WithWorkFn(workFn WorkFn) Option { 35 return func(ctx SchedulerContext) SchedulerContext { 36 return WrapWithWorkFn(ctx, workFn) 37 } 38 } 39 func WithWorkFnWrapper(wrapper WorkFnWrapper) Option { 40 return func(ctx SchedulerContext) SchedulerContext { 41 return WrapWithWorkFnWrapper(ctx, wrapper) 42 } 43 } 44 func WithTaskId(taskId string) Option { 45 return func(ctx SchedulerContext) SchedulerContext { 46 return WrapWithTaskId(ctx, taskId) 47 } 48 } 49 func WithWorkId(workId string) Option { 50 return func(ctx SchedulerContext) SchedulerContext { 51 return WrapWithWorkId(ctx, workId) 52 } 53 }