Search in sources :

Example 1 with ScheduledExecution

use of com.haulmont.cuba.core.entity.ScheduledExecution in project cuba by cuba-platform.

the class RunnerBean method registerExecutionStart.

protected ScheduledExecution registerExecutionStart(ScheduledTask task, long now) {
    if (!BooleanUtils.isTrue(task.getLogStart()) && !BooleanUtils.isTrue(task.getSingleton()) && task.getSchedulingType() != SchedulingType.FIXED_DELAY)
        return null;
    log.trace("{}: registering execution start", task);
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        ScheduledExecution execution = metadata.create(ScheduledExecution.class);
        execution.setTask(em.getReference(ScheduledTask.class, task.getId()));
        execution.setStartTime(new Date(now));
        execution.setServer(serverInfo.getServerId());
        em.persist(execution);
        tx.commit();
        return execution;
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ScheduledExecution(com.haulmont.cuba.core.entity.ScheduledExecution) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Example 2 with ScheduledExecution

use of com.haulmont.cuba.core.entity.ScheduledExecution in project cuba by cuba-platform.

the class RunnerBean method runTask.

@Override
public void runTask(ScheduledTask task, final long now, @Nullable final UserSession userSession) {
    // It's better not to pass an entity instance in managed state to another thread
    final ScheduledTask taskCopy = metadata.getTools().copy(task);
    executorService.submit(() -> {
        log.debug("{}: running", taskCopy);
        try {
            boolean runConcurrent = scheduling.setRunning(taskCopy, true);
            if (!runConcurrent) {
                try {
                    setSecurityContext(taskCopy, userSession);
                    ScheduledExecution execution = registerExecutionStart(taskCopy, now);
                    statisticsCounter.incCubaScheduledTasksCount();
                    try {
                        Object result = executeTask(taskCopy);
                        registerExecutionFinish(taskCopy, execution, result);
                    } catch (Throwable throwable) {
                        registerExecutionFinish(taskCopy, execution, throwable);
                        throw throwable;
                    }
                } finally {
                    scheduling.setRunning(taskCopy, false);
                    scheduling.setFinished(task);
                }
            } else {
                log.info("Detected concurrent task execution: {}, skip it", taskCopy);
            }
        } catch (Throwable throwable) {
            log.error("Error running {}", taskCopy, throwable);
        }
    });
}
Also used : ScheduledExecution(com.haulmont.cuba.core.entity.ScheduledExecution) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Aggregations

ScheduledExecution (com.haulmont.cuba.core.entity.ScheduledExecution)2 ScheduledTask (com.haulmont.cuba.core.entity.ScheduledTask)2 EntityManager (com.haulmont.cuba.core.EntityManager)1 Transaction (com.haulmont.cuba.core.Transaction)1