import logging
from typing import Any, List, Optional

from messagebus import Event, Message, MessageBus
from messageprocessor import DefaultMessageProcessorExecutor, MessageProcessor, MessageProcessorCallback

from rediseventlock.exceptions import LockedEventException


class EventLockMessageProcessorExecutor(DefaultMessageProcessorExecutor):
    def __init__(self, event_processor: MessageProcessor, callbacks: Optional[List[MessageProcessorCallback]] = None):
        super().__init__(event_processor=event_processor, callbacks=callbacks)
        self.logger = logging.getLogger()

    def execute_processor(self, message_bus: MessageBus, message: Optional[Message], event: Optional[Event], data: Any):
        event_name = self.event_processor.get_processor_name()
        try:
            super().execute_processor(message_bus=message_bus, message=message, event=event, data=data)
        except LockedEventException as ex:
            self.logger.debug(f"Skipping event locked {event_name}", exc_info=ex)
            return
