0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-01-29 03:11:34 +00:00
alerta_alerta/alerta/tasks.py
fshaikh-eg d4b7848fb0 Pass timeout to action process_action and take_action functions (#1042)
* added kwarg for timeout for process_action so it can be used in a plugin

* change default to 3 values expected and exception to 4 values

* changed time_out to timeout and fixed kwargs ordering

* implementing recommended improvements to get length of tuple

* updated exception logic

* fixed formatting
2019-08-13 20:01:15 +02:00

43 lines
1.3 KiB
Python

from typing import List
from alerta.app import create_celery_app
from alerta.exceptions import InvalidAction, RejectException
from alerta.models.alert import Alert
from alerta.utils.api import process_action, process_status
celery = create_celery_app()
@celery.task
def action_alerts(alerts: List[str], action: str, text: str, timeout: int) -> None:
updated = []
errors = []
for alert_id in alerts:
alert = Alert.find_by_id(alert_id)
try:
previous_status = alert.status
alert, action, text, timeout = process_action(alert, action, text, timeout)
alert = alert.from_action(action, text, timeout)
except RejectException as e:
errors.append(str(e))
continue
except InvalidAction as e:
errors.append(str(e))
continue
except Exception as e:
errors.append(str(e))
continue
if previous_status != alert.status:
try:
alert, status, text = process_status(alert, alert.status, text)
alert = alert.from_status(status, text, timeout)
except RejectException as e:
errors.append(str(e))
continue
except Exception as e:
errors.append(str(e))
continue
updated.append(alert.id)