add error handling and status for user exports

* fix Safari not downloading with the correct filename
* add FAILED status
* don't provide download link for stopped jobs
This commit is contained in:
Hugh Rundle 2023-10-21 19:43:44 +11:00
parent a4bfcb34d5
commit 781b01a007
No known key found for this signature in database
GPG key ID: A7E35779918253F9
3 changed files with 23 additions and 13 deletions

View file

@ -35,13 +35,14 @@ def start_export_task(**kwargs):
# don't start the job if it was stopped from the UI # don't start the job if it was stopped from the UI
if job.complete: if job.complete:
return return
try:
# This is where ChildJobs get made # This is where ChildJobs get made
job.export_data = ContentFile(b"", str(uuid4())) job.export_data = ContentFile(b"", str(uuid4()))
json_data = json_export(job.user) json_data = json_export(job.user)
tar_export(json_data, job.user, job.export_data) tar_export(json_data, job.user, job.export_data)
except Exception as err: # pylint: disable=broad-except
logger.exception("Job %s Failed with error: %s", job.id, err)
job.set_status("failed")
job.save(update_fields=["export_data"]) job.save(update_fields=["export_data"])
@ -56,6 +57,7 @@ def tar_export(json_data: str, user, f):
editions, books = get_books_for_user(user) editions, books = get_books_for_user(user)
for book in editions: for book in editions:
if getattr(book, "cover", False):
tar.add_image(book.cover) tar.add_image(book.cover)
f.close() f.close()

View file

@ -19,6 +19,7 @@ class Job(models.Model):
ACTIVE = "active", _("Active") ACTIVE = "active", _("Active")
COMPLETE = "complete", _("Complete") COMPLETE = "complete", _("Complete")
STOPPED = "stopped", _("Stopped") STOPPED = "stopped", _("Stopped")
FAILED = "failed", _("Failed")
task_id = models.UUIDField(unique=True, null=True, blank=True) task_id = models.UUIDField(unique=True, null=True, blank=True)
@ -43,13 +44,16 @@ class Job(models.Model):
self.save(update_fields=["status", "complete", "updated_date"]) self.save(update_fields=["status", "complete", "updated_date"])
def stop_job(self): def stop_job(self, reason=None):
"""Stop the job""" """Stop the job"""
if self.complete: if self.complete:
return return
self.__terminate_job() self.__terminate_job()
if reason and reason is "failed":
self.status = self.Status.FAILED
else:
self.status = self.Status.STOPPED self.status = self.Status.STOPPED
self.complete = True self.complete = True
self.updated_date = timezone.now() self.updated_date = timezone.now()
@ -72,6 +76,10 @@ class Job(models.Model):
self.stop_job() self.stop_job()
return return
if status == self.Status.FAILED:
self.stop_job(reason="failed")
return
self.updated_date = timezone.now() self.updated_date = timezone.now()
self.status = status self.status = status

View file

@ -48,8 +48,8 @@
{% for job in jobs %} {% for job in jobs %}
<tr> <tr>
<td> <td>
{% if job.complete %} {% if job.complete and not job.status == "stopped" and not job.status == "failed" %}
<p><a href="/preferences/user-export/{{ job.task_id }}">{{ job.created_date }}</a></p> <p><a download="" href="/preferences/user-export/{{ job.task_id }}">{{ job.created_date }}</a></p>
{% else %} {% else %}
<p>{{ job.created_date }}</p> <p>{{ job.created_date }}</p>
{% endif %} {% endif %}
@ -57,7 +57,7 @@
<td>{{ job.updated_date }}</td> <td>{{ job.updated_date }}</td>
<td> <td>
<span <span
{% if job.status == "stopped" %} {% if job.status == "stopped" or job.status == "failed" %}
class="tag is-danger" class="tag is-danger"
{% elif job.status == "pending" %} {% elif job.status == "pending" %}
class="tag is-warning" class="tag is-warning"