2022-03-22 21:16:34 +01:00
|
|
|
import logging.config
|
2022-07-12 07:39:56 +02:00
|
|
|
from typing import Optional, Iterator
|
2022-03-22 21:16:34 +01:00
|
|
|
|
2022-07-12 07:39:56 +02:00
|
|
|
from mobilizon_reshare.config.command import CommandConfig
|
2022-03-22 21:16:34 +01:00
|
|
|
from mobilizon_reshare.event.event import MobilizonEvent
|
|
|
|
from mobilizon_reshare.event.event_selection_strategies import select_event_to_publish
|
2022-05-17 23:14:52 +02:00
|
|
|
from mobilizon_reshare.publishers import get_active_publishers
|
|
|
|
from mobilizon_reshare.publishers.abstract import EventPublication
|
2022-03-22 21:16:34 +01:00
|
|
|
from mobilizon_reshare.publishers.coordinator import (
|
|
|
|
PublicationFailureNotifiersCoordinator,
|
|
|
|
PublisherCoordinatorReport,
|
|
|
|
)
|
|
|
|
from mobilizon_reshare.publishers.coordinator import PublisherCoordinator
|
|
|
|
from mobilizon_reshare.storage.query.read import (
|
|
|
|
get_published_events,
|
|
|
|
build_publications,
|
|
|
|
events_without_publications,
|
|
|
|
)
|
|
|
|
from mobilizon_reshare.storage.query.write import save_publication_report
|
2022-07-12 07:39:56 +02:00
|
|
|
from mobilizon_reshare.publishers.coordinator import DryRunPublisherCoordinator
|
2022-03-22 21:16:34 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-05-17 23:14:52 +02:00
|
|
|
async def publish_publications(
|
|
|
|
publications: list[EventPublication],
|
|
|
|
) -> PublisherCoordinatorReport:
|
|
|
|
report = PublisherCoordinator(publications).run()
|
|
|
|
|
|
|
|
await save_publication_report(report)
|
|
|
|
for publication_report in report.reports:
|
|
|
|
if not publication_report.succesful:
|
2022-07-12 07:39:56 +02:00
|
|
|
PublicationFailureNotifiersCoordinator(publication_report,).notify_failure()
|
2022-05-17 23:14:52 +02:00
|
|
|
|
|
|
|
return report
|
|
|
|
|
|
|
|
|
2022-07-12 07:39:56 +02:00
|
|
|
def perform_dry_run(publications: list[EventPublication]):
|
|
|
|
return DryRunPublisherCoordinator(publications).run()
|
|
|
|
|
|
|
|
|
2022-05-17 23:14:52 +02:00
|
|
|
async def publish_event(
|
2022-07-12 07:39:56 +02:00
|
|
|
event: MobilizonEvent,
|
|
|
|
command_config: CommandConfig,
|
|
|
|
publishers: Optional[Iterator[str]] = None,
|
2022-05-17 23:14:52 +02:00
|
|
|
) -> PublisherCoordinatorReport:
|
|
|
|
logger.info(f"Event to publish found: {event.name}")
|
|
|
|
|
|
|
|
if not (publishers and all(publishers)):
|
|
|
|
publishers = get_active_publishers()
|
|
|
|
|
|
|
|
publications = await build_publications(event, publishers)
|
2022-07-12 07:39:56 +02:00
|
|
|
if command_config.dry_run:
|
|
|
|
logger.info("Executing in dry run mode. No event is going to be published.")
|
|
|
|
return perform_dry_run(publications)
|
|
|
|
else:
|
|
|
|
return await publish_publications(publications)
|
2022-05-17 23:14:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def select_and_publish(
|
2022-07-12 07:39:56 +02:00
|
|
|
command_config: CommandConfig,
|
2022-05-17 23:14:52 +02:00
|
|
|
unpublished_events: Optional[list[MobilizonEvent]] = None,
|
2022-03-22 21:16:34 +01:00
|
|
|
) -> Optional[PublisherCoordinatorReport]:
|
|
|
|
"""
|
|
|
|
STUB
|
|
|
|
:return:
|
|
|
|
"""
|
2022-05-17 23:14:52 +02:00
|
|
|
if unpublished_events is None:
|
|
|
|
unpublished_events = await events_without_publications()
|
2022-03-22 21:16:34 +01:00
|
|
|
|
|
|
|
event = select_event_to_publish(
|
2022-07-12 07:39:56 +02:00
|
|
|
list(await get_published_events()), unpublished_events,
|
2022-03-22 21:16:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if event:
|
2022-07-12 07:39:56 +02:00
|
|
|
return await publish_event(event, command_config)
|
2022-03-22 21:16:34 +01:00
|
|
|
else:
|
|
|
|
logger.info("No event to publish found")
|