Skip to content

Commit ac0aa3f

Browse files
Merge pull request #1112 from syucream/fix/simplify-job-tasks
Simplify most of job tasks
2 parents 3c4a453 + 1cadbf9 commit ac0aa3f

File tree

8 files changed

+246
-323
lines changed

8 files changed

+246
-323
lines changed

airone/lib/job.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ def may_schedule_until_job_is_ready(
1010
@functools.wraps(func)
1111
def wrapper(kls, job_id: int):
1212
job = Job.objects.get(id=job_id)
13-
if job.proceed_if_ready():
14-
# update Job status from PREPARING to PROCEEDING
15-
job.update(JobStatus.PROCESSING.value)
1613

17-
try:
18-
# running Job processing
19-
ret: JobStatus | tuple[int, str] | None = func(kls, job)
20-
except Exception:
21-
ret = JobStatus.ERROR
14+
if not job.proceed_if_ready():
15+
return
16+
# update Job status from PREPARING to PROCEEDING
17+
job.update(JobStatus.PROCESSING.value)
2218

23-
# update Job status after finishing Job processing
24-
if isinstance(ret, JobStatus):
25-
job.update(status=ret.value)
26-
elif (
27-
isinstance(ret, tuple) and isinstance(ret[0], JobStatus) and isinstance(ret[1], str)
28-
):
29-
job.update(status=ret[0].value, text=ret[1])
30-
elif not job.is_canceled():
31-
job.update(JobStatus.DONE.value)
19+
try:
20+
# running Job processing
21+
ret: JobStatus | tuple[JobStatus, str] | None = func(kls, job)
22+
except Exception:
23+
ret = JobStatus.ERROR
24+
25+
# update Job status after finishing Job processing
26+
if isinstance(ret, JobStatus):
27+
job.update(status=ret.value)
28+
elif isinstance(ret, tuple) and isinstance(ret[0], JobStatus) and isinstance(ret[1], str):
29+
job.update(status=ret[0].value, text=ret[1])
30+
elif not job.is_canceled():
31+
job.update(JobStatus.DONE.value)
3232

3333
return wrapper

dashboard/tasks.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import csv
22
import io
33
import json
4-
from typing import Any, Optional
4+
from typing import Any
55

66
import yaml
77
from django.conf import settings
88
from natsort import natsorted
99

1010
from airone.celery import app
11+
from airone.lib.job import may_schedule_until_job_is_ready
1112
from airone.lib.types import AttrTypeValue
1213
from entry.models import Entry
13-
from job.models import Job, JobStatus
14+
from job.models import Job
1415

1516

16-
def _csv_export(job: Job, values, recv_data: dict, has_referral: bool) -> Optional[io.StringIO]:
17+
def _csv_export(job: Job, values, recv_data: dict, has_referral: bool) -> io.StringIO | None:
1718
output = io.StringIO(newline="")
1819
writer = csv.writer(output)
1920

@@ -102,7 +103,7 @@ def _csv_export(job: Job, values, recv_data: dict, has_referral: bool) -> Option
102103
return output
103104

104105

105-
def _yaml_export(job: Job, values, recv_data: dict, has_referral: bool) -> Optional[io.StringIO]:
106+
def _yaml_export(job: Job, values, recv_data: dict, has_referral: bool) -> io.StringIO | None:
106107
output = io.StringIO()
107108

108109
def _get_attr_value(atype: int, value: dict):
@@ -163,22 +164,15 @@ def _get_attr_value(atype: int, value: dict):
163164

164165

165166
@app.task(bind=True)
166-
def export_search_result(self, job_id):
167-
job = Job.objects.get(id=job_id)
168-
169-
if not job.proceed_if_ready():
170-
return
171-
172-
# set flag to indicate that this job starts processing
173-
job.update(JobStatus.PROCESSING.value)
174-
167+
@may_schedule_until_job_is_ready
168+
def export_search_result(self, job: Job):
175169
user = job.user
176170
recv_data = json.loads(job.params)
177171

178172
# Do not care whether the "has_referral" value is
179173
has_referral: bool = recv_data.get("has_referral", False)
180-
referral_name: Optional[str] = recv_data.get("referral_name")
181-
entry_name: Optional[str] = recv_data.get("entry_name")
174+
referral_name: str | None = recv_data.get("referral_name")
175+
entry_name: str | None = recv_data.get("entry_name")
182176
if has_referral and referral_name is None:
183177
referral_name = ""
184178

@@ -191,15 +185,11 @@ def export_search_result(self, job_id):
191185
referral_name,
192186
)
193187

194-
io_stream: Optional[io.StringIO] = None
188+
io_stream: io.StringIO | None = None
195189
if recv_data["export_style"] == "yaml":
196190
io_stream = _yaml_export(job, resp["ret_values"], recv_data, has_referral)
197191
elif recv_data["export_style"] == "csv":
198192
io_stream = _csv_export(job, resp["ret_values"], recv_data, has_referral)
199193

200194
if io_stream:
201195
job.set_cache(io_stream.getvalue())
202-
203-
# update job status and save it except for the case that target job is canceled.
204-
if not job.is_canceled():
205-
job.update(JobStatus.DONE.value)

0 commit comments

Comments
 (0)