Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting customize Timer to controll the scheduling behavior #491

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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