from typing import Dict, Any


from app.main.engine.service.Process import Process
from app.main.engine.enum.StatusEnum import StatusEnum
from app.main.engine.enum.DescResponseEnum import DescResponseEnum


class EngineService:

    def __init__(self, app, descriptor: Dict[str, Any], executions: int, max_executions: int) -> None:
        self.app = app
        self.descriptor = descriptor
        self.executions = executions
        self.max_executions = max_executions

    def run(self):
        response = {}
        try:
            self.app.logger.info(f"Ejecuciones simultáneas actualmente: {self.executions}")
            if self.executions > self.max_executions:
                self.app.logger.info(f"Máxima de ejecuciones en paralelo alcanzado. Máximo: {self.max_executions}")
                response = {'status': StatusEnum.ERROR.name,
                            'message': DescResponseEnum.MAX_EXECUTION_ERROR.value}
            else:
                process = Process(self.app, self.descriptor)
                response = process.run()
        except Exception as e:
            self.app.logger.error(f"Error mandando el proceso a engine. {e}")
        finally:
            return response
