From 0a99a8ca13721db8212cf22ed1395edbba94fa93 Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Mon, 7 Aug 2023 16:32:29 +0200 Subject: [PATCH] Added exception handling to client.py --- tools/client.py | 7 ++++--- tools/jobs.py | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tools/client.py b/tools/client.py index bf7cc8c64..2313470a3 100644 --- a/tools/client.py +++ b/tools/client.py @@ -9,7 +9,7 @@ sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../lib') -from jobs import get_job, process_job +from jobs import get_job, process_job, handle_job_exception from global_config import GlobalConfig from db import DB @@ -19,7 +19,7 @@ # We currently have this dynamically as it will probably change quite a bit STATUS_LIST = ['job_no', 'job_start', 'job_error', 'job_end', 'cleanup_start', 'cleanup_stop'] - +# pylint: disable=redefined-outer-name def set_status(status_code, data=None, project_id=None): if status_code not in STATUS_LIST: raise ValueError(f"Status code not valid: '{status_code}'. Should be in: {STATUS_LIST}") @@ -33,7 +33,7 @@ def set_status(status_code, data=None, project_id=None): DB().query(query=query, params=params) - +# pylint: disable=broad-exception-caught if __name__ == '__main__': while True: @@ -49,6 +49,7 @@ def set_status(status_code, data=None, project_id=None): process_job(*job) except Exception as exc: set_status('job_error', str(exc), project_id) + handle_job_exception(exc, project_id) else: set_status('job_end', '', project_id) diff --git a/tools/jobs.py b/tools/jobs.py index 8f3971743..03f882335 100644 --- a/tools/jobs.py +++ b/tools/jobs.py @@ -132,6 +132,20 @@ def _do_project_job(job_id, project_id, skip_config_check=False, docker_prune=Fa except Exception as exc: raise exc +# pylint: disable=redefined-outer-name +def handle_job_exception(exce, p_id): + project_name = None + client_mail = None + if p_id: + [project_name, _, client_mail, _, _] = get_project(p_id) + + error_helpers.log_error('Base exception occurred in jobs.py: ', exce) + email_helpers.send_error_email(GlobalConfig().config['admin']['email'], error_helpers.format_error( + 'Base exception occurred in jobs.py: ', exce), project_id=p_id, name=project_name) + + # reduced error message to client + if client_mail and GlobalConfig().config['admin']['email'] != client_mail: + email_helpers.send_error_email(client_mail, exce, project_id=p_id, name=project_name) if __name__ == '__main__': #pylint: disable=broad-except,invalid-name @@ -171,15 +185,4 @@ def _do_project_job(job_id, project_id, skip_config_check=False, docker_prune=Fa process_job(job[0], job[1], job[2], args.skip_config_check, args.docker_prune, args.full_docker_prune) print('Successfully processed jobs queue item.') except Exception as exce: - project_name = None - client_mail = None - if p_id: - [project_name, _, client_mail, _, _] = get_project(p_id) - - error_helpers.log_error('Base exception occurred in jobs.py: ', exce) - email_helpers.send_error_email(GlobalConfig().config['admin']['email'], error_helpers.format_error( - 'Base exception occurred in jobs.py: ', exce), project_id=p_id, name=project_name) - - # reduced error message to client - if client_mail and GlobalConfig().config['admin']['email'] != client_mail: - email_helpers.send_error_email(client_mail, exce, project_id=p_id, name=project_name) + handle_job_exception(exce, p_id)