Skip to content

Commit

Permalink
Merge pull request #71 from evenh/startup-strategy
Browse files Browse the repository at this point in the history
Configurable scheduler start
  • Loading branch information
kagkarlsson authored Oct 16, 2019
2 parents 2e5af93 + aa6338e commit 626230c
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ This software includes third party software subject to the following licenses:
Spring Boot Actuator AutoConfigure under Apache License, Version 2.0
Spring Boot Actuator Starter under Apache License, Version 2.0
Spring Boot AOP Starter under Apache License, Version 2.0
Spring Boot Auto-Configure Annotation Processor under Apache License, Version 2.0
Spring Boot AutoConfigure under Apache License, Version 2.0
Spring Boot Configuration Processor under Apache License, Version 2.0
Spring Boot Data JPA Starter under Apache License, Version 2.0
Spring Boot JDBC Starter under Apache License, Version 2.0
Spring Boot Json Starter under Apache License, Version 2.0
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ For Spring Boot applications, there is a starter `db-scheduler-spring-boot-start
<dependency>
<groupId>com.github.kagkarlsson</groupId>
<artifactId>db-scheduler-spring-boot-starter</artifactId>
<version>6.0</version>
<version>6.1</version>
</dependency>
```
**NOTE**: This includes the db-scheduler dependency itself.
Expand All @@ -202,6 +202,7 @@ db-scheduler.table-name=scheduled_tasks
db-scheduler.immediate-execution-enabled=false
db-scheduler.scheduler-name=
db-scheduler.threads=10
db-scheduler.delay-startup-until-context-ready=false
```
Expand Down
17 changes: 17 additions & 0 deletions db-scheduler-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<!-- Validation -->
<dependency>
Expand All @@ -66,6 +76,13 @@
<scope>provided</scope>
</dependency>

<!-- Annotations -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.github.kagkarlsson.scheduler.boot.actuator.DbSchedulerHealthIndicator;
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerCustomizer;
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerProperties;
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerStarter;
import com.github.kagkarlsson.scheduler.boot.config.startup.ContextReadyStart;
import com.github.kagkarlsson.scheduler.boot.config.startup.ImmediateStart;
import com.github.kagkarlsson.scheduler.task.OnStartup;
import com.github.kagkarlsson.scheduler.task.Task;
import java.util.List;
Expand Down Expand Up @@ -62,7 +65,7 @@ public DbSchedulerCustomizer noopCustomizer() {

@ConditionalOnBean(DataSource.class)
@ConditionalOnMissingBean
@Bean(initMethod = "start", destroyMethod = "stop")
@Bean(destroyMethod = "stop")
public Scheduler scheduler(DbSchedulerCustomizer customizer) {
log.info("Creating db-scheduler using tasks from Spring context: {}", configuredTasks);

Expand Down Expand Up @@ -112,6 +115,16 @@ public HealthIndicator dbScheduler(Scheduler scheduler) {
return new DbSchedulerHealthIndicator(scheduler);
}

@ConditionalOnBean(Scheduler.class)
@Bean
public DbSchedulerStarter dbSchedulerStarter(Scheduler scheduler) {
if (config.isDelayStartupUntilContextReady()) {
return new ContextReadyStart(scheduler);
}

return new ImmediateStart(scheduler);
}

private static DataSource configureDataSource(DataSource existingDataSource) {
if (existingDataSource instanceof TransactionAwareDataSourceProxy) {
log.debug("Using an already transaction aware DataSource");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public class DbSchedulerProperties {
*/
private Optional<Integer> pollingLimit = Optional.empty();

/**
* <p>Whether to start the scheduler when the application context has been loaded or as soon as
* possible.
*/
private boolean delayStartupUntilContextReady = false;

public boolean isEnabled() {
return enabled;
}
Expand Down Expand Up @@ -131,4 +137,12 @@ public Optional<Integer> getPollingLimit() {
public void setPollingLimit(final Optional<Integer> pollingLimit) {
this.pollingLimit = pollingLimit;
}

public boolean isDelayStartupUntilContextReady() {
return delayStartupUntilContextReady;
}

public void setDelayStartupUntilContextReady(final boolean delayStartupUntilContextReady) {
this.delayStartupUntilContextReady = delayStartupUntilContextReady;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.kagkarlsson.scheduler.boot.config;

public interface DbSchedulerStarter {
void doStart();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.kagkarlsson.scheduler.boot.config.startup;

import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.SchedulerState;
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerStarter;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractSchedulerStarter implements DbSchedulerStarter {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private final Scheduler scheduler;

protected AbstractSchedulerStarter(Scheduler scheduler) {
this.scheduler = Objects.requireNonNull(scheduler, "A scheduler must be provided");
}

@Override
public void doStart() {
SchedulerState state = scheduler.getSchedulerState();

if (state.isShuttingDown()) {
log.warn("Scheduler is shutting down - will not attempting to start");
return;
}

if (state.isStarted()) {
log.info("Scheduler already started - will not attempt to start again");
return;
}

log.info("Triggering scheduler start");
scheduler.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.kagkarlsson.scheduler.boot.config.startup;

import com.github.kagkarlsson.scheduler.Scheduler;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;

public class ContextReadyStart extends AbstractSchedulerStarter {
public ContextReadyStart(Scheduler scheduler) {
super(scheduler);
}

@EventListener(ContextRefreshedEvent.class)
public void whenContextIsReady() {
doStart();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.kagkarlsson.scheduler.boot.config.startup;

import com.github.kagkarlsson.scheduler.Scheduler;
import javax.annotation.PostConstruct;

public class ImmediateStart extends AbstractSchedulerStarter {
public ImmediateStart(final Scheduler scheduler) {
super(scheduler);
}

@PostConstruct
void startImmediately() {
doStart();
}
}
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@
<configuration>
<failOnWarning>true</failOnWarning>
<ignoreNonCompile>true</ignoreNonCompile>
<ignoredDependencies>
<ignoredDependency>org.springframework.boot:spring-boot-autoconfigure-processor</ignoredDependency>
<ignoredDependency>org.springframework.boot:spring-boot-configuration-processor</ignoredDependency>
</ignoredDependencies>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit 626230c

Please sign in to comment.