diff --git a/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java b/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java index 30a7d8c6fc..01431f6e31 100644 --- a/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java +++ b/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java @@ -177,61 +177,83 @@ private void executeInject(ExecutableInject executableInject) { finalStatus.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); injectStatusRepository.save(finalStatus); } else { - inject.getInjectorContract().ifPresent(injectorContract -> { + setInjectStatusAndExecuteInject(executableInject, inject); + } + } - if (!inject.isReady()) { + private void setInjectStatusAndExecuteInject(ExecutableInject executableInject, Inject inject) { + inject.getInjectorContract().ifPresentOrElse(injectorContract -> { + if (!inject.isReady()) { + // Status + if (inject.getStatus().isEmpty()) { + InjectStatus status = new InjectStatus(); + status.getTraces().add(InjectStatusExecution.traceError("The inject is not ready to be executed (missing mandatory fields)")); + status.setName(ExecutionStatus.ERROR); + status.setTrackingSentDate(Instant.now()); + status.setInject(inject); + status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); + injectStatusRepository.save(status); + } else { + InjectStatus status = inject.getStatus().get(); + status.getTraces().add(InjectStatusExecution.traceError("The inject is not ready to be executed (missing mandatory fields)")); + status.setName(ExecutionStatus.ERROR); + status.setTrackingSentDate(Instant.now()); + status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); + injectStatusRepository.save(status); + } + return; + } + + Injector externalInjector = injectorRepository.findByType(injectorContract.getInjector().getType()).orElseThrow(); + LOGGER.log(Level.INFO, "Executing inject " + inject.getInject().getTitle()); + // Executor logics + ExecutableInject newExecutableInject = executableInject; + if (Boolean.TRUE.equals(injectorContract.getNeedsExecutor())) { + try { // Status if (inject.getStatus().isEmpty()) { InjectStatus status = new InjectStatus(); - status.getTraces().add(InjectStatusExecution.traceError("The inject is not ready to be executed (missing mandatory fields)")); - status.setName(ExecutionStatus.ERROR); + status.setName(ExecutionStatus.EXECUTING); status.setTrackingSentDate(Instant.now()); status.setInject(inject); status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); injectStatusRepository.save(status); } else { InjectStatus status = inject.getStatus().get(); - status.getTraces().add(InjectStatusExecution.traceError("The inject is not ready to be executed (missing mandatory fields)")); - status.setName(ExecutionStatus.ERROR); + status.setName(ExecutionStatus.EXECUTING); status.setTrackingSentDate(Instant.now()); status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); injectStatusRepository.save(status); } - return; + newExecutableInject = this.executionExecutorService.launchExecutorContext(executableInject, inject); + } catch (InterruptedException e) { + throw new RuntimeException(e); } + } + if (externalInjector.isExternal()) { + executeExternal(newExecutableInject); + } else { + executeInternal(newExecutableInject); + } + }, () -> setInjectStatusWhenNoInjectorContractExists(inject)); + } - Injector externalInjector = injectorRepository.findByType(injectorContract.getInjector().getType()).orElseThrow(); - LOGGER.log(Level.INFO, "Executing inject " + inject.getInject().getTitle()); - // Executor logics - ExecutableInject newExecutableInject = executableInject; - if (Boolean.TRUE.equals(injectorContract.getNeedsExecutor())) { - try { - // Status - if (inject.getStatus().isEmpty()) { - InjectStatus status = new InjectStatus(); - status.setName(ExecutionStatus.EXECUTING); - status.setTrackingSentDate(Instant.now()); - status.setInject(inject); - status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); - } else { - InjectStatus status = inject.getStatus().get(); - status.setName(ExecutionStatus.EXECUTING); - status.setTrackingSentDate(Instant.now()); - status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); - } - newExecutableInject = this.executionExecutorService.launchExecutorContext(executableInject, inject); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - if (externalInjector.isExternal()) { - executeExternal(newExecutableInject); - } else { - executeInternal(newExecutableInject); - } - }); + private void setInjectStatusWhenNoInjectorContractExists(Inject inject) { + if (inject.getStatus().isEmpty()) { + InjectStatus status = new InjectStatus(); + status.getTraces().add(InjectStatusExecution.traceError("Inject does not have a contract")); + status.setName(ExecutionStatus.ERROR); + status.setTrackingSentDate(Instant.now()); + status.setInject(inject); + status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); + injectStatusRepository.save(status); + } else { + InjectStatus status = inject.getStatus().get(); + status.getTraces().add(InjectStatusExecution.traceError("Inject does not have a contract")); + status.setName(ExecutionStatus.ERROR); + status.setTrackingSentDate(Instant.now()); + status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); + injectStatusRepository.save(status); } }