Skip to content

Commit

Permalink
Merge pull request #24605 from knutwannheden/applicationnotrunning-sk…
Browse files Browse the repository at this point in the history
…ippredicate

Provide a new `ApplicationNotRunning` predicate for `@Scheduled`
  • Loading branch information
mkouba authored Apr 5, 2022
2 parents 6aeb031 + 785e88a commit 85e1f13
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class SchedulerProcessor {
@BuildStep
void beans(Capabilities capabilities, BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
if (capabilities.isMissing(Capability.QUARTZ)) {
additionalBeans.produce(new AdditionalBeanBuildItem(SimpleScheduler.class));
additionalBeans.produce(new AdditionalBeanBuildItem(SimpleScheduler.class, Scheduled.ApplicationNotRunning.class));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.scheduler.test;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.enterprise.event.Observes;
import javax.interceptor.Interceptor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Priority;
import io.quarkus.runtime.StartupEvent;
import io.quarkus.scheduler.FailedExecution;
import io.quarkus.scheduler.Scheduled;
import io.quarkus.scheduler.SuccessfulExecution;
import io.quarkus.test.QuarkusUnitTest;

public class ApplicationNotRunningPredicateTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest().withApplicationRoot((jar) -> jar.addClasses(Jobs.class));

static final CountDownLatch SUCCESS_LATCH = new CountDownLatch(1);
static volatile FailedExecution failedExecution;

@Test
public void testTriggerErrorStatus() throws InterruptedException {
assertTrue(SUCCESS_LATCH.await(5, TimeUnit.SECONDS));
assertNull(failedExecution);
}

void observeSuccessfulExecution(@Observes SuccessfulExecution successfulExecution) {
SUCCESS_LATCH.countDown();
}

void observeFailedExecution(@Observes FailedExecution failedExecution) {
ApplicationNotRunningPredicateTest.failedExecution = failedExecution;
}

static class Jobs {

volatile boolean preStart;

void started(@Observes @Priority(Interceptor.Priority.PLATFORM_BEFORE) StartupEvent event) {
preStart = true;
}

@Scheduled(every = "0.2s", skipExecutionIf = Scheduled.ApplicationNotRunning.class)
void scheduleAfterStarted() {
if (!preStart) {
throw new IllegalStateException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
import java.util.concurrent.TimeUnit;

import javax.enterprise.context.Dependent;
import javax.enterprise.event.Observes;
import javax.inject.Singleton;

import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import io.quarkus.scheduler.Scheduled.Schedules;

/**
Expand Down Expand Up @@ -206,4 +210,27 @@ public boolean test(ScheduledExecution execution) {

}

/**
* Execution is skipped if the application is not running (either not started or already shutdown).
*/
@Singleton
class ApplicationNotRunning implements SkipPredicate {

private volatile boolean running;

void started(@Observes StartupEvent event) {
this.running = true;
}

void shutdown(@Observes ShutdownEvent event) {
this.running = false;
}

@Override
public boolean test(ScheduledExecution execution) {
return !running;
}

}

}

0 comments on commit 85e1f13

Please sign in to comment.