package eu.ecb.mora.scheduler; import org.camunda.bpm.engine.delegate.BpmnError; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import eu.ecb.mora.exception.LockedException; import eu.ecb.mora.scheduler.feign.FeignClientConfiguration; import eu.ecb.mora.scheduler.restclient.ExecuteSchedulerDispatcherClient; @Component("executeDispatcher") public class ExecuteSchedulerDispatcher implements JavaDelegate { private final Logger LOGGER = LoggerFactory.getLogger(ExecuteSchedulerDispatcher.class.getName()); @Autowired private FeignClientConfiguration feignConfiguration; @Override public void execute(DelegateExecution execution) throws Exception { String endpoint = (String) execution.getVariable("server"); Long taskId = (Long) execution.getVariable("taskId"); ExecuteSchedulerDispatcherClient executeDispatcher = feignConfiguration.execute(endpoint); LOGGER.debug("entering execute:" + taskId); ResponseEntity response = null; try { response = executeDispatcher.execute(taskId); if (response == null) { execution.setVariable("response", ""); } else { execution.setVariable("response", response.getStatusCode().value()); } } catch (LockedException e) { LOGGER.error("Execution rejected, Assessment locked (" + taskId + ")"); execution.setVariable("error_message", e.getMessage()); Integer counter = (Integer) execution.getVariable("errorCounter"); if (counter == null) { counter = new Integer(0); } counter = counter + 1; if (counter.intValue() >= 3) { LOGGER.debug("exit execute:" + taskId + "/" + counter + "/FatalError"); execution.setVariable("errorCounter", new Integer(0)); throw new BpmnError("FatalError"); } execution.setVariable("errorCounter", counter); LOGGER.debug("exit execute:" + taskId + "/" + counter + "/ProcessLocked"); throw e; } catch (Exception e) { execution.setVariable("error_message", e.getMessage()); LOGGER.error("Execution rejected, Fatal error (" + taskId + ")"); throw new BpmnError("FatalError"); } } }