Skip to content

Commit

Permalink
Rend publication_watchdog plus résistant aux exceptions (#6264)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippemilink authored Mar 28, 2022
1 parent e2c0775 commit f8d2136
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions zds/tutorialv2/management/commands/publication_watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,42 @@ def handle(self, *args, **options):
while requested_events.count() == 0:
time.sleep(60)

self.run()
try:
self.run()
except:
logger.exception("Exception during one publication_watchdog run.")

def run(self):
requested_events = PublicationEvent.objects.select_related(
"published_object", "published_object__content", "published_object__content__image"
).filter(state_of_processing="REQUESTED")

for publication_event in requested_events.iterator():
content = publication_event.published_object
extra_content_dir = content.get_extra_contents_directory()
building_extra_content_path = Path(
str(Path(extra_content_dir).parent) + "__building", "extra_contents", content.content_public_slug
)
if not building_extra_content_path.exists():
building_extra_content_path.mkdir(parents=True)
base_name = str(building_extra_content_path)
md_file_path = base_name + ".md"

logger.info("Exporting « %s » as %s", content.title(), publication_event.format_requested)
publication_event.state_of_processing = "RUNNING"
publication_event.save()

publicator = PublicatorRegistry.get(publication_event.format_requested)
try:
content = publication_event.published_object
extra_content_dir = content.get_extra_contents_directory()
building_extra_content_path = Path(
str(Path(extra_content_dir).parent) + "__building", "extra_contents", content.content_public_slug
)
if not building_extra_content_path.exists():
building_extra_content_path.mkdir(parents=True)
base_name = str(building_extra_content_path)
md_file_path = base_name + ".md"

logger.info("Exporting « %s » as %s", content.title(), publication_event.format_requested)
publication_event.state_of_processing = "RUNNING"
publication_event.save()

publicator = PublicatorRegistry.get(publication_event.format_requested)
publicator.publish(md_file_path, base_name)
except FailureDuringPublication:
logger.error("Failed to export « %s » as %s", content.title(), publication_event.format_requested)
except:
# Update and save the publication state before logging, in case
# content.title() would raise an exception (it already used to
# happen!).
publication_event.state_of_processing = "FAILURE"
publication_event.save()
logger.exception("Failed to export « %s » as %s", content.title(), publication_event.format_requested)
else:
logger.info("Succeed to export « %s » as %s", content.title(), publication_event.format_requested)
publication_event.state_of_processing = "SUCCESS"
publication_event.save()
publication_event.save()
logger.info("Succeed to export « %s » as %s", content.title(), publication_event.format_requested)

0 comments on commit f8d2136

Please sign in to comment.