Search in sources :

Example 1 with ScheduledTask

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

the class Scheduling method processScheduledTasks.

@Override
public void processScheduledTasks(boolean onlyIfActive) {
    if (onlyIfActive && !isActive())
        return;
    log.debug("Processing scheduled tasks");
    if (schedulingStartTime == 0)
        schedulingStartTime = timeSource.currentTimeMillis();
    authentication.begin();
    try {
        StopWatch sw = new Slf4JStopWatch("Scheduling.processTasks");
        Coordinator.Context context = coordinator.begin();
        try {
            for (ScheduledTask task : context.getTasks()) {
                processTask(task);
            }
        } finally {
            coordinator.end(context);
        }
        sw.stop();
    } finally {
        authentication.end();
    }
}
Also used : Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) StopWatch(org.perf4j.StopWatch)

Example 2 with ScheduledTask

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

the class SchedulingServiceBean method setActive.

@Override
public void setActive(Set<ScheduledTask> tasks, boolean active) {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        for (ScheduledTask task : tasks) {
            ScheduledTask t = em.find(ScheduledTask.class, task.getId());
            t.setActive(active);
        }
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Example 3 with ScheduledTask

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

the class SchedulingServiceBean method setActive.

@Override
public void setActive(ScheduledTask task, boolean active) {
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        ScheduledTask t = em.find(ScheduledTask.class, task.getId());
        t.setActive(active);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Example 4 with ScheduledTask

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

the class PersistenceAttributeLoadedCheckTest method setUp.

@Before
public void setUp() throws Exception {
    dataManager = AppBeans.get(DataManager.class);
    persistence = AppBeans.get(Persistence.class);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        ScheduledTask task = new ScheduledTask();
        task.setBeanName("BeanName");
        task.setMethodName("MethodName");
        taskId = task.getId();
        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("login" + userId);
        user.setPassword("000");
        user.setGroup(em.find(Group.class, groupId));
        em.persist(user);
        em.persist(task);
        em.persist(user);
        tx.commit();
    }
    taskView = new View(ScheduledTask.class, true).addProperty("beanName");
    userView = new View(User.class, true).addProperty("login").addProperty("loginLowerCase").addProperty("name").addProperty("password").addProperty("group", new View(Group.class).addProperty("name")).addProperty("userRoles", new View(UserRole.class));
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask)

Example 5 with ScheduledTask

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

the class SchedulingTest method testCurrentStart.

@Test
public void testCurrentStart() throws Exception {
    Scheduling scheduling = new Scheduling() {

        @Override
        protected TimeZone getCurrentTimeZone() {
            return TimeZone.getTimeZone("GMT-0");
        }
    };
    ScheduledTask scheduledTask = new ScheduledTask();
    scheduledTask.setSchedulingType(SchedulingType.CRON);
    scheduledTask.setCron("*/5 * * * * *");
    // scheduler has failed couple of runs and now we should run it
    long currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-11-13 15:29:00").getTime(), date("2013-11-13 15:30:00").getTime(), 10000l);
    assertEquals(date("2013-11-13 15:29:55"), new Date(currentStart));
    // last run was year ago, so now-frame should be considered
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2012-11-13 15:29:00").getTime(), date("2013-11-13 15:30:00").getTime(), 10000l);
    assertEquals(date("2013-11-13 15:29:55"), new Date(currentStart));
    // last run was very close to now, last start date should be considered
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-11-13 15:29:59").getTime(), date("2013-11-13 15:30:01").getTime(), 10000l);
    assertEquals(date("2013-11-13 15:30:00"), new Date(currentStart));
    scheduledTask.setCron("0 0 0 * * FRI");
    // task should run in next friday
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-11-08 01:01:01").getTime(), date("2013-11-13 15:30:00").getTime(), 10000l);
    assertEquals(date("2013-11-15 00:00:00"), new Date(currentStart));
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-11-08 01:01:01").getTime(), date("2013-11-08 01:01:02").getTime(), 600000l);
    assertEquals(date("2013-11-15 00:00:00"), new Date(currentStart));
    // task is late but matches frame
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-11-07 23:59:59").getTime(), date("2013-11-08 00:01:00").getTime(), 600000l);
    assertEquals(date("2013-11-8 00:00:00"), new Date(currentStart));
    // task is late and does not match frame
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-11-07 23:59:59").getTime(), date("2013-11-08 00:11:00").getTime(), 600000l);
    assertEquals(date("2013-11-15 00:00:00"), new Date(currentStart));
    scheduledTask.setCron("0 59 1 * * *");
    // time shift forward
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-10-26 1:59:59").getTime(), date("2013-10-27 00:00:00").getTime(), 600000l);
    assertEquals(date("2013-10-27 01:59:00"), new Date(currentStart));
    // time shift backward
    currentStart = scheduling.calculateNextCronDate(scheduledTask, date("2013-03-30 1:59:00").getTime(), date("2013-03-31 00:00:00").getTime(), 600000l);
    assertEquals(date("2013-03-31 01:59:00"), new Date(currentStart));
}
Also used : ScheduledTask(com.haulmont.cuba.core.entity.ScheduledTask) Date(java.util.Date) Test(org.junit.Test)

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