Skip to content

Commit

Permalink
feat: handle pointer to func (#531)
Browse files Browse the repository at this point in the history
* feat: handle pointer to func

* refactor: update

* refactor: fix problem
  • Loading branch information
ErfanMomeniii authored Jul 31, 2023
1 parent b242c44 commit 4e5d327
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func getFunctionName(fn interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
}

func getFunctionNameOfPointer(fn interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(fn).Elem().Pointer()).Name()
}

func parseTime(t string) (hour, min, sec int, err error) {
var timeLayout string
switch {
Expand Down
25 changes: 19 additions & 6 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,22 +931,35 @@ func (s *Scheduler) doCommon(jobFun interface{}, params ...interface{}) (*Job, e
return nil, job.error
}

typ := reflect.TypeOf(jobFun)
if typ.Kind() != reflect.Func {
val := reflect.ValueOf(jobFun)
for val.Kind() == reflect.Ptr {
val = val.Elem()
}

if val.Kind() != reflect.Func {
// delete the job for the same reason as above
s.RemoveByReference(job)
return nil, ErrNotAFunction
}

fname := getFunctionName(jobFun)
var fname string
if val == reflect.ValueOf(jobFun) {
fname = getFunctionName(jobFun)
} else {
fname = getFunctionNameOfPointer(jobFun)
}

if job.funcName != fname {
job.function = jobFun
if val != reflect.ValueOf(jobFun) {
job.function = val.Interface()
}

job.parameters = params
job.funcName = fname
}

f := reflect.ValueOf(jobFun)
expectedParamLength := f.Type().NumIn()
expectedParamLength := val.Type().NumIn()
if job.runWithDetails {
expectedParamLength--
}
Expand All @@ -957,7 +970,7 @@ func (s *Scheduler) doCommon(jobFun interface{}, params ...interface{}) (*Job, e
return nil, job.error
}

if job.runWithDetails && f.Type().In(len(params)).Kind() != reflect.ValueOf(*job).Kind() {
if job.runWithDetails && val.Type().In(len(params)).Kind() != reflect.ValueOf(*job).Kind() {
s.RemoveByReference(job)
job.error = wrapOrError(job.error, ErrDoWithJobDetails)
return nil, job.error
Expand Down
18 changes: 18 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ func TestImmediateExecution(t *testing.T) {
}
}

func TestExecutionWithPointerToFunc(t *testing.T) {
s := NewScheduler(time.UTC)
semaphore := make(chan bool)
fn := func() { semaphore <- true }

_, err := s.Every(1).Second().Do(&fn)
require.NoError(t, err)
s.StartAsync()
select {
case <-time.After(time.Second):
s.stop()
t.Fatal("job did not run immediately")
case <-semaphore:
// test passed
s.stop()
}
}

func TestScheduler_Every_InvalidInterval(t *testing.T) {
testCases := []struct {
description string
Expand Down

0 comments on commit 4e5d327

Please sign in to comment.