From a06753ca4b3b85f8fc05d0338500b289b89b78e2 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Fri, 15 Sep 2017 13:09:46 +0200 Subject: [PATCH 1/2] Use hostname and send celery errors to DCL_ERROR_EMAILS_TO --- datacenterlight/tasks.py | 6 ++++-- dynamicweb/settings/base.py | 17 +++++++++++++++-- utils/tasks.py | 11 +++++++---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/datacenterlight/tasks.py b/datacenterlight/tasks.py index 1e3e1caa..dea4e0a6 100644 --- a/datacenterlight/tasks.py +++ b/datacenterlight/tasks.py @@ -1,5 +1,6 @@ from dynamicweb.celery import app from celery.utils.log import get_task_logger +from celery import current_task from django.conf import settings from opennebula_api.models import OpenNebulaManager from opennebula_api.serializers import VirtualMachineSerializer @@ -45,6 +46,7 @@ def create_vm_task(self, vm_template_id, user, specs, template, stripe_customer_id, billing_address_data, billing_address_id, charge, cc_details): + logger.debug("Running create_vm_task on {}".format(current_task.request.hostname)) vm_id = None try: final_price = specs.get('price') @@ -134,8 +136,8 @@ def create_vm_task(self, vm_template_id, user, specs, template, email_data = { 'subject': '{} CELERY TASK ERROR: {}'.format(settings.DCL_TEXT, msg_text), - 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, - 'to': ['info@ungleich.ch'], + 'from_email': current_task.request.hostname, + 'to': settings.DCL_ERROR_EMAILS_TO_LIST, 'body': ',\n'.join(str(i) for i in self.request.args) } email = EmailMessage(**email_data) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 29533211..a7aa6350 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -173,7 +173,8 @@ TEMPLATES = [ os.path.join(PROJECT_DIR, 'nosystemd/templates/'), os.path.join(PROJECT_DIR, 'ungleich/templates/djangocms_blog/'), - os.path.join(PROJECT_DIR, 'ungleich/templates/cms/ungleichch'), + os.path.join(PROJECT_DIR, + 'ungleich/templates/cms/ungleichch'), os.path.join(PROJECT_DIR, 'ungleich/templates/ungleich'), os.path.join(PROJECT_DIR, 'ungleich_page/templates/ungleich_page'), @@ -559,9 +560,21 @@ CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND') CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' -#CELERY_TIMEZONE = 'Europe/Zurich' +# CELERY_TIMEZONE = 'Europe/Zurich' CELERY_MAX_RETRIES = int_env('CELERY_MAX_RETRIES', 5) +DCL_ERROR_EMAILS_TO = env('DCL_ERROR_EMAILS_TO_ADDRESS') + +DCL_ERROR_EMAILS_TO_LIST = [] +if DCL_ERROR_EMAILS_TO is not None: + DCL_ERROR_EMAILS_TO_ADDRESS_LIST = [x.strip() for x in + DCL_ERROR_EMAILS_TO.split( + ',')] \ + if "," in DCL_ERROR_EMAILS_TO else [DCL_ERROR_EMAILS_TO.strip()] + +if 'info@ungleich.ch' not in DCL_ERROR_EMAILS_TO_LIST: + DCL_ERROR_EMAILS_TO_LIST.append('info@ungleich.ch') + ENABLE_DEBUG_LOGGING = bool_env('ENABLE_DEBUG_LOGGING') if ENABLE_DEBUG_LOGGING: diff --git a/utils/tasks.py b/utils/tasks.py index d66c37ee..23deac51 100644 --- a/utils/tasks.py +++ b/utils/tasks.py @@ -3,6 +3,7 @@ import tempfile import cdist from cdist.integration import configure_hosts_simple from celery.result import AsyncResult +from celery import current_task from celery.utils.log import get_task_logger from django.conf import settings from django.core.mail import EmailMessage @@ -38,6 +39,8 @@ def save_ssh_key(self, hosts, keys): 'state': True # whether key is to be added or } # removed """ + logger.debug( + "Running save_ssh_key on {}".format(current_task.request.hostname)) logger.debug("""Running save_ssh_key task for Hosts: {hosts_str} Keys: {keys_str}""".format(hosts_str=", ".join(hosts), @@ -70,8 +73,8 @@ def save_ssh_key(self, hosts, keys): email_data = { 'subject': "celery save_ssh_key error - task id {0}".format( self.request.id.__str__()), - 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, - 'to': ['info@ungleich.ch'], + 'from_email': current_task.request.hostname, + 'to': settings.DCL_ERROR_EMAILS_TO_LIST, 'body': "Task Id: {0}\nResult: {1}\nTraceback: {2}".format( self.request.id.__str__(), False, str(cdist_exception)), } @@ -87,8 +90,8 @@ def save_ssh_key_error_handler(uuid): 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'], + 'from_email': current_task.request.hostname, + 'to': settings.DCL_ERROR_EMAILS_TO_LIST, 'body': "Task Id: {0}\nResult: {1}\nTraceback: {2}".format( uuid, exc, result.traceback), } From e6514f8061835ba552376fc148d9560f1d8c5061 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Fri, 15 Sep 2017 13:10:11 +0200 Subject: [PATCH 2/2] Refactored DCL_ERROR_EMAILS_TO_ADDRESS to DCL_ERROR_EMAILS_TO --- dynamicweb/settings/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index a7aa6350..dc2917ea 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -563,12 +563,12 @@ CELERY_RESULT_SERIALIZER = 'json' # CELERY_TIMEZONE = 'Europe/Zurich' CELERY_MAX_RETRIES = int_env('CELERY_MAX_RETRIES', 5) -DCL_ERROR_EMAILS_TO = env('DCL_ERROR_EMAILS_TO_ADDRESS') +DCL_ERROR_EMAILS_TO = env('DCL_ERROR_EMAILS_TO') DCL_ERROR_EMAILS_TO_LIST = [] if DCL_ERROR_EMAILS_TO is not None: - DCL_ERROR_EMAILS_TO_ADDRESS_LIST = [x.strip() for x in - DCL_ERROR_EMAILS_TO.split( + DCL_ERROR_EMAILS_TO_LIST = [x.strip() for x in + DCL_ERROR_EMAILS_TO.split( ',')] \ if "," in DCL_ERROR_EMAILS_TO else [DCL_ERROR_EMAILS_TO.strip()]