Work on System Notification handler

This commit is contained in:
mike cullerton 2023-01-10 08:44:53 -05:00
parent e62cdad588
commit 61699b6b5d
2 changed files with 86 additions and 40 deletions

View File

@ -74,3 +74,6 @@ SPIFF_DATABASE_TYPE = environ.get(
SPIFFWORKFLOW_BACKEND_DATABASE_URI = environ.get( SPIFFWORKFLOW_BACKEND_DATABASE_URI = environ.get(
"SPIFFWORKFLOW_BACKEND_DATABASE_URI", default=None "SPIFFWORKFLOW_BACKEND_DATABASE_URI", default=None
) )
SYSTEM_NOTIFICATION_PROCESS_MODEL_MESSAGE_ID = environ.get(
"SYSTEM_NOTIFICATION_PROCESS_MODEL_MESSAGE_ID", default="Message_SystemMessageNotification"
)

View File

@ -1,4 +1,9 @@
"""Error_handling_service.""" """Error_handling_service."""
import json
from flask import g
from flask import current_app
from flask.wrappers import Response
from typing import Any from typing import Any
from typing import List from typing import List
from typing import Union from typing import Union
@ -6,9 +11,15 @@ from typing import Union
from flask_bpmn.api.api_error import ApiError from flask_bpmn.api.api_error import ApiError
from flask_bpmn.models.db import db from flask_bpmn.models.db import db
from spiffworkflow_backend.models.message_model import MessageModel
from spiffworkflow_backend.models.message_triggerable_process_model import (
MessageTriggerableProcessModel,
)
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
from spiffworkflow_backend.models.process_instance import ProcessInstanceModelSchema
from spiffworkflow_backend.models.process_instance import ProcessInstanceStatus from spiffworkflow_backend.models.process_instance import ProcessInstanceStatus
from spiffworkflow_backend.services.email_service import EmailService from spiffworkflow_backend.services.email_service import EmailService
from spiffworkflow_backend.services.message_service import MessageService
from spiffworkflow_backend.services.process_instance_processor import ( from spiffworkflow_backend.services.process_instance_processor import (
ProcessInstanceProcessor, ProcessInstanceProcessor,
) )
@ -37,6 +48,7 @@ class ErrorHandlingService:
process_model = ProcessModelService.get_process_model( process_model = ProcessModelService.get_process_model(
_processor.process_model_identifier _processor.process_model_identifier
) )
# First, suspend or fault the instance
if process_model.fault_or_suspend_on_exception == "suspend": if process_model.fault_or_suspend_on_exception == "suspend":
self.set_instance_status( self.set_instance_status(
_processor.process_instance_model.id, _processor.process_instance_model.id,
@ -49,57 +61,88 @@ class ErrorHandlingService:
ProcessInstanceStatus.error.value, ProcessInstanceStatus.error.value,
) )
# Second, call the System Notification Process
# Note that this isn't the best way to do this.
# The configs are all in the model.
# Maybe we can move some of this to the notification process, or dmn tables.
if len(process_model.exception_notification_addresses) > 0: if len(process_model.exception_notification_addresses) > 0:
try: try:
# some notification method (waku?) self.handle_system_notification(_error, process_model.exception_notification_addresses)
self.handle_email_notification(
_processor, _error, process_model.exception_notification_addresses
)
except Exception as e: except Exception as e:
# hmm... what to do if a notification method fails. Probably log, at least # hmm... what to do if a notification method fails. Probably log, at least
print(e) print(e)
@staticmethod @staticmethod
def hanle_sentry_notification(_error: ApiError, _recipients: List) -> None: def handle_system_notification(
"""SentryHandler."""
...
@staticmethod
def handle_email_notification(
processor: ProcessInstanceProcessor,
error: Union[ApiError, Exception], error: Union[ApiError, Exception],
recipients: List, recipients: List,
) -> None: ):
"""EmailHandler.""" message_payload = {
subject = "Unexpected error in app" 'my_var': 'my_value',
if isinstance(error, ApiError): 'error': error.__repr__(),
content = f"{error.message}" 'recipients': recipients
else: }
content = str(error) message_identifier = current_app.config["SYSTEM_NOTIFICATION_PROCESS_MODEL_MESSAGE_ID"]
content_html = content message_model = MessageModel.query.filter_by(identifier=message_identifier).first()
message_triggerable_process_model = (
EmailService.add_email( MessageTriggerableProcessModel.query.filter_by(
subject, message_model_id=message_model.id
"sender@company.com", ).first()
recipients, )
content, process_instance = MessageService.process_message_triggerable_process_model(
content_html, message_triggerable_process_model,
cc=None, message_identifier,
bcc=None, message_payload,
reply_to=None, g.user
attachment_files=None,
) )
@staticmethod return Response(
def handle_waku_notification(_error: ApiError, _recipients: List) -> Any: json.dumps(ProcessInstanceModelSchema().dump(process_instance)),
"""WakuHandler.""" status=200,
# class WakuMessage: mimetype="application/json",
# """WakuMessage.""" )
#
# payload: str # @staticmethod
# contentTopic: str # Optional # def handle_sentry_notification(_error: ApiError, _recipients: List) -> None:
# version: int # Optional # """SentryHandler."""
# timestamp: int # Optional # ...
#
# @staticmethod
# def handle_email_notification(
# processor: ProcessInstanceProcessor,
# error: Union[ApiError, Exception],
# recipients: List,
# ) -> None:
# """EmailHandler."""
# subject = "Unexpected error in app"
# if isinstance(error, ApiError):
# content = f"{error.message}"
# else:
# content = str(error)
# content_html = content
#
# EmailService.add_email(
# subject,
# "sender@company.com",
# recipients,
# content,
# content_html,
# cc=None,
# bcc=None,
# reply_to=None,
# attachment_files=None,
# )
#
# @staticmethod
# def handle_waku_notification(_error: ApiError, _recipients: List) -> Any:
# """WakuHandler."""
# # class WakuMessage:
# # """WakuMessage."""
# #
# # payload: str
# # contentTopic: str # Optional
# # version: int # Optional
# # timestamp: int # Optional
class FailingService: class FailingService: