Skip to content

Commit

Permalink
Jobs.py now also submits machine in email
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneTR committed Aug 8, 2023
1 parent b07325e commit a397ea2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
8 changes: 6 additions & 2 deletions lib/email_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def send_admin_email(subject, body):
send_email(message, config['admin']['email'])


def send_error_email(receiver_email, error, project_id=None, name=None):
def send_error_email(receiver_email, error, project_id=None, name=None, machine=None):
config = GlobalConfig().config
message = """\
From: {smtp_sender}
Expand All @@ -54,6 +54,7 @@ def send_error_email(receiver_email, error, project_id=None, name=None):
Name: {name}
Project id: {project_id}
Machine: {machine}
Link: {url}/stats.html?id={project_id}
{errors}
Expand All @@ -65,20 +66,22 @@ def send_error_email(receiver_email, error, project_id=None, name=None):
receiver_email=receiver_email,
errors=error,
name=name,
machine=machine,
url=config['cluster']['metrics_url'],
project_id=project_id,
smtp_sender=config['smtp']['sender'])
send_email(message, receiver_email)


def send_report_email(receiver_email, report_id, name):
def send_report_email(receiver_email, report_id, name, machine=None):
config = GlobalConfig().config
message = """\
From: {smtp_sender}
To: {receiver_email}
Subject: Your Green Metric report is ready
Project name: {name}
Machine: {machine}
Your report is now accessible under the URL: {url}/stats.html?id={report_id}
Expand All @@ -88,6 +91,7 @@ def send_report_email(receiver_email, report_id, name):
message = message.format(
receiver_email=receiver_email,
report_id=report_id,
machine=machine,
name=name,
url=config['cluster']['metrics_url'],
smtp_sender=config['smtp']['sender'])
Expand Down
17 changes: 10 additions & 7 deletions tools/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def clear_old_jobs():

def get_project(project_id):
data = DB().fetch_one(
"SELECT name, uri,email,branch,filename FROM projects WHERE id = %s LIMIT 1", (project_id, ))
"""SELECT p.name, p.uri, p.email, p.branch, p.filename, m.description
FROM projects as p
LEFT JOIN machines AS m ON p.machine_id = m.id
WHERE p.id = %s LIMIT 1""", (project_id, ))

if (data is None or data == []):
raise RuntimeError(f"couldn't find project w/ id: {project_id}")
Expand All @@ -97,11 +100,11 @@ def process_job(job_id, job_type, project_id, skip_config_check=False, docker_pr
def _do_email_job(job_id, project_id):
check_job_running('email', job_id)

[name, _, email, _, _] = get_project(project_id)
[name, _, email, _, _, machine] = get_project(project_id)

config = GlobalConfig().config
if (config['admin']['notify_admin_for_own_project_ready'] or config['admin']['email'] != email):
email_helpers.send_report_email(email, project_id, name)
email_helpers.send_report_email(email, project_id, name, machine=machine)

delete_job(job_id)

Expand All @@ -110,7 +113,7 @@ def _do_email_job(job_id, project_id):
def _do_project_job(job_id, project_id, skip_config_check=False, docker_prune=False, full_docker_prune=False):
check_job_running('project', job_id)

[_, uri, _, branch, filename] = get_project(project_id)
[_, uri, _, branch, filename, _] = get_project(project_id)

runner = Runner(
uri=uri,
Expand All @@ -137,15 +140,15 @@ def handle_job_exception(exce, p_id):
project_name = None
client_mail = None
if p_id:
[project_name, _, client_mail, _, _] = get_project(p_id)
[project_name, _, client_mail, _, _, machine] = 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)
'Base exception occurred in jobs.py: ', exce), project_id=p_id, name=project_name, machine=machine)

# 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)
email_helpers.send_error_email(client_mail, exce, project_id=p_id, name=project_name, machine=machine)

if __name__ == '__main__':
#pylint: disable=broad-except,invalid-name
Expand Down

0 comments on commit a397ea2

Please sign in to comment.