2021-08-27 23:45:24 +02:00
|
|
|
import logging
|
2021-10-16 01:25:45 +02:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import List, Optional
|
2021-08-05 00:29:50 +02:00
|
|
|
from uuid import UUID
|
2021-07-07 11:45:54 +02:00
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus
|
2021-10-02 18:09:03 +02:00
|
|
|
from mobilizon_reshare.publishers import get_active_notifiers
|
2021-10-16 01:25:45 +02:00
|
|
|
from mobilizon_reshare.publishers.abstract import (
|
|
|
|
EventPublication,
|
|
|
|
AbstractPlatform,
|
|
|
|
RecapPublication,
|
|
|
|
)
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.publishers.exceptions import PublisherError
|
2021-10-02 18:09:03 +02:00
|
|
|
from mobilizon_reshare.publishers.platforms.platform_mapping import get_notifier_class
|
2021-07-07 11:45:54 +02:00
|
|
|
|
2021-08-27 23:45:24 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-07-07 11:45:54 +02:00
|
|
|
|
|
|
|
@dataclass
|
2021-10-16 01:25:45 +02:00
|
|
|
class BasePublicationReport:
|
2021-08-05 00:29:50 +02:00
|
|
|
status: PublicationStatus
|
2021-10-16 01:25:45 +02:00
|
|
|
reason: Optional[str]
|
|
|
|
|
|
|
|
def get_failure_message(self):
|
|
|
|
|
|
|
|
return (
|
|
|
|
f"Publication failed with status: {self.status}.\n" f"Reason: {self.reason}"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EventPublicationReport(BasePublicationReport):
|
2021-08-27 23:45:24 +02:00
|
|
|
publication_id: UUID
|
2021-07-07 11:45:54 +02:00
|
|
|
|
2021-10-16 01:25:45 +02:00
|
|
|
def get_failure_message(self):
|
|
|
|
|
|
|
|
return (
|
|
|
|
f"Publication {self.publication_id } failed with status: {self.status}.\n"
|
|
|
|
f"Reason: {self.reason}"
|
|
|
|
)
|
|
|
|
|
2021-07-07 11:45:54 +02:00
|
|
|
|
|
|
|
@dataclass
|
2021-10-16 01:25:45 +02:00
|
|
|
class BaseCoordinatorReport:
|
|
|
|
reports: List[BasePublicationReport]
|
2021-07-07 11:45:54 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def successful(self):
|
2021-10-16 01:25:45 +02:00
|
|
|
return all(r.status == PublicationStatus.COMPLETED for r in self.reports)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PublisherCoordinatorReport(BaseCoordinatorReport):
|
|
|
|
|
|
|
|
reports: List[EventPublicationReport]
|
|
|
|
publications: List[EventPublication]
|
2021-07-07 11:45:54 +02:00
|
|
|
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
class PublisherCoordinator:
|
|
|
|
def __init__(self, publications: List[EventPublication]):
|
|
|
|
self.publications = publications
|
2021-07-07 11:45:54 +02:00
|
|
|
|
|
|
|
def run(self) -> PublisherCoordinatorReport:
|
2021-08-05 00:29:50 +02:00
|
|
|
errors = self._validate()
|
2021-07-12 22:17:49 +02:00
|
|
|
if errors:
|
2021-08-27 23:45:24 +02:00
|
|
|
return PublisherCoordinatorReport(
|
2021-10-02 18:09:03 +02:00
|
|
|
reports=errors, publications=self.publications
|
2021-08-27 23:45:24 +02:00
|
|
|
)
|
2021-07-07 11:45:54 +02:00
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
return self._post()
|
2021-07-07 11:45:54 +02:00
|
|
|
|
2021-08-28 13:17:39 +02:00
|
|
|
def _make_successful_report(self, failed_ids):
|
2021-10-16 01:25:45 +02:00
|
|
|
return [
|
|
|
|
EventPublicationReport(
|
2021-08-27 23:45:24 +02:00
|
|
|
status=PublicationStatus.COMPLETED,
|
|
|
|
reason="",
|
2021-10-02 18:09:03 +02:00
|
|
|
publication_id=publication.id,
|
2021-07-15 18:13:11 +02:00
|
|
|
)
|
2021-10-02 18:09:03 +02:00
|
|
|
for publication in self.publications
|
|
|
|
if publication.id not in failed_ids
|
2021-10-16 01:25:45 +02:00
|
|
|
]
|
2021-07-07 11:45:54 +02:00
|
|
|
|
|
|
|
def _post(self):
|
2021-10-16 01:25:45 +02:00
|
|
|
reports = []
|
2021-10-02 18:09:03 +02:00
|
|
|
|
|
|
|
for publication in self.publications:
|
|
|
|
|
2021-07-07 11:45:54 +02:00
|
|
|
try:
|
2021-10-02 18:09:03 +02:00
|
|
|
message = publication.formatter.get_message_from_event(
|
|
|
|
publication.event
|
|
|
|
)
|
|
|
|
publication.publisher.send(message)
|
2021-10-16 01:25:45 +02:00
|
|
|
reports.append(
|
|
|
|
EventPublicationReport(
|
|
|
|
status=PublicationStatus.COMPLETED,
|
|
|
|
publication_id=publication.id,
|
|
|
|
reason=None,
|
|
|
|
)
|
|
|
|
)
|
2021-07-07 11:45:54 +02:00
|
|
|
except PublisherError as e:
|
2021-10-16 01:25:45 +02:00
|
|
|
reports.append(
|
|
|
|
EventPublicationReport(
|
|
|
|
status=PublicationStatus.FAILED,
|
|
|
|
reason=str(e),
|
|
|
|
publication_id=publication.id,
|
|
|
|
)
|
2021-07-07 11:45:54 +02:00
|
|
|
)
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
return PublisherCoordinatorReport(
|
2021-10-02 18:09:03 +02:00
|
|
|
publications=self.publications, reports=reports
|
2021-08-27 23:45:24 +02:00
|
|
|
)
|
2021-07-07 11:45:54 +02:00
|
|
|
|
|
|
|
def _validate(self):
|
2021-10-16 01:25:45 +02:00
|
|
|
errors = []
|
2021-10-02 18:09:03 +02:00
|
|
|
|
|
|
|
for publication in self.publications:
|
|
|
|
|
2021-08-05 00:29:50 +02:00
|
|
|
reason = []
|
2021-10-02 18:09:03 +02:00
|
|
|
if not publication.publisher.are_credentials_valid():
|
2021-08-05 00:29:50 +02:00
|
|
|
reason.append("Invalid credentials")
|
2021-10-02 18:09:03 +02:00
|
|
|
if not publication.formatter.is_event_valid(publication.event):
|
2021-08-05 00:29:50 +02:00
|
|
|
reason.append("Invalid event")
|
2021-10-02 18:09:03 +02:00
|
|
|
if not publication.formatter.is_message_valid(publication.event):
|
2021-08-05 00:29:50 +02:00
|
|
|
reason.append("Invalid message")
|
|
|
|
|
|
|
|
if len(reason) > 0:
|
2021-10-16 01:25:45 +02:00
|
|
|
errors.append(
|
|
|
|
EventPublicationReport(
|
|
|
|
status=PublicationStatus.FAILED,
|
|
|
|
reason=", ".join(reason),
|
|
|
|
publication_id=publication.id,
|
|
|
|
)
|
2021-07-07 11:45:54 +02:00
|
|
|
)
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
return errors
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
2021-10-16 01:25:45 +02:00
|
|
|
class AbstractCoordinator:
|
|
|
|
def __init__(self, message: str, platforms: List[AbstractPlatform] = None):
|
2021-10-02 18:09:03 +02:00
|
|
|
self.message = message
|
2021-10-16 01:25:45 +02:00
|
|
|
self.platforms = platforms
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
def send_to_all(self):
|
2021-10-16 01:25:45 +02:00
|
|
|
# TODO: failure to send should fail safely and write to a dedicated log
|
|
|
|
for platform in self.platforms:
|
|
|
|
platform.send(self.message)
|
|
|
|
|
|
|
|
|
|
|
|
class AbstractNotifiersCoordinator(AbstractCoordinator):
|
|
|
|
def __init__(self, message: str, notifiers: List[AbstractPlatform] = None):
|
|
|
|
platforms = notifiers or [
|
|
|
|
get_notifier_class(notifier)() for notifier in get_active_notifiers()
|
|
|
|
]
|
|
|
|
super(AbstractNotifiersCoordinator, self).__init__(message, platforms)
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PublicationFailureNotifiersCoordinator(AbstractNotifiersCoordinator):
|
2021-10-16 01:25:45 +02:00
|
|
|
def __init__(self, report: EventPublicationReport, platforms=None):
|
2021-10-02 18:09:03 +02:00
|
|
|
self.report = report
|
|
|
|
super(PublicationFailureNotifiersCoordinator, self).__init__(
|
2021-10-16 01:25:45 +02:00
|
|
|
message=report.get_failure_message(), notifiers=platforms
|
2021-08-27 23:45:24 +02:00
|
|
|
)
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
def notify_failure(self):
|
2021-10-16 01:25:45 +02:00
|
|
|
logger.info("Sending failure notifications")
|
2021-10-02 18:09:03 +02:00
|
|
|
if self.report.status == PublicationStatus.FAILED:
|
|
|
|
self.send_to_all()
|
2021-10-16 01:25:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RecapCoordinator:
|
|
|
|
def __init__(self, recap_publications: List[RecapPublication]):
|
|
|
|
self.recap_publications = recap_publications
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
reports = []
|
|
|
|
for recap_publication in self.recap_publications:
|
|
|
|
try:
|
|
|
|
|
2021-10-17 14:09:24 +02:00
|
|
|
fragments = [recap_publication.formatter.get_recap_header()]
|
2021-10-16 01:25:45 +02:00
|
|
|
for event in recap_publication.events:
|
|
|
|
fragments.append(
|
|
|
|
recap_publication.formatter.get_recap_fragment(event)
|
|
|
|
)
|
|
|
|
message = "\n\n".join(fragments)
|
|
|
|
recap_publication.publisher.send(message)
|
|
|
|
reports.append(
|
|
|
|
BasePublicationReport(
|
|
|
|
status=PublicationStatus.COMPLETED, reason=None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except PublisherError as e:
|
|
|
|
reports.append(
|
|
|
|
BasePublicationReport(
|
|
|
|
status=PublicationStatus.FAILED, reason=str(e),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return BaseCoordinatorReport(reports=reports)
|