Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a new ApplicationNotRunning predicate for @Scheduled #24605

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -201,4 +205,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;
}

}

}