Search in sources :

Example 1 with SchedulerRunnable

use of org.openhab.core.scheduler.SchedulerRunnable in project openhab-core by openhab.

the class CronSchedulerImpl method schedule.

@Override
public ScheduledCompletableFuture<@Nullable Void> schedule(CronJob job, Map<String, Object> config, String cronExpression) {
    final CronAdjuster cronAdjuster = new CronAdjuster(cronExpression);
    final SchedulerRunnable runnable = () -> job.run(config);
    if (cronAdjuster.isReboot()) {
        return scheduler.at(runnable, Instant.ofEpochMilli(1));
    } else {
        return scheduler.schedule(runnable, cronAdjuster);
    }
}
Also used : CronAdjuster(org.openhab.core.scheduler.CronAdjuster) SchedulerRunnable(org.openhab.core.scheduler.SchedulerRunnable)

Example 2 with SchedulerRunnable

use of org.openhab.core.scheduler.SchedulerRunnable in project openhab-core by openhab.

the class SchedulerImplTest method testScheduleException.

@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
public void testScheduleException() throws InterruptedException {
    Semaphore s = new Semaphore(0);
    TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter();
    SchedulerRunnable runnable = () -> {
        // Pass a exception not very likely thrown by the scheduler it self to avoid missing real exceptions.
        throw new FileNotFoundException("testBeforeTimeoutException");
    };
    ScheduledCompletableFuture<@Nullable Void> schedule = scheduler.schedule(runnable, temporalAdjuster);
    schedule.getPromise().exceptionally(e -> {
        if (e instanceof FileNotFoundException) {
            s.release();
        }
        return null;
    });
    s.acquire(1);
    // wait a little longer to see if not more are scheduled.
    Thread.sleep(300);
    assertEquals(0, s.availablePermits(), "Scheduler should not have released more after cancel");
    assertEquals(0, temporalAdjuster.getCount(), "Scheduler should have run 0 time");
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Semaphore(java.util.concurrent.Semaphore) SchedulerRunnable(org.openhab.core.scheduler.SchedulerRunnable) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

SchedulerRunnable (org.openhab.core.scheduler.SchedulerRunnable)2 FileNotFoundException (java.io.FileNotFoundException)1 Semaphore (java.util.concurrent.Semaphore)1 Test (org.junit.jupiter.api.Test)1 Timeout (org.junit.jupiter.api.Timeout)1 CronAdjuster (org.openhab.core.scheduler.CronAdjuster)1