From 781b01a007b8248992e5c98ceda958ec520aed4c Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Sat, 21 Oct 2023 19:43:44 +1100 Subject: [PATCH] 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 --- bookwyrm/models/bookwyrm_export_job.py | 18 ++++++++++-------- bookwyrm/models/job.py | 12 ++++++++++-- .../templates/preferences/export-user.html | 6 +++--- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/bookwyrm/models/bookwyrm_export_job.py b/bookwyrm/models/bookwyrm_export_job.py index c3a0b652a..00cab7559 100644 --- a/bookwyrm/models/bookwyrm_export_job.py +++ b/bookwyrm/models/bookwyrm_export_job.py @@ -35,13 +35,14 @@ def start_export_task(**kwargs): # don't start the job if it was stopped from the UI if job.complete: return - - # This is where ChildJobs get made - job.export_data = ContentFile(b"", str(uuid4())) - - json_data = json_export(job.user) - tar_export(json_data, job.user, job.export_data) - + try: + # This is where ChildJobs get made + job.export_data = ContentFile(b"", str(uuid4())) + json_data = json_export(job.user) + 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"]) @@ -56,7 +57,8 @@ def tar_export(json_data: str, user, f): editions, books = get_books_for_user(user) for book in editions: - tar.add_image(book.cover) + if getattr(book, "cover", False): + tar.add_image(book.cover) f.close() diff --git a/bookwyrm/models/job.py b/bookwyrm/models/job.py index 6e8d0dc5c..7557c5855 100644 --- a/bookwyrm/models/job.py +++ b/bookwyrm/models/job.py @@ -19,6 +19,7 @@ class Job(models.Model): ACTIVE = "active", _("Active") COMPLETE = "complete", _("Complete") STOPPED = "stopped", _("Stopped") + FAILED = "failed", _("Failed") task_id = models.UUIDField(unique=True, null=True, blank=True) @@ -43,14 +44,17 @@ class Job(models.Model): self.save(update_fields=["status", "complete", "updated_date"]) - def stop_job(self): + def stop_job(self, reason=None): """Stop the job""" if self.complete: return self.__terminate_job() - self.status = self.Status.STOPPED + if reason and reason is "failed": + self.status = self.Status.FAILED + else: + self.status = self.Status.STOPPED self.complete = True self.updated_date = timezone.now() @@ -72,6 +76,10 @@ class Job(models.Model): self.stop_job() return + if status == self.Status.FAILED: + self.stop_job(reason="failed") + return + self.updated_date = timezone.now() self.status = status diff --git a/bookwyrm/templates/preferences/export-user.html b/bookwyrm/templates/preferences/export-user.html index 393d8990e..2dd3f6de3 100644 --- a/bookwyrm/templates/preferences/export-user.html +++ b/bookwyrm/templates/preferences/export-user.html @@ -48,8 +48,8 @@ {% for job in jobs %} - {% if job.complete %} -

{{ job.created_date }}

+ {% if job.complete and not job.status == "stopped" and not job.status == "failed" %} +

{{ job.created_date }}

{% else %}

{{ job.created_date }}

{% endif %} @@ -57,7 +57,7 @@ {{ job.updated_date }}