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

issue-654: allow setting a stopTime for job. #760

Merged
merged 4 commits into from
Jul 19, 2024
Merged

Conversation

Higan
Copy link
Contributor

@Higan Higan commented Jul 18, 2024

What does this do?

This PR exposes a stopTime when creating a job so that the job cannot be executed once the stopTime is reached.

Which issue(s) does this PR fix/relate to?

Resolves #654

List any changes that modify/break current functionality

Have you included tests for your changes?

Yes I created several test cases in scheduler_test.go regarding job creation with stopTime specified and also the expected behavior when stopTime is reached.

Did you document any new/modified functionality?

  • Updated example_test.go
  • Updated README.md

Notes

job.go Outdated
@@ -60,6 +61,13 @@ func (j *internalJob) stop() {
j.cancel()
}

func (j *internalJob) stopTimeReached() bool {
Copy link
Contributor

@JohnRoesler JohnRoesler Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to support time mocking, this function should accept a now time.Time value. then, when it is called, we should pass in the schedulers clockwork time now value

executor.go Outdated
Comment on lines 150 to 152
if j.stopTimeReached() {
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to cover all of the job types, this check should happen inside the runJob() method of the executor right after it checks if the job or executor contexts are canceled

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to support time mocking, we probably need to move the clockwork object off of the scheduler struct and into the executor struct, so that the clockwork time now value can be passed in here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take care of moving the clock. I'm going to open a PR to do that.

job.go Outdated
Comment on lines 614 to 615
// WithStopAt sets the option for stopping the job at
// a specific datetime.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// WithStopAt sets the option for stopping the job at
// a specific datetime.
// WithStopAt sets the option for stopping the job from running
// after the specified time.

job.go Outdated
Comment on lines 625 to 626
// WithStopDateTime sets the final data & time after which the job should stop
// This datetime must be in the future and should be after the startTime (if specified)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// WithStopDateTime sets the final data & time after which the job should stop
// This datetime must be in the future and should be after the startTime (if specified)
// WithStopDateTime sets the final date & time after which the job should stop.
// This must be in the future and should be after the startTime (if specified).
// The job's final run may be at the stop time, but not after.

scheduler.go Outdated
@@ -473,6 +473,10 @@ func (s *scheduler) selectStart() {
next = j.next(s.now())
}

if j.stopTimeReached() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check shouldn't need to happen here, but instead in the selectExecJobsOutForRescheduling function. since the executor is checking, if the stop time is reached, new jobs will be caught by that and then not rescheduled.

Copy link
Contributor

@JohnRoesler JohnRoesler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work so far!

job.go Outdated
}

// StopAtOption defines options for stopping the job
type StopAtOption func(*internalJob) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once my PR is merged, could you pull it in and change the signature here to accept a time.Time as well, and pass that in to avoid using time.Now() for validation inside the WithStopDateTime func?

@Higan
Copy link
Contributor Author

Higan commented Jul 19, 2024

@JohnRoesler Sure I'll handle these later today. Thanks for reviewing and commenting.

@Higan
Copy link
Contributor Author

Higan commented Jul 19, 2024

Hi @JohnRoesler I've made some changes with regard to your comments and now you could check this PR again. Thank you.

@Higan Higan changed the title [WIP] issue-654: allow setting a stopTime for job. issue-654: allow setting a stopTime for job. Jul 19, 2024
@JohnRoesler JohnRoesler merged commit 3b2dcd8 into go-co-op:v2 Jul 19, 2024
4 checks passed
rbroggi pushed a commit to rbroggi/gocron that referenced this pull request Sep 19, 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.

[FEATURE] Allow setting a stopTime on jobs to run the job until that date/time is reached
2 participants