From e08a2edfa70c2ccfbab626a49d89a4ccad634270 Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Sat, 16 Jan 2021 01:39:31 +0100
Subject: [PATCH] =?UTF-8?q?Jobs:=20V=20pr=C5=AFb=C4=9Bhu=20jobu=20se=20sb?=
 =?UTF-8?q?=C3=ADraj=C3=AD=20hl=C3=A1=C5=A1en=C3=AD=20o=20chyb=C3=A1ch?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mo/jobs/__init__.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/mo/jobs/__init__.py b/mo/jobs/__init__.py
index 60b5d811..b017d1c9 100644
--- a/mo/jobs/__init__.py
+++ b/mo/jobs/__init__.py
@@ -4,7 +4,7 @@ from datetime import timedelta
 import os
 import secrets
 from sqlalchemy import or_
-from typing import Optional, Dict, Callable
+from typing import Optional, Dict, Callable, List
 
 import mo
 import mo.config as config
@@ -28,10 +28,12 @@ class TheJob:
 
     job: db.Job
     job_id: Optional[int]
+    errors: List[str]
 
     def __init__(self, job_id: Optional[int] = None):
         """Pokud chceme pracovat s existujícím jobem, zadáme jeho ID."""
         self.job_id = job_id
+        self.errors = []
 
     def load(self) -> db.Job:
         sess = db.get_session()
@@ -95,6 +97,9 @@ class TheJob:
         logger.info(f'Job: Ruším job #{self.job.job_id}')
         return self._finish_remove()
 
+    def error(self, msg):
+        self.errors.append(msg)
+
     def run(self):
         sess = db.get_session()
         if not self.load() or self.job.state != db.JobState.ready:
@@ -110,11 +115,17 @@ class TheJob:
 
         try:
             _handler_table[job.type](self)
-            logger.info(f'Job: Úspěšně dokončen job #{job.job_id}')
-            job.state = db.JobState.done
+            if self.errors:
+                logger.info(f'Job: Neúspěšně dokončen job #{job.job_id}')
+                job.state = db.JobState.failed
+                job.out_json = {'errors': self.errors}
+            else:
+                logger.info(f'Job: Úspěšně dokončen job #{job.job_id}')
+                job.state = db.JobState.done
         except Exception as e:
             logger.error(f'Job: Chyba při zpracování jobu #{job.job_id}: %s', e, exc_info=e)
             job.state = db.JobState.failed
+            job.out_json = {'errors': 'Interní chyba, informujte prosím správce systému.'}
             return
 
         job.finished_at = mo.util.get_now()
-- 
GitLab