Skip to content

Commit 26c0ec6

Browse files
Rename "completed" tasks to "succeeded" (#119)
* Rename "completed" tasks to "succeeded" "completed" could be misread as completed unsuccessfully
1 parent 1e3f238 commit 26c0ec6

13 files changed

+132
-67
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Finally, you can run the `db_worker` command to run tasks as they're created. Ch
159159

160160
### Pruning old tasks
161161

162-
After a while, tasks may start to build up in your database. This can be managed using the `prune_db_task_results` management command, which deletes completed and failed tasks according to the given retention policy. Check the `--help` for the available options.
162+
After a while, tasks may start to build up in your database. This can be managed using the `prune_db_task_results` management command, which deletes completed tasks according to the given retention policy. Check the `--help` for the available options.
163163

164164
### Retrieving task result
165165

@@ -184,10 +184,10 @@ default_task_backend.get_result(result_id)
184184

185185
### Return values
186186

187-
If your task returns something, it can be retrieved from the `.return_value` attribute on a `TaskResult`. Accessing this property on an unfinished task (ie not `COMPLETE` or `FAILED`) will raise a `ValueError`.
187+
If your task returns something, it can be retrieved from the `.return_value` attribute on a `TaskResult`. Accessing this property on an unfinished task (ie not `SUCCEEDED` or `FAILED`) will raise a `ValueError`.
188188

189189
```python
190-
assert result.status == ResultStatus.COMPLETE
190+
assert result.status == ResultStatus.SUCCEEDED
191191
assert result.return_value == 42
192192
```
193193

@@ -196,7 +196,7 @@ If a result has been updated in the background, you can call `refresh` on it to
196196
```python
197197
assert result.status == ResultStatus.NEW
198198
result.refresh()
199-
assert result.status == ResultStatus.COMPLETE
199+
assert result.status == ResultStatus.SUCCEEDED
200200
```
201201

202202
#### Exceptions
@@ -240,7 +240,7 @@ A few [Signals](https://docs.djangoproject.com/en/stable/topics/signals/) are pr
240240
Whilst signals are available, they may not be the most maintainable approach.
241241

242242
- `django_tasks.signals.task_enqueued`: Called when a task is enqueued. The sender is the backend class. Also called with the enqueued `task_result`.
243-
- `django_tasks.signals.task_finished`: Called when a task finishes (`COMPLETE` or `FAILED`). The sender is the backend class. Also called with the finished `task_result`.
243+
- `django_tasks.signals.task_finished`: Called when a task finishes (`SUCCEEDED` or `FAILED`). The sender is the backend class. Also called with the finished `task_result`.
244244

245245
## Contributing
246246

django_tasks/backends/database/management/commands/db_worker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def start(self) -> None:
124124

125125
def run_task(self, db_task_result: DBTaskResult) -> None:
126126
"""
127-
Run the given task, marking it as complete or failed.
127+
Run the given task, marking it as succeeded or failed.
128128
"""
129129
try:
130130
task = db_task_result.task
@@ -140,7 +140,7 @@ def run_task(self, db_task_result: DBTaskResult) -> None:
140140

141141
# Setting the return and success value inside the error handling,
142142
# So errors setting it (eg JSON encode) can still be recorded
143-
db_task_result.set_complete(return_value)
143+
db_task_result.set_succeeded(return_value)
144144
task_finished.send(
145145
sender=type(task.get_backend()), task_result=db_task_result.task_result
146146
)

django_tasks/backends/database/management/commands/prune_db_task_results.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def handle(
115115
results = results.filter(finished_at__lte=min_age)
116116
else:
117117
results = results.filter(
118-
Q(status=ResultStatus.COMPLETE, finished_at__lte=min_age)
118+
Q(status=ResultStatus.SUCCEEDED, finished_at__lte=min_age)
119119
| Q(status=ResultStatus.FAILED, finished_at__lte=failed_min_age)
120120
)
121121

django_tasks/backends/database/migrations/0008_separate_results_field.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ def separate_results_field(
1212
) -> None:
1313
DBTaskResult = apps.get_model("django_tasks_database", "DBTaskResult")
1414

15-
# If a task completed, the result is its return value
15+
# If a task succeeded, the result is its return value
1616
DBTaskResult.objects.using(schema_editor.connection.alias).filter(
17-
status=ResultStatus.COMPLETE
17+
status=ResultStatus.SUCCEEDED
1818
).update(return_value=models.F("result"))
1919

2020
# If a task failed, the result is the exception data (or nothing)
@@ -28,9 +28,9 @@ def merge_results_field(
2828
) -> None:
2929
DBTaskResult = apps.get_model("django_tasks_database", "DBTaskResult")
3030

31-
# If a task completed, the result is its return value
31+
# If a task succeeded, the result is its return value
3232
DBTaskResult.objects.using(schema_editor.connection.alias).filter(
33-
status=ResultStatus.COMPLETE
33+
status=ResultStatus.SUCCEEDED
3434
).update(result=models.F("return_value"))
3535

3636
# If a task failed, the result is the exception data (or nothing)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 4.2.13 on 2024-11-21 13:46
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("django_tasks_database", "0009_remove_results_field"),
9+
]
10+
11+
operations = [
12+
migrations.AlterField(
13+
model_name="dbtaskresult",
14+
name="status",
15+
field=models.CharField(
16+
choices=[
17+
("NEW", "New"),
18+
("RUNNING", "Running"),
19+
("FAILED", "Failed"),
20+
("SUCCEEDED", "Succeeded"),
21+
],
22+
default="NEW",
23+
max_length=9,
24+
verbose_name="status",
25+
),
26+
),
27+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated by Django 4.2.13 on 2024-08-23 14:38
2+
3+
from django.db import migrations
4+
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
5+
from django.db.migrations.state import StateApps
6+
7+
from django_tasks import ResultStatus
8+
9+
10+
def separate_results_field(
11+
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
12+
) -> None:
13+
DBTaskResult = apps.get_model("django_tasks_database", "DBTaskResult")
14+
15+
DBTaskResult.objects.using(schema_editor.connection.alias).filter(
16+
status="COMPLETE"
17+
).update(status=ResultStatus.SUCCEEDED)
18+
19+
20+
def merge_results_field(
21+
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
22+
) -> None:
23+
DBTaskResult = apps.get_model("django_tasks_database", "DBTaskResult")
24+
25+
DBTaskResult.objects.using(schema_editor.connection.alias).filter(
26+
status=ResultStatus.SUCCEEDED
27+
).update(status="COMPLETE")
28+
29+
30+
class Migration(migrations.Migration):
31+
dependencies = [
32+
("django_tasks_database", "0010_alter_dbtaskresult_status"),
33+
]
34+
35+
operations = [migrations.RunPython(separate_results_field, merge_results_field)]

django_tasks/backends/database/models.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def ready(self) -> "DBTaskResultQuerySet":
5555
status=ResultStatus.NEW,
5656
).filter(models.Q(run_after=None) | models.Q(run_after__lte=timezone.now()))
5757

58-
def complete(self) -> "DBTaskResultQuerySet":
59-
return self.filter(status=ResultStatus.COMPLETE)
58+
def succeeded(self) -> "DBTaskResultQuerySet":
59+
return self.filter(status=ResultStatus.SUCCEEDED)
6060

6161
def failed(self) -> "DBTaskResultQuerySet":
6262
return self.filter(status=ResultStatus.FAILED)
@@ -65,7 +65,7 @@ def running(self) -> "DBTaskResultQuerySet":
6565
return self.filter(status=ResultStatus.RUNNING)
6666

6767
def finished(self) -> "DBTaskResultQuerySet":
68-
return self.failed() | self.complete()
68+
return self.failed() | self.succeeded()
6969

7070
@retry()
7171
def get_locked(self) -> Optional["DBTaskResult"]:
@@ -173,8 +173,8 @@ def claim(self) -> None:
173173
self.save(update_fields=["status", "started_at"])
174174

175175
@retry()
176-
def set_complete(self, return_value: Any) -> None:
177-
self.status = ResultStatus.COMPLETE
176+
def set_succeeded(self, return_value: Any) -> None:
177+
self.status = ResultStatus.SUCCEEDED
178178
self.finished_at = timezone.now()
179179
self.return_value = return_value
180180
self.exception_data = None

django_tasks/backends/immediate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _execute_task(self, task_result: TaskResult) -> None:
6262
task_finished.send(type(self), task_result=task_result)
6363
else:
6464
object.__setattr__(task_result, "finished_at", timezone.now())
65-
object.__setattr__(task_result, "status", ResultStatus.COMPLETE)
65+
object.__setattr__(task_result, "status", ResultStatus.SUCCEEDED)
6666

6767
task_finished.send(type(self), task_result=task_result)
6868

django_tasks/task.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ResultStatus(TextChoices):
4646
NEW = ("NEW", _("New"))
4747
RUNNING = ("RUNNING", _("Running"))
4848
FAILED = ("FAILED", _("Failed"))
49-
COMPLETE = ("COMPLETE", _("Complete"))
49+
SUCCEEDED = ("SUCCEEDED", _("Succeeded"))
5050

5151

5252
T = TypeVar("T")
@@ -273,13 +273,13 @@ def return_value(self) -> Optional[T]:
273273
"""
274274
Get the return value of the task.
275275
276-
If the task didn't complete successfully, an exception is raised.
276+
If the task didn't succeed, an exception is raised.
277277
This is to distinguish against the task returning None.
278278
"""
279279
if self.status == ResultStatus.FAILED:
280280
raise ValueError("Task failed")
281281

282-
elif self.status != ResultStatus.COMPLETE:
282+
elif self.status != ResultStatus.SUCCEEDED:
283283
raise ValueError("Task has not finished yet")
284284

285285
return cast(T, self._return_value)

0 commit comments

Comments
 (0)