Skip to content

Commit

Permalink
Scheduler: register ApplicationNotRunning as bean even if quartz is used
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba authored and holly-cummins committed Feb 8, 2024
1 parent a2bfa38 commit 34773da
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.quartz.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 jakarta.enterprise.event.Observes;

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

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 started;

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

@Scheduled(every = "0.2s", skipExecutionIf = Scheduled.ApplicationNotRunning.class)
void scheduleAfterStarted() {
if (!started) {
throw new IllegalStateException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ public class SchedulerProcessor {

@BuildStep
void beans(Capabilities capabilities, BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(new AdditionalBeanBuildItem(Scheduled.ApplicationNotRunning.class));
if (capabilities.isMissing(Capability.QUARTZ)) {
additionalBeans.produce(new AdditionalBeanBuildItem(SimpleScheduler.class, Scheduled.ApplicationNotRunning.class));
additionalBeans.produce(new AdditionalBeanBuildItem(SimpleScheduler.class));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import jakarta.annotation.Priority;
import jakarta.enterprise.event.Observes;
import jakarta.interceptor.Interceptor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -43,15 +41,15 @@ void observeFailedExecution(@Observes FailedExecution failedExecution) {

static class Jobs {

volatile boolean preStart;
volatile boolean started;

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

@Scheduled(every = "0.2s", skipExecutionIf = Scheduled.ApplicationNotRunning.class)
void scheduleAfterStarted() {
if (!preStart) {
if (!started) {
throw new IllegalStateException();
}
}
Expand Down

0 comments on commit 34773da

Please sign in to comment.