Skip to content

Supporting customize Timer to controll the scheduling behavior#491

Open
cloudfly wants to merge 1 commit intorobfig:masterfrom
cloudfly:feature-custom-timer
Open

Supporting customize Timer to controll the scheduling behavior#491
cloudfly wants to merge 1 commit intorobfig:masterfrom
cloudfly:feature-custom-timer

Conversation

@cloudfly
Copy link
Contributor

Add a new interface Timer replace time.Timer for scheduling the job.

So that the user can create custom timer instead of using standard time.Timer,mainly used for testing。

eg.

import (
	"github.com/benbjohnson/clock"
        "github.com/robfig/cron/v3"
)

func main() {
    mockClock := clock.NewMock()
    cronEngine = cron.New(cron.WithTimerFunc(NewMockTimerFunc(mockClock)))
}

func NewMockTimerFunc(mock *clock.Mock) func(time.Duration) cron.Timer {
	return func(d time.Duration) cron.Timer {
		return &mockTimer{
			timer: mock.Timer(d),
		}
	}
}

type mockTimer struct {
	timer *clock.Timer
}

func (t *mockTimer) C() <-chan time.Time {
	return t.timer.C
}

func (t *mockTimer) Stop() bool {
	return t.timer.Stop()
}

dsh2dsh added a commit to dsh2dsh/cron that referenced this pull request Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant