-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from kagkarlsson/support_disabled_schedules
Support for disabled schedules
- Loading branch information
Showing
11 changed files
with
263 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...eduler/src/main/java/com/github/kagkarlsson/scheduler/task/schedule/DisabledSchedule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.kagkarlsson.scheduler.task.schedule; | ||
|
||
import com.github.kagkarlsson.scheduler.task.ExecutionComplete; | ||
|
||
import java.time.Instant; | ||
|
||
public class DisabledSchedule implements Schedule{ | ||
|
||
@Override | ||
public Instant getNextExecutionTime(ExecutionComplete executionComplete) { | ||
throw unsupportedException(); | ||
} | ||
|
||
@Override | ||
public boolean isDeterministic() { | ||
throw unsupportedException(); | ||
} | ||
|
||
@Override | ||
public Instant getInitialExecutionTime(Instant now) { | ||
throw unsupportedException(); | ||
} | ||
|
||
@Override | ||
public boolean isDisabled() { | ||
return true; | ||
} | ||
|
||
private UnsupportedOperationException unsupportedException() { | ||
return new UnsupportedOperationException("DisabledSchedule does not support any other operations than 'isDisabled()'. This appears to be a bug."); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../src/main/java/com/github/kagkarlsson/scheduler/task/schedule/DisabledScheduleParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.kagkarlsson.scheduler.task.schedule; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.regex.MatchResult; | ||
import java.util.regex.Pattern; | ||
|
||
final class DisabledScheduleParser extends RegexBasedParser { | ||
private static final Pattern DISABLED_PATTERN = Pattern.compile("^-$"); | ||
private static final List<String> EXAMPLES = Collections.singletonList("-"); | ||
|
||
DisabledScheduleParser() { | ||
super(DISABLED_PATTERN, EXAMPLES); | ||
} | ||
|
||
@Override | ||
protected Schedule matchedSchedule(MatchResult matchResult) { | ||
return new DisabledSchedule(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...duler/src/test/java/com/github/kagkarlsson/scheduler/functional/DisabledCronTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.github.kagkarlsson.scheduler.functional; | ||
|
||
import com.github.kagkarlsson.scheduler.EmbeddedPostgresqlExtension; | ||
import com.github.kagkarlsson.scheduler.TestTasks; | ||
import com.github.kagkarlsson.scheduler.task.TaskInstanceId; | ||
import com.github.kagkarlsson.scheduler.task.helper.RecurringTask; | ||
import com.github.kagkarlsson.scheduler.task.helper.Tasks; | ||
import com.github.kagkarlsson.scheduler.task.schedule.Schedules; | ||
import com.github.kagkarlsson.scheduler.testhelper.ManualScheduler; | ||
import com.github.kagkarlsson.scheduler.testhelper.SettableClock; | ||
import com.github.kagkarlsson.scheduler.testhelper.TestHelper; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class DisabledCronTaskTest { | ||
|
||
public static final String RECURRING_A = "recurring-a"; | ||
private SettableClock clock; | ||
|
||
@RegisterExtension | ||
public EmbeddedPostgresqlExtension postgres = new EmbeddedPostgresqlExtension(); | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
clock = new SettableClock(); | ||
clock.set( | ||
ZonedDateTime.of( | ||
LocalDate.of(2018, 3, 1), | ||
LocalTime.of(8, 0), | ||
ZoneId.systemDefault()).toInstant()); | ||
} | ||
|
||
@Test | ||
public void should_remove_existing_executions_for_tasks_with_disabled_schedule() { | ||
RecurringTask<Void> recurringTask = Tasks.recurring(RECURRING_A, Schedules.cron("0 0 12 * * ?")) | ||
.execute(TestTasks.DO_NOTHING); | ||
|
||
ManualScheduler scheduler = manualSchedulerFor(recurringTask); | ||
scheduler.start(); | ||
scheduler.stop(); | ||
|
||
assertTrue(scheduler.getScheduledExecution(TaskInstanceId.of(RECURRING_A, RecurringTask.INSTANCE)).isPresent()); | ||
|
||
RecurringTask<Void> disabledRecurringTask = Tasks.recurring(RECURRING_A, Schedules.cron("-")) | ||
.execute(TestTasks.DO_NOTHING); | ||
|
||
ManualScheduler restartedScheduler = manualSchedulerFor(disabledRecurringTask); | ||
restartedScheduler.start(); | ||
restartedScheduler.stop(); | ||
|
||
assertFalse(scheduler.getScheduledExecution(TaskInstanceId.of(RECURRING_A, RecurringTask.INSTANCE)).isPresent()); | ||
|
||
restartedScheduler.start(); | ||
|
||
assertFalse(scheduler.getScheduledExecution(TaskInstanceId.of(RECURRING_A, RecurringTask.INSTANCE)).isPresent()); | ||
} | ||
|
||
private ManualScheduler manualSchedulerFor(RecurringTask<?> recurringTasks) { | ||
return TestHelper.createManualScheduler(postgres.getDataSource()) | ||
.clock(clock) | ||
.startTasks(singletonList(recurringTasks)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
examples/features/src/main/java/com/github/kagkarlsson/examples/JobChainingPocMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters