Skip to content

Commit

Permalink
Added exception handling to client.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneTR committed Aug 7, 2023
1 parent 5d1c062 commit 0a99a8c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
7 changes: 4 additions & 3 deletions tools/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}")
Expand All @@ -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:
Expand All @@ -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)

Expand Down
27 changes: 15 additions & 12 deletions tools/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 0a99a8c

Please sign in to comment.