Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ Job.objects.create(name='critical_job', priority=2)

Jobs will be ordered by their `priority` (highest to lowest) and then the time which they were created (oldest to newest) and processed in that order.

### Scheduling jobs
If you'd like to create a job but have it run at some time in the future, you can use the `run_after` field on the Job model:

```python
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
Contributor

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.


## Terminology

### Job
Expand Down
18 changes: 18 additions & 0 deletions django_dbq/migrations/0005_job_run_after.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2rc1 on 2021-11-04 03:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("django_dbq", "0004_auto_20210818_0247"),
]

operations = [
migrations.AddField(
model_name="job",
name="run_after",
field=models.DateTimeField(db_index=True, null=True),
),
]
8 changes: 7 additions & 1 deletion django_dbq/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ def delete_old(self):

def to_process(self, queue_name):
return self.select_for_update().filter(
queue_name=queue_name, state__in=(Job.STATES.READY, Job.STATES.NEW)
models.Q(queue_name=queue_name)
& models.Q(state__in=(Job.STATES.READY, Job.STATES.NEW))
& models.Q(
models.Q(run_after__isnull=True)
| models.Q(run_after__lte=timezone.now())
)
)


Expand All @@ -91,6 +96,7 @@ class STATES(TextChoices):
workspace = JSONField(null=True)
queue_name = models.CharField(max_length=20, default="default", db_index=True)
priority = models.SmallIntegerField(default=0, db_index=True)
run_after = models.DateTimeField(null=True, db_index=True)

class Meta:
ordering = ["-priority", "created"]
Expand Down
14 changes: 14 additions & 0 deletions django_dbq/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ def test_gets_jobs_in_priority_and_date_order(self):
self.assertEqual(Job.objects.get_ready_or_none("default"), job_1)
self.assertFalse(Job.objects.to_process("default").filter(id=job_2.id).exists())

def test_ignores_jobs_until_run_after_is_in_the_past(self):
job_1 = Job.objects.create(name="testjob")
job_2 = Job.objects.create(name="testjob", run_after=datetime(2021, 11, 4, 8))

with freezegun.freeze_time(datetime(2021, 11, 4, 7)):
self.assertEqual(
{job for job in Job.objects.to_process("default")}, {job_1}
)

with freezegun.freeze_time(datetime(2021, 11, 4, 9)):
self.assertEqual(
{job for job in Job.objects.to_process("default")}, {job_1, job_2}
)

def test_get_next_ready_job_created(self):
"""
Created jobs should be picked too.
Expand Down