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));
}
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);
});
}
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();
}
}
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);
}
});
}
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;
}
Aggregations