diff --git a/scheduler.go b/scheduler.go index ad2f8db7..a9e0fc5a 100644 --- a/scheduler.go +++ b/scheduler.go @@ -535,12 +535,25 @@ func (s *scheduler) NewJob(jobDefinition JobDefinition, task Task, options ...Jo return s.addOrUpdateJob(uuid.Nil, jobDefinition, task, options) } +func (s *scheduler) verifyInterfaceVariadic(taskFunc reflect.Value, tsk task, variadicStart int) error { + ifaceType := taskFunc.Type().In(variadicStart).Elem() + for i := variadicStart; i < len(tsk.parameters); i++ { + if !reflect.TypeOf(tsk.parameters[i]).Implements(ifaceType) { + return ErrNewJobWrongTypeOfParameters + } + } + return nil +} + func (s *scheduler) verifyVariadic(taskFunc reflect.Value, tsk task, variadicStart int) error { if err := s.verifyNonVariadic(taskFunc, tsk, variadicStart); err != nil { return err } parameterType := taskFunc.Type().In(variadicStart).Elem().Kind() - if parameterType == reflect.Interface || parameterType == reflect.Pointer { + if parameterType == reflect.Interface { + return s.verifyInterfaceVariadic(taskFunc, tsk, variadicStart) + } + if parameterType == reflect.Pointer { parameterType = reflect.Indirect(reflect.ValueOf(taskFunc.Type().In(variadicStart))).Kind() } diff --git a/scheduler_test.go b/scheduler_test.go index 52fb8e2f..e55b47b7 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "os" "sync" "sync/atomic" @@ -924,6 +925,16 @@ func TestScheduler_NewJobTask(t *testing.T) { NewTask(func(args ...interface{}) {}), nil, }, + { + "all good - interface variadic, int, string", + NewTask(func(args ...interface{}) {}, 1, "2", 3.0), + nil, + }, + { + "parameter type does not match - different argument types against interface variadic parameters", + NewTask(func(args ...io.Reader) {}, os.Stdout, any(3.0)), + ErrNewJobWrongTypeOfParameters, + }, } for _, tt := range tests {