From 1bc78ceb5da49586672bd1a5bf9e3a7555abf187 Mon Sep 17 00:00:00 2001 From: PCoder Date: Fri, 1 Sep 2017 02:33:31 +0530 Subject: [PATCH] Added save_ssh_key_error_handler --- opennebula_api/models.py | 5 +++-- utils/tasks.py | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index c115f225..f88f3b15 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -8,7 +8,7 @@ from oca.pool import WrongNameError, WrongIdError from hosting.models import HostingOrder from utils.models import CustomUser -from utils.tasks import save_ssh_key +from utils.tasks import save_ssh_key, save_ssh_key_error_handler from .exceptions import KeyExistsError, UserExistsError, UserCredentialError logger = logging.getLogger(__name__) @@ -553,7 +553,8 @@ class OpenNebulaManager(): hosts = self.get_all_hosts() if len(hosts) > 0 and len(keys) > 0: - save_ssh_key.apply_async((hosts, keys), countdown=countdown) + save_ssh_key.apply_async((hosts, keys), countdown=countdown, + link_error=save_ssh_key_error_handler.s()) else: logger.debug("Keys and hosts are empty, so not managing any keys") diff --git a/utils/tasks.py b/utils/tasks.py index 5c6aaeab..1844bc16 100644 --- a/utils/tasks.py +++ b/utils/tasks.py @@ -1,6 +1,8 @@ -import cdist import tempfile + +import cdist from cdist.integration import configure_hosts_simple +from celery.result import AsyncResult from celery.utils.log import get_task_logger from django.conf import settings from django.core.mail import EmailMessage @@ -66,3 +68,19 @@ def save_ssh_key(self, hosts, keys): logger.error(cdist_exception) return_value = False return return_value + + +@app.task +def save_ssh_key_error_handler(uuid): + result = AsyncResult(uuid) + exc = result.get(propagate=False) + logger.error('Task {0} raised exception: {1!r}\n{2!r}'.format( + uuid, exc, result.traceback)) + email_data = { + 'subject': "[celery error] Save SSH key error {0}".format(uuid), + 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, + 'to': ['info@ungleich.ch'], + 'body': "Task Id: {0}\nResult: {1}\nTraceback: {2}".format( + uuid, exc, result.traceback), + } + send_plain_email_task(email_data)