Skip to content

Commit

Permalink
Merge pull request #25408 from machi1990/fix/25214
Browse files Browse the repository at this point in the history
Make sure that disabled jobs are paused in Quartz
  • Loading branch information
machi1990 authored May 11, 2022
2 parents 9873e60 + 52e83f1 commit 70a6197
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc
String cron = SchedulerUtils.lookUpPropertyValue(scheduled.cron());
if (!cron.isEmpty()) {
if (SchedulerUtils.isOff(cron)) {
this.pause(identity);
continue;
}
if (!CronType.QUARTZ.equals(cronType)) {
Expand Down Expand Up @@ -214,6 +215,7 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc
} else if (!scheduled.every().isEmpty()) {
OptionalLong everyMillis = SchedulerUtils.parseEveryAsMillis(scheduled);
if (!everyMillis.isPresent()) {
this.pause(identity);
continue;
}
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.it.quartz;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/scheduler/disabled")
public class DisabledMethodResource {

@Inject
DisabledScheduledMethods disabledScheduledMethods;

@GET
@Path("cron")
@Produces(MediaType.TEXT_PLAIN)
public String getCronCount() {
return disabledScheduledMethods.valueSetByCronScheduledMethod;
}

@GET
@Path("every")
@Produces(MediaType.TEXT_PLAIN)
public String getEveryCount() {
return disabledScheduledMethods.valueSetByEveryScheduledMethod;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.it.quartz;

import javax.enterprise.context.ApplicationScoped;

import io.quarkus.scheduler.Scheduled;

@ApplicationScoped
public class DisabledScheduledMethods {
volatile static String valueSetByCronScheduledMethod = "";
volatile static String valueSetByEveryScheduledMethod = "";

// This should never be called as the job is disabled
@Scheduled(cron = "${disabled}", identity = "disabled-cron-counter")
void setValueByCron() {
valueSetByCronScheduledMethod = "cron";
}

// This should never be called as the job is turned off
@Scheduled(every = "${off}", identity = "disabled-every-counter")
void setValueByEvery() {
valueSetByEveryScheduledMethod = "every";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ quarkus.flyway.baseline-description=Quartz
# Register de LoggingJobHistoryPlugin for testing
quarkus.quartz.plugins.jobHistory.class=org.quartz.plugins.history.LoggingJobHistoryPlugin
quarkus.quartz.plugins.jobHistory.properties.jobSuccessMessage=Job [{1}.{0}] execution complete and reports: {8}

disabled=disabled
off=off
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.it.quartz;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
Expand All @@ -19,6 +20,14 @@ public void testCount() throws InterruptedException {
assertCounter("/scheduler/count/fix-8555");
}

@Test
public void testDisabledMethodsShouldNeverBeExecuted() throws InterruptedException {
// Wait at least 1 second
Thread.sleep(1000);
assertEmptyValueForDisabledMethod("/scheduler/disabled/cron");
assertEmptyValueForDisabledMethod("/scheduler/disabled/every");
}

private void assertCounter(String counterPath) {
Response response = given().when().get(counterPath);
String body = response.asString();
Expand All @@ -29,4 +38,13 @@ private void assertCounter(String counterPath) {
.statusCode(200);
}

private void assertEmptyValueForDisabledMethod(String path) {
Response response = given().when().get(path);
String body = response.asString();
assertEquals("", body);
response
.then()
.statusCode(200);
}

}

0 comments on commit 70a6197

Please sign in to comment.