mirror of
https://framagit.org/framasoft/framaspace/argos.git
synced 2025-04-28 18:02:41 +02:00
🎨 — Make each handler build its own message
This commit is contained in:
parent
2ab9afabc9
commit
9ed58220f4
3 changed files with 31 additions and 19 deletions
|
@ -22,10 +22,18 @@ def handle_alert(config: Config, result, task, severity, request):
|
|||
result.status,
|
||||
severity)
|
||||
|
||||
if config.general.mail is not None or \
|
||||
config.general.gotify is not None:
|
||||
if config.general.mail is not None and \
|
||||
'mail' in getattr(config.general.alerts, severity):
|
||||
notify_by_mail(result, task, severity, config.general.mail, request)
|
||||
|
||||
if config.general.gotify is not None and \
|
||||
'gotify' in getattr(config.general.alerts, severity):
|
||||
notify_with_gotify(result, task, severity, config.general.gotify, request)
|
||||
|
||||
|
||||
def notify_by_mail(result, task, severity, config: Mail, request) -> None:
|
||||
logger.debug('Will send mail notification')
|
||||
|
||||
subject = f"{urlparse(task.url).netloc}: status {severity}"
|
||||
msg = f"""\
|
||||
URL: {task.url}
|
||||
Check: {task.check}
|
||||
|
@ -35,18 +43,8 @@ Time: {result.submitted_at}
|
|||
See results of task on {request.url_for('get_task_results', task_id=task.id)}
|
||||
"""
|
||||
|
||||
if config.general.mail is not None and \
|
||||
'mail' in getattr(config.general.alerts, severity):
|
||||
notify_by_mail(subject, msg, config.general.mail)
|
||||
if config.general.gotify is not None and \
|
||||
'gotify' in getattr(config.general.alerts, severity):
|
||||
notify_with_gotify(subject, msg, severity, config.general.gotify)
|
||||
|
||||
|
||||
def notify_by_mail(subject: str, msg: str, config: Mail) -> None:
|
||||
logger.debug('Will send mail notification')
|
||||
mail = f"""\
|
||||
Subject: [Argos] {subject}
|
||||
Subject: [Argos] {urlparse(task.url).netloc}: status {severity}
|
||||
|
||||
{msg}"""
|
||||
|
||||
|
@ -75,16 +73,29 @@ Subject: [Argos] {subject}
|
|||
smtp.sendmail(config.mailfrom, address, mail)
|
||||
|
||||
|
||||
def notify_with_gotify(subject: str, msg: str, severity: str, config: List[GotifyUrl]) -> None:
|
||||
def notify_with_gotify(result, task, severity: str, config: List[GotifyUrl], request) -> None:
|
||||
logger.debug('Will send gotify notification')
|
||||
headers = {'accept': 'application/json',
|
||||
'content-type': 'application/json'}
|
||||
|
||||
priority = 9
|
||||
icon = '❌'
|
||||
if severity == Severity.OK:
|
||||
priority = 1
|
||||
icon = '✅'
|
||||
elif severity == Severity.WARNING:
|
||||
priority = 5
|
||||
icon = '⚠️'
|
||||
|
||||
subject = f"{icon} {urlparse(task.url).netloc}: status {severity}"
|
||||
msg = f"""\
|
||||
URL: {task.url}
|
||||
Check: {task.check}
|
||||
Status: {severity}
|
||||
Time: {result.submitted_at}
|
||||
|
||||
See results of task on {request.url_for('get_task_results', task_id=task.id)}
|
||||
"""
|
||||
|
||||
payload = {'title': subject,
|
||||
'message': msg,
|
||||
|
@ -99,7 +110,7 @@ def notify_with_gotify(subject: str, msg: str, severity: str, config: List[Gotif
|
|||
headers=headers,
|
||||
json=payload)
|
||||
res.raise_for_status()
|
||||
except httpx.RequestError as exc:
|
||||
except httpx.RequestError as err:
|
||||
logger.error('An error occurred while sending a message to %s with token %s',
|
||||
exc.request.url,
|
||||
err.request.url,
|
||||
token)
|
||||
|
|
|
@ -59,6 +59,7 @@ async def create_results(
|
|||
|
||||
# Don’t create an alert if the severity has not changed
|
||||
if last_severity != severity:
|
||||
# XXX Use a job queue or make it async
|
||||
handle_alert(config, result, task, severity, request)
|
||||
|
||||
db_results.append(result)
|
||||
|
|
Loading…
Reference in a new issue