Search in sources :

Example 6 with ScheduledTask

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

the class SchedulingTest method testRunning.

@Test
public void testRunning() {
    Scheduling scheduling = new Scheduling() {

        {
            timeSource = new TimeSource() {

                @Override
                public Date currentTimestamp() {
                    return new Date();
                }

                @Override
                public long currentTimeMillis() {
                    return System.currentTimeMillis();
                }
            };
        }

        @Override
        protected TimeZone getCurrentTimeZone() {
            return TimeZone.getTimeZone("GMT-0");
        }
    };
    ScheduledTask scheduledTask = new ScheduledTask();
    assertFalse(scheduling.setRunning(scheduledTask, true));
    assertTrue(scheduling.setRunning(scheduledTask, true));
}
Also used : ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask) TimeSource(com.haulmont.cuba.core.global.TimeSource) Date(java.util.Date) Test(org.junit.Test)

Example 7 with ScheduledTask

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

the class ScheduledTaskBrowser method init.

@Override
public void init(Map<String, Object> params) {
    ComponentsHelper.createActions(tasksTable);
    final Action editAction = tasksTable.getActionNN(EditAction.ACTION_ID);
    editAction.setEnabled(false);
    final Action removeAction = tasksTable.getActionNN(RemoveAction.ACTION_ID);
    removeAction.setEnabled(false);
    activateBtn.setAction(new AbstractAction("activate") {

        @Override
        public void actionPerform(Component component) {
            Set<ScheduledTask> tasks = tasksTable.getSelected();
            service.setActive(tasks, !BooleanUtils.isTrue(tasks.iterator().next().getActive()));
            tasksDs.refresh();
        }
    });
    activateBtn.setEnabled(false);
    final ShowExecutionsAction showExecutionsAction = new ShowExecutionsAction();
    showExecutionsAction.setEnabled(false);
    tasksTable.addAction(showExecutionsAction);
    tasksDs.addItemChangeListener(e -> {
        ScheduledTask singleSelected = tasksTable.getSingleSelected();
        Set<ScheduledTask> selected = tasksTable.getSelected();
        boolean isSingleSelected = selected.size() == 1;
        boolean enableEdit = isSingleSelected && !BooleanUtils.isTrue(singleSelected.getActive());
        editAction.setEnabled(enableEdit);
        removeAction.setEnabled(checkAllTasksIsNotActive(selected));
        activateBtn.setEnabled(checkAllTasksHaveSameStatus(selected));
        if (singleSelected == null) {
            activateBtn.setCaption(getMessage("activate"));
        } else {
            activateBtn.setCaption(BooleanUtils.isTrue(singleSelected.getActive()) ? getMessage("deactivate") : getMessage("activate"));
        }
        showExecutionsAction.setEnabled(isSingleSelected);
    });
}
Also used : EditAction(com.haulmont.cuba.gui.components.actions.EditAction) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) RemoveAction(com.haulmont.cuba.gui.components.actions.RemoveAction) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Example 8 with ScheduledTask

use of com.haulmont.cuba.core.entity.ScheduledTask 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 9 with ScheduledTask

use of com.haulmont.cuba.core.entity.ScheduledTask 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)

Example 10 with ScheduledTask

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

the class Scheduling method getActiveTasks.

@Override
public List<ScheduledTask> getActiveTasks() {
    Coordinator.Context context = coordinator.begin();
    coordinator.end(context);
    List<ScheduledTask> tasks = context.getTasks();
    for (ScheduledTask task : tasks) {
        if (!BooleanUtils.isTrue(task.getSingleton())) {
            Long time = lastStartCache.get(task);
            if (time != null)
                task.setLastStartTime(new Date(time));
        }
    }
    return tasks;
}
Also used : ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Aggregations

ScheduledTask (com.haulmont.cuba.core.entity.ScheduledTask)12 EntityManager (com.haulmont.cuba.core.EntityManager)3 Transaction (com.haulmont.cuba.core.Transaction)3 ScheduledExecution (com.haulmont.cuba.core.entity.ScheduledExecution)2 User (com.haulmont.cuba.security.entity.User)2 Date (java.util.Date)2 Test (org.junit.Test)2 TimeSource (com.haulmont.cuba.core.global.TimeSource)1 EditAction (com.haulmont.cuba.gui.components.actions.EditAction)1 ItemTrackingAction (com.haulmont.cuba.gui.components.actions.ItemTrackingAction)1 RemoveAction (com.haulmont.cuba.gui.components.actions.RemoveAction)1 Group (com.haulmont.cuba.security.entity.Group)1 SimpleDateFormat (java.text.SimpleDateFormat)1 StopWatch (org.perf4j.StopWatch)1 Slf4JStopWatch (org.perf4j.slf4j.Slf4JStopWatch)1