Skip to content

Commit 6a30d6d

Browse files
authored
feat: add WithStartDateTimePast WithStartAt option (#882)
1 parent 536410f commit 6a30d6d

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var (
5454
ErrWithMonitorNil = errors.New("gocron: WithMonitor: monitor must not be nil")
5555
ErrWithNameEmpty = errors.New("gocron: WithName: name must not be empty")
5656
ErrWithStartDateTimePast = errors.New("gocron: WithStartDateTime: start must not be in the past")
57+
ErrWithStartDateTimePastZero = errors.New("gocron: WithStartDateTime: start must not be zero")
5758
ErrWithStopDateTimePast = errors.New("gocron: WithStopDateTime: end must not be in the past")
5859
ErrStartTimeLaterThanEndTime = errors.New("gocron: WithStartDateTime: start must not be later than end")
5960
ErrStopTimeEarlierThanStartTime = errors.New("gocron: WithStopDateTime: end must not be earlier than start")

example_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,90 @@ func ExampleWithStartAt() {
10461046
// 9999-09-09 09:09:09.000000009 +0000 UTC
10471047
}
10481048

1049+
func ExampleWithStartDateTime() {
1050+
s, _ := gocron.NewScheduler()
1051+
defer func() { _ = s.Shutdown() }()
1052+
1053+
start := time.Date(9999, 9, 9, 9, 9, 9, 9, time.UTC)
1054+
1055+
j, _ := s.NewJob(
1056+
gocron.DurationJob(
1057+
time.Second,
1058+
),
1059+
gocron.NewTask(
1060+
func(one string, two int) {
1061+
fmt.Printf("%s, %d", one, two)
1062+
},
1063+
"one", 2,
1064+
),
1065+
gocron.WithStartAt(
1066+
gocron.WithStartDateTime(start),
1067+
),
1068+
)
1069+
s.Start()
1070+
1071+
next, _ := j.NextRun()
1072+
fmt.Println(next)
1073+
1074+
_ = s.StopJobs()
1075+
// Output:
1076+
// 9999-09-09 09:09:09.000000009 +0000 UTC
1077+
}
1078+
1079+
func ExampleWithStartDateTimePast() {
1080+
s, _ := gocron.NewScheduler()
1081+
defer func() { _ = s.Shutdown() }()
1082+
1083+
start := time.Now().Add(-time.Minute)
1084+
1085+
j, _ := s.NewJob(
1086+
gocron.DurationJob(
1087+
time.Second,
1088+
),
1089+
gocron.NewTask(
1090+
func(one string, two int) {
1091+
fmt.Printf("%s, %d", one, two)
1092+
},
1093+
"one", 2,
1094+
),
1095+
gocron.WithStartAt(
1096+
gocron.WithStartDateTimePast(start),
1097+
),
1098+
)
1099+
s.Start()
1100+
1101+
time.Sleep(100 * time.Millisecond)
1102+
1103+
_, _ = j.NextRun()
1104+
1105+
_ = s.StopJobs()
1106+
}
1107+
1108+
func ExampleWithStartImmediately() {
1109+
s, _ := gocron.NewScheduler()
1110+
defer func() { _ = s.Shutdown() }()
1111+
1112+
j, _ := s.NewJob(
1113+
gocron.DurationJob(
1114+
time.Second,
1115+
),
1116+
gocron.NewTask(
1117+
func(one string, two int) {
1118+
fmt.Printf("%s, %d", one, two)
1119+
},
1120+
"one", 2,
1121+
),
1122+
gocron.WithStartAt(
1123+
gocron.WithStartImmediately(),
1124+
),
1125+
)
1126+
s.Start()
1127+
1128+
_, _ = j.NextRun()
1129+
1130+
_ = s.StopJobs()
1131+
}
1132+
10491133
func ExampleWithStopTimeout() {
10501134
_, _ = gocron.NewScheduler(
10511135
gocron.WithStopTimeout(time.Second * 5),

job.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,26 @@ func WithStartDateTime(start time.Time) StartAtOption {
718718
}
719719
}
720720

721+
// WithStartDateTimePast sets the first date & time at which the job should run
722+
// from a time in the past. This is useful when you want to backdate
723+
// the start time of a job to a time in the past, for example
724+
// if you want to start a job from a specific date in the past
725+
// and have it run on its schedule from then.
726+
// The start time can be in the past, but not zero.
727+
// If the start time is in the future, it behaves the same as WithStartDateTime.
728+
func WithStartDateTimePast(start time.Time) StartAtOption {
729+
return func(j *internalJob, _ time.Time) error {
730+
if start.IsZero() {
731+
return ErrWithStartDateTimePastZero
732+
}
733+
if !j.stopTime.IsZero() && j.stopTime.Before(start) {
734+
return ErrStartTimeLaterThanEndTime
735+
}
736+
j.startTime = start
737+
return nil
738+
}
739+
}
740+
721741
// WithStopAt sets the option for stopping the job from running
722742
// after the specified time.
723743
func WithStopAt(option StopAtOption) JobOption {

scheduler_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,14 @@ func TestScheduler_NewJobErrors(t *testing.T) {
10041004
[]JobOption{WithStartAt(WithStartDateTime(time.Now().Add(-time.Second)))},
10051005
ErrWithStartDateTimePast,
10061006
},
1007+
{
1008+
"WithStartDateTimePast is zero",
1009+
DurationJob(
1010+
time.Second,
1011+
),
1012+
[]JobOption{WithStartAt(WithStartDateTimePast(time.Time{}))},
1013+
ErrWithStartDateTimePastZero,
1014+
},
10071015
{
10081016
"WithStartDateTime is later than the end",
10091017
DurationJob(

0 commit comments

Comments
 (0)