-
-
Notifications
You must be signed in to change notification settings - Fork 226
[change] Update trigger_vpn_server_endpoint to send generic_message #… #1104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
import logging | ||
from http import HTTPStatus | ||
from time import sleep | ||
|
||
from celery import shared_task | ||
from django.core.cache import cache | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.utils.translation import gettext as _ | ||
from openwisp_notifications.signals import notify | ||
from requests.exceptions import RequestException | ||
from swapper import load_model | ||
|
||
from openwisp_controller.config.api.zerotier_service import ZerotierService | ||
from openwisp_utils.tasks import OpenwispCeleryTask | ||
|
||
from .settings import API_TASK_RETRY_OPTIONS | ||
from .utils import handle_error_notification, handle_recovery_notification | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -27,48 +24,6 @@ class OpenwispApiTask(OpenwispCeleryTask): | |
HTTPStatus.GATEWAY_TIMEOUT, # 504 | ||
] | ||
|
||
def _send_api_task_notification(self, type, **kwargs): | ||
vpn = kwargs.get("instance") | ||
action = kwargs.get("action").replace("_", " ") | ||
status_code = kwargs.get("status_code") | ||
# Adding some delay here to prevent overlapping | ||
# of the django success message container | ||
# with the ow-notification container | ||
# https://github.com/openwisp/openwisp-notifications/issues/264 | ||
sleep(2) | ||
message_map = { | ||
"error": { | ||
"verb": _("encountered an unrecoverable error"), | ||
"message": _( | ||
"Unable to perform {action} operation on the " | ||
"{target} VPN server due to an " | ||
"unrecoverable error " | ||
"(status code: {status_code})" | ||
), | ||
"level": "error", | ||
}, | ||
"recovery": { | ||
"verb": _("has been completed successfully"), | ||
"message": _("The {action} operation on {target} {verb}."), | ||
"level": "info", | ||
}, | ||
} | ||
meta = message_map[type] | ||
notify.send( | ||
type="generic_message", | ||
sender=vpn, | ||
target=vpn, | ||
action=action, | ||
verb=meta["verb"], | ||
message=meta["message"].format( | ||
action=action, | ||
target=str(vpn), | ||
status_code=status_code, | ||
verb=meta["verb"], | ||
), | ||
level=meta["level"], | ||
) | ||
|
||
def handle_api_call(self, fn, *args, send_notification=True, **kwargs): | ||
""" | ||
This method handles API calls and their responses | ||
|
@@ -105,10 +60,7 @@ def handle_api_call(self, fn, *args, send_notification=True, **kwargs): | |
response.raise_for_status() | ||
logger.info(info_msg) | ||
if send_notification: | ||
task_result = cache.get(task_key) | ||
if task_result == "error": | ||
self._send_api_task_notification("recovery", **kwargs) | ||
cache.set(task_key, "success", None) | ||
handle_recovery_notification(task_key, **kwargs) | ||
except RequestException as e: | ||
if response.status_code in self._RECOVERABLE_API_CODES: | ||
retry_logger = logger.warn | ||
|
@@ -122,12 +74,7 @@ def handle_api_call(self, fn, *args, send_notification=True, **kwargs): | |
raise e | ||
logger.error(f"{err_msg}, Error: {e}") | ||
if send_notification: | ||
task_result = cache.get(task_key) | ||
if task_result in (None, "success"): | ||
cache.set(task_key, "error", None) | ||
self._send_api_task_notification( | ||
"error", status_code=response.status_code, **kwargs | ||
) | ||
handle_error_notification(task_key, response, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pass in snooze=2 here |
||
return (response, updated_config) if updated_config else response | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,14 @@ | ||||||
import logging | ||||||
from time import sleep | ||||||
|
||||||
from django.core.cache import cache | ||||||
from django.core.exceptions import ValidationError | ||||||
from django.db.models import Q | ||||||
from django.http import Http404, HttpResponse | ||||||
from django.shortcuts import get_object_or_404 as base_get_object_or_404 | ||||||
from django.urls import path, re_path | ||||||
from django.utils.translation import gettext as _ | ||||||
from openwisp_notifications.signals import notify | ||||||
from openwisp_notifications.utils import _get_object_link | ||||||
|
||||||
logger = logging.getLogger(__name__) | ||||||
|
@@ -206,3 +210,60 @@ def get_default_templates_queryset( | |||||
def get_config_error_notification_target_url(obj, field, absolute_url=True): | ||||||
url = _get_object_link(obj._related_object(field), absolute_url) | ||||||
return f"{url}#config-group" | ||||||
|
||||||
|
||||||
def send_api_task_notification(type, **kwargs): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
vpn = kwargs.get("instance") | ||||||
action = kwargs.get("action").replace("_", " ") | ||||||
status_code = kwargs.get("status_code") | ||||||
# Adding some delay here to prevent overlapping | ||||||
# of the django success message container | ||||||
# with the ow-notification container | ||||||
# https://github.com/openwisp/openwisp-notifications/issues/264 | ||||||
sleep(2) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be optional. Eg: if snooze:
sleep(snooze) |
||||||
message_map = { | ||||||
"error": { | ||||||
"verb": _("encountered an unrecoverable error"), | ||||||
"message": _( | ||||||
"Unable to perform {action} operation on the " | ||||||
"{target} VPN server due to an " | ||||||
"unrecoverable error " | ||||||
"(status code: {status_code})" | ||||||
), | ||||||
"level": "error", | ||||||
}, | ||||||
"recovery": { | ||||||
"verb": _("has been completed successfully"), | ||||||
"message": _("The {action} operation on {target} {verb}."), | ||||||
"level": "info", | ||||||
}, | ||||||
} | ||||||
meta = message_map[type] | ||||||
notify.send( | ||||||
type="generic_message", | ||||||
sender=vpn, | ||||||
target=vpn, | ||||||
action=action, | ||||||
verb=meta["verb"], | ||||||
message=meta["message"].format( | ||||||
action=action, | ||||||
target=str(vpn), | ||||||
status_code=status_code, | ||||||
verb=meta["verb"], | ||||||
), | ||||||
level=meta["level"], | ||||||
) | ||||||
|
||||||
|
||||||
def handle_recovery_notification(task_key, **kwargs): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we extracting this logic here if we aren't reusing it for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought this is something that can be used and would be needed to be exposed in future and looks cleaner. Just as we thought _send_api_task_notification wasn't meant to be exposed separately initially but as we see it had to be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #1104 (comment). |
||||||
task_result = cache.get(task_key) | ||||||
if task_result == "error": | ||||||
send_api_task_notification("recovery", **kwargs) | ||||||
cache.set(task_key, "success", None) | ||||||
|
||||||
|
||||||
def handle_error_notification(task_key, response, **kwargs): | ||||||
task_result = cache.get(task_key) | ||||||
if task_result in (None, "success"): | ||||||
cache.set(task_key, "error", None) | ||||||
send_api_task_notification("error", status_code=response.status_code, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass in snooze=2 here