Skip to content

Add support for scheduling jobs to run in the future #43

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

Merged
merged 3 commits into from
Nov 23, 2021
Merged

Conversation

j4mie
Copy link
Member

@j4mie j4mie commented Nov 4, 2021

This PR adds a new (nullable) run_after field to the Job model, which is a DateTimeField. If this field is populated, the job worker will ignore any jobs for which run_after is in the future. This allows jobs to be scheduled to run later.

README.md Outdated
Job.objects.create(name='scheduled_job', run_after=timezone.now() + timedelta(minutes=10))
```

Of course, the job will only be run if your `python manage.py worker` process is running at the time when the job is scheduled to run. Otherwise, it will run the next time you start your worker process after that time has passed.
Copy link
Member

Choose a reason for hiding this comment

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

is it worth noting that this isn't exact? say if you're queue has a large backlog it may be sometime before the worker can pick it up

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh that's a good point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Have added a note on that. A large backlog shouldn't be so much of an issue, because jobs are picked up in order of creation - so scheduled jobs were probably created a long time ago and so will be picked up before any other jobs that have been created more recently. However if a job is currently being processed then of course your scheduled job will only run after it's finished.

@j4mie j4mie merged commit 322f111 into master Nov 23, 2021
@j4mie j4mie deleted the run-after branch November 23, 2021 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants