Skip to content

Commit 2619a49

Browse files
committed
Add STOPPING state and update when shutdown is requested
1 parent 5a88fa7 commit 2619a49

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

django_dbq/management/commands/worker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def init_signals(self):
3030

3131
def shutdown(self, signum, frame):
3232
self.alive = False
33+
if self.current_job:
34+
self.current_job.state = Job.STATES.STOPPING
35+
self.current_job.save(update_fields=["state"])
3336

3437
def run(self):
3538
while self.alive:
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 3.2rc1 on 2021-11-29 04:43
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("django_dbq", "0004_auto_20210818_0247"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="job",
15+
name="state",
16+
field=models.CharField(
17+
choices=[
18+
("NEW", "New"),
19+
("READY", "Ready"),
20+
("PROCESSING", "Processing"),
21+
("STOPPING", "Stopping"),
22+
("FAILED", "Failed"),
23+
("COMPLETE", "Complete"),
24+
],
25+
db_index=True,
26+
default="NEW",
27+
max_length=20,
28+
),
29+
),
30+
]

django_dbq/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class STATES(TextChoices):
7777
NEW = "NEW"
7878
READY = "READY"
7979
PROCESSING = "PROCESSING"
80+
STOPPING = "STOPPING"
8081
FAILED = "FAILED"
8182
COMPLETE = "COMPLETE"
8283

django_dbq/tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ def test_process_job_previous_job_long_time_ago(self, mock_sleep):
152152
self.assertEqual(self.mock_worker.last_job_finished, timezone.now())
153153

154154

155+
@override_settings(JOBS={"testjob": {"tasks": ["a"]}})
156+
class ShutdownTestCase(TestCase):
157+
def test_shutdown_sets_state_to_stopping(self):
158+
job = Job.objects.create(name="testjob")
159+
worker = Worker("default", 1)
160+
worker.current_job = job
161+
162+
worker.shutdown(None, None)
163+
164+
job.refresh_from_db()
165+
self.assertEqual(job.state, Job.STATES.STOPPING)
166+
167+
155168
@override_settings(JOBS={"testjob": {"tasks": ["a"]}})
156169
class JobTestCase(TestCase):
157170
def test_create_job(self):

0 commit comments

Comments
 (0)