Skip to content

Commit

Permalink
Merge pull request #96 from evenh/spring-boot/scheduler-starter-flex
Browse files Browse the repository at this point in the history
Allow for custom DbSchedulerStarter bean
  • Loading branch information
kagkarlsson authored Mar 29, 2020
2 parents 3de3f07 + dc63d5e commit 346fae6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ db-scheduler.table-name=scheduled_tasks
db-scheduler.immediate-execution-enabled=false
db-scheduler.scheduler-name=
db-scheduler.threads=10
# Ignored if a custom DbSchedulerStarter bean is defined
db-scheduler.delay-startup-until-context-ready=false
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public HealthIndicator dbScheduler(Scheduler scheduler) {
}

@ConditionalOnBean(Scheduler.class)
@ConditionalOnMissingBean
@Bean
public DbSchedulerStarter dbSchedulerStarter(Scheduler scheduler) {
if (config.isDelayStartupUntilContextReady()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.boot.actuator.DbSchedulerHealthIndicator;
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerStarter;
import com.github.kagkarlsson.scheduler.boot.config.startup.AbstractSchedulerStarter;
import com.github.kagkarlsson.scheduler.boot.config.startup.ContextReadyStart;
import com.github.kagkarlsson.scheduler.boot.config.startup.ImmediateStart;
import com.github.kagkarlsson.scheduler.task.Task;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import com.github.kagkarlsson.scheduler.task.schedule.Schedule;
import com.google.common.collect.ImmutableList;
import java.util.Objects;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
Expand All @@ -20,8 +26,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.junit.jupiter.api.Test;


public class DbSchedulerAutoConfigurationTest {
private static final Logger log = LoggerFactory.getLogger(DbSchedulerAutoConfigurationTest.class);
Expand Down Expand Up @@ -91,6 +95,47 @@ public void it_should_skip_autoconfiguration_if_explicitly_disabled() {
});
}

@Test
public void it_should_start_as_soon_as_possible() {
ctxRunner
.withPropertyValues("db-scheduler.delay-startup-until-context-ready=false")
.run((AssertableApplicationContext ctx) -> {
assertThat(ctx).hasSingleBean(Scheduler.class);

assertThat(ctx).hasSingleBean(DbSchedulerStarter.class);
assertThat(ctx).hasSingleBean(ImmediateStart.class);
assertThat(ctx).doesNotHaveBean(ContextReadyStart.class);

});
}

@Test
public void it_should_start_when_the_context_is_ready() {
ctxRunner
.withPropertyValues("db-scheduler.delay-startup-until-context-ready=true")
.run((AssertableApplicationContext ctx) -> {
assertThat(ctx).hasSingleBean(Scheduler.class);

assertThat(ctx).hasSingleBean(DbSchedulerStarter.class);
assertThat(ctx).hasSingleBean(ContextReadyStart.class);
assertThat(ctx).doesNotHaveBean(ImmediateStart.class);

});
}

@Test
public void it_should_support_custom_starting_strategies() {
ctxRunner
.withUserConfiguration(CustomStarterConfiguration.class)
.run((AssertableApplicationContext ctx) -> {
assertThat(ctx).hasSingleBean(Scheduler.class);

assertThat(ctx).hasSingleBean(DbSchedulerStarter.class);
assertThat(ctx).doesNotHaveBean(ContextReadyStart.class);
assertThat(ctx).doesNotHaveBean(ImmediateStart.class);
});
}

@Configuration
static class SingleTaskConfiguration {
@Bean
Expand All @@ -117,6 +162,28 @@ Task<String> thirdTask() {
}
}

@Configuration
static class CustomStarterConfiguration extends SingleTaskConfiguration {
@Bean
DbSchedulerStarter someCustomStarter(Scheduler scheduler) {
return new SomeCustomStarter(scheduler);
}

static class SomeCustomStarter extends AbstractSchedulerStarter {
SomeCustomStarter(Scheduler scheduler) {
super(scheduler);

try {
log.info("Thinking 5 seconds before starting the scheduler");
Thread.sleep(5_000);
doStart();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private static Task<String> namedStringTask(String name) {
Objects.requireNonNull(name);

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<dependency-plugin.failOnWarning>true</dependency-plugin.failOnWarning>

<!-- Dependency versions -->
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
</properties>

<modules>
Expand Down

0 comments on commit 346fae6

Please sign in to comment.