Stop automatic Job Execution by DUEDATE after Camunda restart

Hi, I have this simple bpmn for a proof of concept

My goals are:

  • Be able to dynamic schedule a Process Instace. (DONE)
  • I case of complete shutdown of Camunda, it cannot auto start (default behavior) because this can create some issues. (This topic here)

This is the code that starts the process

@PostMapping(
            value = "/agenda"
    )
    public String agendaTask1MinAFrente(){
        final Date date = DateBuilder.futureDate(10, DateBuilder.IntervalUnit.SECOND);
        final ProcessInstance agendamentoTaskCamunda = runtimeService.createProcessInstanceByKey("testa-agd")
                .setVariable("agendamento", date)
                .execute();
        logger.info("ID: {} DATA PREVISTA: {}", agendamentoTaskCamunda.getId(), date);
        return agendamentoTaskCamunda.getId();
    }

The problem is, if I shutdown for testing purpose, it gets automatically run after startup

After startup, time 17:15:44, it ran a Job scheduled for 17:14:12.

2024-01-22T17:15:44.196-03:00 INFO 28516 — [aTaskExecutor-1] c.e.workflow.tasks.ExecutaPrimeiraTask : ID: c8d8f82e-b962-11ee-89dc-00090ffe0001, Iniciando task agendada Mon Jan 22 17:14:12 BRT 2024

I know the default behavior is any kind of DUE.

" * it is due, meaning that the value in the DUEDATE_ column is in the past"

But is there a way to change that like “after 1 hour of DUEDATE do not run the job” ?

Hi @g0ld7911,

it’s an uncommon use case to react on delayed timers due to a stopped process engine.

You can either adjust the dueDate and set it to a date in the future. For the stopped engine, you have to update the due date in the database.

Another option could be to add an interrupting event subprocess with another timer, that stops overdue process instances at all: Event Subprocess | docs.camunda.org

Hope this helps, Ingo

1 Like

Hi, I found another way that can give me some free space to work on

@Service("Avoid")
public class Avoid implements JavaDelegate {

    private final Logger logger = getLogger(this.getClass());
    private final RuntimeService runtimeService;

    public Avoid(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        final Date agendamento = (Date) execution.getVariable("agendamento");
        final LocalDateTime agendamentoLocalDateTime = LocalDateTime.ofInstant(agendamento.toInstant(), ZoneId.systemDefault());
        final LocalDateTime timeNow = LocalDateTime.now();
        if (agendamentoLocalDateTime.plusMinutes(1).isAfter(timeNow) ){
            logger.info("Task can continue {}", timeNow);
        } else {
            logger.info("Task passed 1 minute Due Date {}. Killing", timeNow);
            runtimeService.deleteProcessInstanceIfExists(execution.getProcessInstanceId(), "Stop a força", false, false, false, false);
        }
    }
}
2024-01-23T09:18:10.244-03:00  INFO 18968 --- [aTaskExecutor-1] com.example.workflow.tasks.Avoid         : Task passed 1 minute Due Date 2024-01-23T09:18:10.244298700. Killing

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.