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

Execute the function in the scheduled task immediately when the program starts。 #342

Open
wuruipeng404 opened this issue Sep 24, 2020 · 2 comments

Comments

@wuruipeng404
Copy link

For example, I have a task that is executed every minute. When I start the program, cron will start to execute my task in the next minute. Can I add an option such as "Execute once now" task。

If I manually call the task once in the program, it will look very ugly, and some situations cannot be satisfied

@windzhu0514
Copy link

you can write a custom schedule,but this only support stand parser.

type StartNowSchedule struct {
	firstTime int32
	schedule  cron.Schedule
}

func NewStartNow(spec string) (*StartNowSchedule, error) {
	schedule, err := cron.ParseStandard(spec)
	if err != nil {
		return nil, err
	}

	return &StartNowSchedule{schedule: schedule}, nil
}

func (schedule *StartNowSchedule) Next(t time.Time) time.Time {
	if schedule.firstTime == 0 {
		schedule.firstTime = 1
		return t
	}

	return schedule.schedule.Next(t)
}

@ivanjaros
Copy link

this is quite needed indeed. the AddJob(spec string, cmd Job) could be simply refactored to AddJob(spec string, cmd Job, runOnStart ...bool) to keep the backwards compatibility and the scheduler would fire the tasks one after another in the order they were added.

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

No branches or pull requests

3 participants