diff --git a/example_test.go b/example_test.go index 9f0574b3..4bc9b76b 100644 --- a/example_test.go +++ b/example_test.go @@ -874,7 +874,7 @@ func ExampleScheduler_Weeks() { // --------------------------------------------------------------------- func ExampleSetPanicHandler() { - gocron.SetPanicHandler(func(jobName string, recoverData interface{}) { + gocron.SetPanicHandler(func(jobName string, _ any) { fmt.Printf("Panic in job: %s", jobName) fmt.Println("do something to handle the panic") }) diff --git a/executor.go b/executor.go index f4e27c7b..cf87ff21 100644 --- a/executor.go +++ b/executor.go @@ -56,7 +56,7 @@ func (e *executor) start() { if panicHandler != nil { defer func() { - if r := recover(); r != interface{}(nil) { + if r := recover(); r != any(nil) { panicHandler(f.name, r) } }() @@ -99,7 +99,7 @@ func (e *executor) start() { case defaultMode: runJob() case singletonMode: - _, _, _ = f.limiter.Do("main", func() (interface{}, error) { + _, _, _ = f.limiter.Do("main", func() (any, error) { select { case <-stopCtx.Done(): return nil, nil diff --git a/executor_test.go b/executor_test.go index 0536b26e..af8fbb7c 100644 --- a/executor_test.go +++ b/executor_test.go @@ -22,7 +22,7 @@ func Test_ExecutorExecute(t *testing.T) { assert.Equal(t, arg, "test") wg.Done() }, - parameters: []interface{}{"test"}, + parameters: []any{"test"}, runState: &runState, } @@ -33,7 +33,7 @@ func Test_ExecutorExecute(t *testing.T) { func Test_ExecutorPanicHandling(t *testing.T) { panicHandled := make(chan bool, 1) - handler := func(jobName string, recoverData interface{}) { + handler := func(jobName string, recoverData any) { fmt.Println("PanicHandler called:") fmt.Println("panic in " + jobName) fmt.Println(recoverData) diff --git a/gocron.go b/gocron.go index 5a9afc43..c512ca85 100644 --- a/gocron.go +++ b/gocron.go @@ -17,7 +17,7 @@ import ( // PanicHandlerFunc represents a type that can be set to handle panics occurring // during job execution. -type PanicHandlerFunc func(jobName string, recoverData interface{}) +type PanicHandlerFunc func(jobName string, recoverData any) // The global panic handler var ( @@ -88,13 +88,13 @@ const ( crontab ) -func callJobFunc(jobFunc interface{}) { +func callJobFunc(jobFunc any) { if jobFunc != nil { reflect.ValueOf(jobFunc).Call([]reflect.Value{}) } } -func callJobFuncWithParams(jobFunc interface{}, params []interface{}) { +func callJobFuncWithParams(jobFunc any, params []any) { f := reflect.ValueOf(jobFunc) if len(params) != f.Type().NumIn() { return @@ -106,7 +106,7 @@ func callJobFuncWithParams(jobFunc interface{}, params []interface{}) { f.Call(in) } -func getFunctionName(fn interface{}) string { +func getFunctionName(fn any) string { return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() } diff --git a/gocron_test.go b/gocron_test.go index 6815106b..a4d6eb93 100644 --- a/gocron_test.go +++ b/gocron_test.go @@ -109,8 +109,8 @@ func TestParseTime(t *testing.T) { func Test_callJobFuncWithParams(t *testing.T) { type args struct { - jobFunc interface{} - params []interface{} + jobFunc any + params []any } tests := []struct { name string @@ -128,14 +128,14 @@ func Test_callJobFuncWithParams(t *testing.T) { name: "test call func with single arg", args: args{ jobFunc: func(arg string) {}, - params: []interface{}{"test"}, + params: []any{"test"}, }, }, { name: "test call func with wrong arg type", args: args{ jobFunc: func(arg int) {}, - params: []interface{}{"test"}, + params: []any{"test"}, }, err: true, }, @@ -143,7 +143,7 @@ func Test_callJobFuncWithParams(t *testing.T) { name: "test call func with wrong arg count", args: args{ jobFunc: func(arg int) {}, - params: []interface{}{}, + params: []any{}, }, err: true, }, @@ -174,7 +174,7 @@ func panicFnToErr(fn func()) (err error) { func Test_getFunctionName(t *testing.T) { type args struct { - fn interface{} + fn any } tests := []struct { name string diff --git a/job.go b/job.go index 136be1e6..5174b5a1 100644 --- a/job.go +++ b/job.go @@ -44,10 +44,10 @@ type random struct { type jobFunction struct { eventListeners // additional functions to allow run 'em during job performing - function interface{} // task's function - parameters []interface{} // task's function parameters + function any // task's function + parameters []any // task's function parameters parametersLen int // length of the passed parameters - name string //nolint the function name to run + name string // nolint the function name to run runConfig runConfig // configuration for how many times to run the job limiter *singleflight.Group // limits inflight runs of job to one ctx context.Context // for cancellation @@ -56,8 +56,8 @@ type jobFunction struct { } type eventListeners struct { - onBeforeJobExecution interface{} // performs before job executing - onAfterJobExecution interface{} // performs after job executing + onBeforeJobExecution any // performs before job executing + onAfterJobExecution any // performs after job executing } type jobMutex struct { @@ -318,7 +318,7 @@ func (j *Job) Tags() []string { } // SetEventListeners accepts two functions that will be called, one before and one after the job is run -func (j *Job) SetEventListeners(onBeforeJobExecution interface{}, onAfterJobExecution interface{}) { +func (j *Job) SetEventListeners(onBeforeJobExecution any, onAfterJobExecution any) { j.eventListeners = eventListeners{ onBeforeJobExecution: onBeforeJobExecution, onAfterJobExecution: onAfterJobExecution, diff --git a/job_test.go b/job_test.go index e5aa33ac..616afd37 100644 --- a/job_test.go +++ b/job_test.go @@ -236,7 +236,7 @@ func TestJob_SetEventListeners(t *testing.T) { ) wg.Add(1) s := NewScheduler(time.UTC) - job, err := s.Tag("tag1").Every("1ms").Do(func() { + job, err := s.Tag("tag1").Every("100ms").Do(func() { jobRanPassed = true }) job.SetEventListeners(func() { diff --git a/scheduler.go b/scheduler.go index 508f4323..e89f838f 100644 --- a/scheduler.go +++ b/scheduler.go @@ -16,7 +16,7 @@ import ( type limitMode int8 // Scheduler struct stores a list of Jobs and the location of time used by the Scheduler, -// and implements the sort.Interface{} for sorting Jobs, by the time of nextRun +// and implements the sort. any for sorting Jobs, by the time of nextRun type Scheduler struct { jobsMutex sync.RWMutex jobs []*Job @@ -494,7 +494,7 @@ func (s *Scheduler) EveryRandom(lower, upper int) *Scheduler { // Interval can be an int, time.Duration or a string that // parses with time.ParseDuration(). // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". -func (s *Scheduler) Every(interval interface{}) *Scheduler { +func (s *Scheduler) Every(interval any) *Scheduler { job := s.newJob(0) if s.updateJob || s.jobCreated { job = s.getCurrentJob() @@ -641,7 +641,7 @@ func (s *Scheduler) RunByTagWithDelay(tag string, d time.Duration) error { // it through gocron as https://pkg.go.dev/time#Timer.Stop explains. // The job function would need to have implemented a means of // stopping, e.g. using a context.WithCancel(). -func (s *Scheduler) Remove(job interface{}) { +func (s *Scheduler) Remove(job any) { fName := getFunctionName(job) j := s.findJobByTaskName(fName) s.removeJobsUniqueTags(j) @@ -783,7 +783,7 @@ func (s *Scheduler) SingletonModeAll() { } // TaskPresent checks if specific job's function was added to the scheduler. -func (s *Scheduler) TaskPresent(j interface{}) bool { +func (s *Scheduler) TaskPresent(j any) bool { for _, job := range s.Jobs() { if job.name == getFunctionName(j) { return true @@ -817,7 +817,7 @@ func (s *Scheduler) Clear() { s.setJobs(make([]*Job, 0)) // If unique tags was enabled, delete all the tags loaded in the tags sync.Map if s.tagsUnique { - s.tags.Range(func(key interface{}, value interface{}) bool { + s.tags.Range(func(key any, value any) bool { s.tags.Delete(key) return true }) @@ -845,7 +845,7 @@ func (s *Scheduler) stopJobs(jobs []*Job) { } } -func (s *Scheduler) doCommon(jobFun interface{}, params ...interface{}) (*Job, error) { +func (s *Scheduler) doCommon(jobFun any, params ...any) (*Job, error) { job := s.getCurrentJob() jobUnit := job.getUnit() @@ -912,7 +912,7 @@ func (s *Scheduler) doCommon(jobFun interface{}, params ...interface{}) (*Job, e } // Do specifies the jobFunc that should be called every time the Job runs -func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error) { +func (s *Scheduler) Do(jobFun any, params ...any) (*Job, error) { return s.doCommon(jobFun, params...) } @@ -920,7 +920,7 @@ func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error) // and additionally passes the details of the current job to the jobFunc. // The last argument of the function must be a gocron.Job that will be passed by // the scheduler when the function is called. -func (s *Scheduler) DoWithJobDetails(jobFun interface{}, params ...interface{}) (*Job, error) { +func (s *Scheduler) DoWithJobDetails(jobFun any, params ...any) (*Job, error) { job := s.getCurrentJob() job.runWithDetails = true job.parametersLen = len(params) @@ -929,7 +929,7 @@ func (s *Scheduler) DoWithJobDetails(jobFun interface{}, params ...interface{}) // At schedules the Job at a specific time of day in the form "HH:MM:SS" or "HH:MM" // or time.Time (note that only the hours, minutes, seconds and nanos are used). -func (s *Scheduler) At(i interface{}) *Scheduler { +func (s *Scheduler) At(i any) *Scheduler { job := s.getCurrentJob() switch t := i.(type) { diff --git a/scheduler_test.go b/scheduler_test.go index f4f3e89e..60d10d86 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -57,7 +57,7 @@ func TestImmediateExecution(t *testing.T) { func TestScheduler_Every_InvalidInterval(t *testing.T) { testCases := []struct { description string - interval interface{} + interval any expectedError string }{ {"zero", 0, ErrInvalidInterval.Error()}, @@ -271,27 +271,27 @@ func TestMultipleAtTimesDecoding(t *testing.T) { exp := []time.Duration{_getHours(1), _getHours(3), _getHours(4), _getHours(7), _getHours(15)} testCases := []struct { name string - params []interface{} + params []any result []time.Duration }{ { name: "multiple simple strings", - params: []interface{}{"03:00", "15:00", "01:00", "07:00", "04:00"}, + params: []any{"03:00", "15:00", "01:00", "07:00", "04:00"}, result: exp, }, { name: "single string separated by semicolons", - params: []interface{}{"03:00;15:00;01:00;07:00;04:00"}, + params: []any{"03:00;15:00;01:00;07:00;04:00"}, result: exp, }, { name: "interpolation of semicolons string, time.Time and simple string", - params: []interface{}{"03:00;15:00;01:00", time.Date(0, 0, 0, 7, 0, 0, 0, time.UTC), "04:00"}, + params: []any{"03:00;15:00;01:00", time.Date(0, 0, 0, 7, 0, 0, 0, time.UTC), "04:00"}, result: exp, }, { name: "repeated values on input don't get duplicated after decoding", - params: []interface{}{"03:00;15:00;01:00;07:00;04:00;01:00"}, + params: []any{"03:00;15:00;01:00;07:00;04:00;01:00"}, result: exp, }, } @@ -773,7 +773,7 @@ func TestClearUnique(t *testing.T) { // be stopped on s.Clear() assert.Equal(t, 1, counter) - s.tags.Range(func(key, value interface{}) bool { + s.tags.Range(func(key, value any) bool { assert.FailNow(t, "map should be empty") return true }) @@ -1177,7 +1177,7 @@ func TestRunJobsWithLimit(t *testing.T) { require.LessOrEqual(t, counter, 1) } - s.tags.Range(func(key, value interface{}) bool { + s.tags.Range(func(key, value any) bool { assert.FailNow(t, "map should be empty") return true }) @@ -1535,10 +1535,10 @@ func TestScheduler_MultipleTagsChained(t *testing.T) { func TestScheduler_DoParameterValidation(t *testing.T) { testCases := []struct { description string - parameters []interface{} + parameters []any }{ - {"less than expected", []interface{}{"p1"}}, - {"more than expected", []interface{}{"p1", "p2", "p3"}}, + {"less than expected", []any{"p1"}}, + {"more than expected", []any{"p1", "p2", "p3"}}, } for _, tc := range testCases { @@ -2195,15 +2195,15 @@ func TestScheduler_MultipleAtTime(t *testing.T) { func TestScheduler_DoWithJobDetails(t *testing.T) { testCases := []struct { description string - jobFunc interface{} - params []interface{} + jobFunc any + params []any expectedError string }{ - {"no error", func(foo, bar string, job Job) {}, []interface{}{"foo", "bar"}, ""}, - {"too few params", func(foo, bar string, job Job) {}, []interface{}{"foo"}, ErrWrongParams.Error()}, - {"too many params", func(foo, bar string, job Job) {}, []interface{}{"foo", "bar", "baz"}, ErrWrongParams.Error()}, - {"jobFunc doesn't have Job param", func(foo, bar string) {}, []interface{}{"foo"}, ErrDoWithJobDetails.Error()}, - {"jobFunc has Job param but not last param", func(job Job, foo, bar string) {}, []interface{}{"foo", "bar"}, ErrDoWithJobDetails.Error()}, + {"no error", func(foo, bar string, job Job) {}, []any{"foo", "bar"}, ""}, + {"too few params", func(foo, bar string, job Job) {}, []any{"foo"}, ErrWrongParams.Error()}, + {"too many params", func(foo, bar string, job Job) {}, []any{"foo", "bar", "baz"}, ErrWrongParams.Error()}, + {"jobFunc doesn't have Job param", func(foo, bar string) {}, []any{"foo"}, ErrDoWithJobDetails.Error()}, + {"jobFunc has Job param but not last param", func(job Job, foo, bar string) {}, []any{"foo", "bar"}, ErrDoWithJobDetails.Error()}, } for _, tc := range testCases {