-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add DefaultOptions to override some global property by `Sys…
…tem.property`
- Loading branch information
Showing
6 changed files
with
170 additions
and
31 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
core/src/main/java/io/github/zero88/schedulerx/DefaultOptions.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,99 @@ | ||
package io.github.zero88.schedulerx; | ||
|
||
import java.time.Duration; | ||
import java.time.format.DateTimeParseException; | ||
|
||
import io.github.zero88.schedulerx.impl.Utils; | ||
import io.github.zero88.schedulerx.trigger.Trigger; | ||
import io.vertx.core.VertxOptions; | ||
|
||
/** | ||
* Instances of this class are used to configure the default options for {@link Trigger}, and {@link Task} instances. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public final class DefaultOptions { | ||
|
||
public static final String DEFAULT_MAX_EXECUTION_TIMEOUT = "schedulerx.default_max_execution_timeout"; | ||
public static final String DEFAULT_MAX_EVALUATION_TIMEOUT = "schedulerx.default_max_evaluation_timeout"; | ||
public static final String DEFAULT_MAX_TRIGGER_RULE_LEEWAY = "schedulerx.default_max_trigger_rule_leeway"; | ||
public static final String DEFAULT_MAX_TRIGGER_PREVIEW_COUNT = "schedulerx.default_max_trigger_preview_count"; | ||
|
||
|
||
private static class Holder { | ||
|
||
private static final DefaultOptions INSTANCE = new DefaultOptions(); | ||
|
||
} | ||
|
||
public static DefaultOptions getInstance() { | ||
return DefaultOptions.Holder.INSTANCE; | ||
} | ||
|
||
/** | ||
* Declares the default max execution timeout. | ||
* | ||
* @apiNote It can be overridden by system property with key {@link #DEFAULT_MAX_EXECUTION_TIMEOUT} | ||
*/ | ||
public final Duration maxExecutionTimeout; | ||
/** | ||
* Declares the default max trigger evaluation timeout. | ||
* | ||
* @apiNote It can be overridden by system property with key {@link #DEFAULT_MAX_EVALUATION_TIMEOUT} | ||
*/ | ||
public final Duration maxEvaluationTimeout; | ||
/** | ||
* Declares the default max trigger rule leeway time. | ||
* | ||
* @apiNote It can be overridden by system property with key {@link #DEFAULT_MAX_TRIGGER_RULE_LEEWAY} | ||
*/ | ||
public final Duration maxTriggerRuleLeeway; | ||
/** | ||
* Declares the default max number of the trigger preview items. | ||
* | ||
* @apiNote It can be overridden by system property with key {@link #DEFAULT_MAX_TRIGGER_PREVIEW_COUNT} | ||
*/ | ||
public final int maxTriggerPreviewCount; | ||
|
||
DefaultOptions() { | ||
this.maxExecutionTimeout = loadMaxExecutionTimeout(); | ||
this.maxEvaluationTimeout = loadMaxEvaluationTimeout(); | ||
this.maxTriggerRuleLeeway = loadTriggerRuleLeeway(); | ||
this.maxTriggerPreviewCount = loadTriggerPreviewCount(); | ||
} | ||
|
||
private static Duration loadMaxExecutionTimeout() { | ||
try { | ||
return Duration.parse(System.getProperty(DEFAULT_MAX_EXECUTION_TIMEOUT)); | ||
} catch (DateTimeParseException | NullPointerException ex) { | ||
return Duration.of(VertxOptions.DEFAULT_MAX_WORKER_EXECUTE_TIME, | ||
Utils.toChronoUnit(VertxOptions.DEFAULT_MAX_WORKER_EXECUTE_TIME_UNIT)); | ||
} | ||
} | ||
|
||
private static Duration loadMaxEvaluationTimeout() { | ||
try { | ||
return Duration.parse(System.getProperty(DEFAULT_MAX_EVALUATION_TIMEOUT)); | ||
} catch (DateTimeParseException | NullPointerException ex) { | ||
return Duration.of(VertxOptions.DEFAULT_MAX_EVENT_LOOP_EXECUTE_TIME, | ||
Utils.toChronoUnit(VertxOptions.DEFAULT_MAX_EVENT_LOOP_EXECUTE_TIME_UNIT)); | ||
} | ||
} | ||
|
||
private static Duration loadTriggerRuleLeeway() { | ||
try { | ||
return Duration.parse(System.getProperty(DEFAULT_MAX_TRIGGER_RULE_LEEWAY)); | ||
} catch (DateTimeParseException | NullPointerException ex) { | ||
return Duration.ofSeconds(10); | ||
} | ||
} | ||
|
||
private static int loadTriggerPreviewCount() { | ||
try { | ||
return Integer.parseInt(System.getProperty(DEFAULT_MAX_TRIGGER_PREVIEW_COUNT)); | ||
} catch (NumberFormatException | NullPointerException ex) { | ||
return 30; | ||
} | ||
} | ||
|
||
} |
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
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
48 changes: 48 additions & 0 deletions
48
core/src/test/java/io/github/zero88/schedulerx/DefaultOptionsTest.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,48 @@ | ||
package io.github.zero88.schedulerx; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
|
||
@Execution(ExecutionMode.SAME_THREAD) | ||
class DefaultOptionsTest { | ||
|
||
@Test | ||
void test_default_options() { | ||
Assertions.assertEquals(DefaultOptions.getInstance().maxEvaluationTimeout, Duration.ofSeconds(2)); | ||
Assertions.assertEquals(DefaultOptions.getInstance().maxExecutionTimeout, Duration.ofSeconds(60)); | ||
Assertions.assertEquals(DefaultOptions.getInstance().maxTriggerPreviewCount, 30); | ||
Assertions.assertEquals(DefaultOptions.getInstance().maxTriggerRuleLeeway, Duration.ofSeconds(10)); | ||
} | ||
|
||
|
||
@Test | ||
@SetSystemProperty(key = "schedulerx.default_max_evaluation_timeout", value = "PT1S") | ||
void test_override_max_evaluation_timeout() { | ||
Assertions.assertEquals(new DefaultOptions().maxEvaluationTimeout, Duration.ofSeconds(1)); | ||
} | ||
|
||
@Test | ||
@SetSystemProperty(key = "schedulerx.default_max_execution_timeout", value = "PT10M") | ||
void test_override_max_execution_timeout() { | ||
Assertions.assertEquals(new DefaultOptions().maxExecutionTimeout, Duration.ofMinutes(10)); | ||
} | ||
|
||
@Test | ||
@SetSystemProperty(key = "schedulerx.default_max_trigger_preview_count", value = "12") | ||
void test_override_max_preview_count() { | ||
Assertions.assertEquals(new DefaultOptions().maxTriggerPreviewCount, 12); | ||
} | ||
|
||
|
||
@Test | ||
@SetSystemProperty(key = "schedulerx.default_max_trigger_rule_leeway", value = "PT3S") | ||
void test_override_max_trigger_rule_leeway() { | ||
Assertions.assertEquals(new DefaultOptions().maxTriggerRuleLeeway, Duration.ofSeconds(3)); | ||
} | ||
|
||
} |