"""Interface for TIF and TOF."""importloggingimportsysfromabcimportABC,abstractmethodfrompoiesis.core.adaptors.message_broker.redis_adaptorimportRedisMessageBrokerfrompoiesis.core.ports.message_brokerimportMessage,MessageStatuslogger=logging.getLogger(__name__)
[docs]classFiler(ABC):"""Interface for TIF and TOF. Attributes: message_broker: Message broker name: Name of the filer """def__init__(self)->None:"""Initialize the filer. Attributes: message_broker: Message broker name: Name of the filer """self.message_broker=RedisMessageBroker()
[docs]asyncdefexecute(self):"""Execute the filer. This will file the file and send a message to TORC via the message broker. """try:logger.info("Starting file operation")awaitself.file()exceptExceptionase:logger.error(f"File operation failed: {e}")self.message(Message(status=MessageStatus.ERROR,message=f"Filer failed: {e}"))sys.exit(1)# TODO: We should update the task status, tell torclogger.info("File operation completed successfully")self.message(Message("Filer completed."))
[docs]@abstractmethodasyncdeffile(self):"""Filing logic, upload or download. If TIF encounters any error, it will send a message to TORC and break. """pass
@property@abstractmethoddefname(self)->str:"""Name of the filer."""pass
[docs]defmessage(self,message:Message):"""Message logic, send a message to TORC."""# TODO: Change this to id, it shouldn't be nameifnothasattr(self,"name"):logger.error("The name attribute is not set")raiseAttributeError("The name attribute is not set.")logger.info(f"Sending message to TORC: {message}")self.message_broker.publish(self.name,message)