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

Make sure that disabled jobs are paused in Quartz #25408

Merged
merged 1 commit into from
May 11, 2022
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 @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pause the job disabled job to effectively let Quartz know that this should not be invoked in any way.
When the job is enabled/turned on, it'll be rescheduled and executed again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in a org.quartz.Scheduler.pauseJob(JobKey) invocation, right? What happens if the job does not exist yet? Is it a no-op?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the job does not exists, it is a no-op.
I'll add a test case in the integration test project to make sure we won't have a regression here.

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't access a static variable like this...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't give this much thought since it is a variable that's there to only show that it is not initialized.

}

@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);
}

}