2022-01-04 21:35:43 +01:00
|
|
|
import logging
|
2022-03-06 10:41:02 +01:00
|
|
|
from typing import Optional
|
2022-01-04 21:35:43 +01:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from mobilizon_reshare.publishers.coordinator import (
|
|
|
|
PublisherCoordinator,
|
2022-03-06 10:41:02 +01:00
|
|
|
PublisherCoordinatorReport,
|
|
|
|
PublicationFailureLoggerCoordinator,
|
2022-01-04 21:35:43 +01:00
|
|
|
)
|
2022-02-09 00:54:56 +01:00
|
|
|
from mobilizon_reshare.storage.query.exceptions import EventNotFound
|
2022-02-14 21:10:27 +01:00
|
|
|
from mobilizon_reshare.storage.query.read import (
|
|
|
|
get_failed_publications_for_event,
|
|
|
|
get_publication,
|
|
|
|
)
|
2022-01-04 21:35:43 +01:00
|
|
|
from mobilizon_reshare.storage.query.write import save_publication_report
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def retry_event_publications(event_id):
|
|
|
|
|
|
|
|
failed_publications = await get_failed_publications_for_event(event_id)
|
|
|
|
if not failed_publications:
|
|
|
|
logger.info("No failed publications found.")
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.info(f"Found {len(failed_publications)} publications.")
|
|
|
|
return PublisherCoordinator(failed_publications).run()
|
|
|
|
|
|
|
|
|
2022-03-06 10:41:02 +01:00
|
|
|
async def retry_publication(publication_id) -> Optional[PublisherCoordinatorReport]:
|
2022-02-14 21:10:27 +01:00
|
|
|
# TODO test this function
|
|
|
|
publication = await get_publication(publication_id)
|
|
|
|
if not publication:
|
|
|
|
logger.info(f"Publication {publication_id} not found.")
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.info(f"Publication {publication_id} found.")
|
2022-03-06 10:41:02 +01:00
|
|
|
reports = PublisherCoordinator([publication]).run()
|
|
|
|
|
|
|
|
await save_publication_report(reports)
|
|
|
|
|
|
|
|
for report in reports.reports:
|
|
|
|
if not report.succesful:
|
|
|
|
PublicationFailureLoggerCoordinator(report,).notify_failure()
|
2022-02-14 21:10:27 +01:00
|
|
|
|
|
|
|
|
2022-03-06 10:41:02 +01:00
|
|
|
async def retry_event(
|
|
|
|
mobilizon_event_id: UUID = None,
|
|
|
|
) -> Optional[PublisherCoordinatorReport]:
|
2022-01-04 21:35:43 +01:00
|
|
|
if mobilizon_event_id is None:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Autonomous retry not implemented yet, please specify an event_id"
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
reports = await retry_event_publications(mobilizon_event_id)
|
|
|
|
except EventNotFound as e:
|
|
|
|
logger.debug(e, exc_info=True)
|
|
|
|
logger.error(f"Event with id {mobilizon_event_id} not found")
|
|
|
|
return
|
|
|
|
|
|
|
|
if not reports:
|
|
|
|
return
|
|
|
|
await save_publication_report(reports)
|
|
|
|
for report in reports.reports:
|
|
|
|
if not report.succesful:
|
2022-03-06 10:41:02 +01:00
|
|
|
PublicationFailureLoggerCoordinator(report,).notify_failure()
|