@@ -38,6 +38,7 @@ type internalJob struct {
38
38
limitRunsTo * limitRunsTo
39
39
startTime time.Time
40
40
startImmediately bool
41
+ stopTime time.Time
41
42
// event listeners
42
43
afterJobRuns func (jobID uuid.UUID , jobName string )
43
44
beforeJobRuns func (jobID uuid.UUID , jobName string )
@@ -60,6 +61,13 @@ func (j *internalJob) stop() {
60
61
j .cancel ()
61
62
}
62
63
64
+ func (j * internalJob ) stopTimeReached (now time.Time ) bool {
65
+ if j .stopTime .IsZero () {
66
+ return false
67
+ }
68
+ return j .stopTime .Before (now )
69
+ }
70
+
63
71
// task stores the function and parameters
64
72
// that are actually run when the job is executed.
65
73
type task struct {
@@ -594,11 +602,41 @@ func WithStartDateTime(start time.Time) StartAtOption {
594
602
if start .IsZero () || start .Before (now ) {
595
603
return ErrWithStartDateTimePast
596
604
}
605
+ if ! j .stopTime .IsZero () && j .stopTime .Before (start ) {
606
+ return ErrStartTimeLaterThanEndTime
607
+ }
597
608
j .startTime = start
598
609
return nil
599
610
}
600
611
}
601
612
613
+ // WithStopAt sets the option for stopping the job from running
614
+ // after the specified time.
615
+ func WithStopAt (option StopAtOption ) JobOption {
616
+ return func (j * internalJob , now time.Time ) error {
617
+ return option (j , now )
618
+ }
619
+ }
620
+
621
+ // StopAtOption defines options for stopping the job
622
+ type StopAtOption func (* internalJob , time.Time ) error
623
+
624
+ // WithStopDateTime sets the final date & time after which the job should stop.
625
+ // This must be in the future and should be after the startTime (if specified).
626
+ // The job's final run may be at the stop time, but not after.
627
+ func WithStopDateTime (end time.Time ) StopAtOption {
628
+ return func (j * internalJob , now time.Time ) error {
629
+ if end .IsZero () || end .Before (now ) {
630
+ return ErrWithStopDateTimePast
631
+ }
632
+ if end .Before (j .startTime ) {
633
+ return ErrStopTimeEarlierThanStartTime
634
+ }
635
+ j .stopTime = end
636
+ return nil
637
+ }
638
+ }
639
+
602
640
// WithTags sets the tags for the job. Tags provide
603
641
// a way to identify jobs by a set of tags and remove
604
642
// multiple jobs by tag.
0 commit comments