Skip to content

Commit

Permalink
replace usage of java scheduler with quartz
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Sep 10, 2024
1 parent e0d7182 commit 42497d8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.ConfigurationFileChanged;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import com.github.thmarx.cms.api.scheduler.CronJobScheduler;
import com.google.inject.Inject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
Expand All @@ -50,12 +50,12 @@
@Slf4j
@RequiredArgsConstructor(onConstructor = @__({
@Inject}))
public class ConfigurationManagement implements Runnable {
public class ConfigurationManagement {

final DB db;
@Getter
final Configuration configuration;
final ScheduledExecutorService scheduler;
final CronJobScheduler scheduler;
final EventBus eventBus;

private final List<ConfigurationResource> watched_configurations = new ArrayList<>();
Expand Down Expand Up @@ -91,14 +91,14 @@ private void init_files () throws IOException {
}

public void init() throws IOException {
init(1, 1, TimeUnit.MINUTES);
init("0 * * * * ?");
}

public void init(int initialDelay, int delay, TimeUnit timeUnit) throws IOException {
public void init(String cronExpression) throws IOException {
init_files();

// setup scheduler
scheduler.scheduleWithFixedDelay(this, initialDelay, delay, timeUnit);
scheduler.schedule(cronExpression, "configuration-updater", this::update);
}

private void addPathToWatch(final Path configFile, final Class<? extends Config> configClass) throws IOException {
Expand All @@ -114,8 +114,9 @@ private List<ConfigurationResource> getConfigurations () {
return new ArrayList<>(watched_configurations);
}

@Override
public void run() {

public void update(CronJobContext jobContext) {
System.out.println("update");
log.trace("check for modified configurations {}", db.getFileSystem().resolve(".").toString());
getConfigurations().forEach(config -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public class JettyServer implements AutoCloseable {
private final Injector globalInjector;
private Server server;

private ScheduledExecutorService scheduledExecutorService;

private final EventBus serverEventBus = new DefaultEventBus();

List<VHost> vhosts = new ArrayList<>();
Expand All @@ -96,7 +94,7 @@ public void reloadVHost(String vhost) {

public void startup() throws IOException {

scheduledExecutorService = Executors.newScheduledThreadPool(1);
// scheduledExecutorService = Executors.newScheduledThreadPool(1);
var properties = globalInjector.getInstance(ServerProperties.class);

Files.list(Path.of("hosts")).forEach((hostPath) -> {
Expand All @@ -105,11 +103,9 @@ public void startup() throws IOException {
try {
Configuration configuration = new Configuration(hostPath);
configuration.add(ServerConfiguration.class, new ServerConfiguration(properties));
var host = new VHost(hostPath, configuration, scheduledExecutorService);
var host = new VHost(hostPath, configuration);
host.init(Path.of(Constants.Folders.MODULES), globalInjector);
vhosts.add(host);

host.getInjector().getInstance(ConfigurationManagement.class).init();
} catch (IOException ex) {
log.error(null, ex);
}
Expand Down Expand Up @@ -137,7 +133,7 @@ public void startup() throws IOException {
log.debug("shutting down vhost : " + host.hostnames());
host.shutdown();
});
scheduledExecutorService.shutdownNow();
// scheduledExecutorService.shutdownNow();

try {
globalInjector.getInstance(Scheduler.class).shutdown();
Expand All @@ -155,6 +151,7 @@ public void startup() throws IOException {
HttpConnectionFactory http11 = new HttpConnectionFactory(httpConfig);

QueuedThreadPool threadPool = new QueuedThreadPool(properties.performance().request_workers());
threadPool.setName("cms-request-worker");
//threadPool.setVirtualThreadsExecutor(Executors.newVirtualThreadPerTaskExecutor());

server = new Server(threadPool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,15 @@ public class VHost {

private final Path hostBase;

private final ScheduledExecutorService scheduledExecutorService;

@Getter
private Handler hostHandler;

@Getter
protected Injector injector;

public VHost(final Path hostBase, final Configuration configuration, final ScheduledExecutorService scheduledExecutorService) {
public VHost(final Path hostBase, final Configuration configuration) {
this.hostBase = hostBase;
this.configuration = configuration;
this.scheduledExecutorService = scheduledExecutorService;
}

public String id() {
Expand Down Expand Up @@ -173,7 +170,7 @@ public List<String> hostnames() {
public void init(Path modulesPath, Injector globalInjector) throws IOException {
this.injector = globalInjector.createChildInjector(
new SiteGlobalModule(),
new SiteModule(hostBase, configuration, scheduledExecutorService),
new SiteModule(hostBase, configuration),
new ModulesModule(modulesPath),
new SiteHandlerModule(),
new ThemeModule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.cache.CacheManager;
import com.github.thmarx.cms.api.cache.CacheProvider;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.extensions.CacheProviderExtentionPoint;
import com.github.thmarx.cms.api.hooks.HookSystem;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.github.thmarx.cms.media.SiteMediaManager;
import com.github.thmarx.cms.request.RequestContextFactory;
import com.github.thmarx.cms.content.template.functions.taxonomy.TaxonomyFunction;
import com.github.thmarx.cms.core.scheduler.SiteCronJobScheduler;
import com.github.thmarx.cms.theme.DefaultTheme;
import com.github.thmarx.modules.api.ModuleManager;
import com.google.inject.AbstractModule;
Expand All @@ -72,7 +73,6 @@
import com.google.inject.name.Named;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.ScheduledExecutorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.graalvm.polyglot.Engine;
Expand All @@ -87,12 +87,10 @@ public class SiteModule extends AbstractModule {

private final Path hostBase;
private final Configuration configuration;
private final ScheduledExecutorService scheduledExecutorService;

@Override
protected void configure() {
bind(Configuration.class).toInstance(configuration);
bind(ScheduledExecutorService.class).toInstance(scheduledExecutorService);
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);
bind(ContentParser.class).to(DefaultContentParser.class).in(Singleton.class);
bind(TaxonomyFunction.class).in(Singleton.class);
Expand All @@ -102,6 +100,14 @@ protected void configure() {
bind(ConfigurationManagement.class).in(Singleton.class);
}

@Provides
@Singleton
public ConfigurationManagement configurationManagement(DB db, Configuration configuration, SiteCronJobScheduler scheduler, EventBus eventBus) throws IOException {
ConfigurationManagement cm = new ConfigurationManagement(db, configuration, scheduler, eventBus);
cm.init();
return cm;
}

@Provides
public SiteProperties siteProperties(Configuration configuration) throws IOException {
return configuration.get(SiteConfiguration.class).siteProperties();
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-filesystem</artifactId>
</dependency>
<dependency>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-core</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import com.github.thmarx.cms.api.eventbus.Event;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.EventListener;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import com.github.thmarx.cms.core.scheduler.SingleCronJobScheduler;
import com.github.thmarx.cms.filesystem.FileDB;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -44,6 +44,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.mockito.Mockito;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

/**
*
Expand All @@ -58,12 +62,12 @@ public class ConfigurationManagementReloadTest {

static ConfigurationManagement configurationManagement;

static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
static Scheduler scheduler;

static MockEventBus eventBus = new MockEventBus();

@BeforeAll
static void setup() throws IOException {
static void setup() throws IOException, SchedulerException {

var serverProps = Mockito.mock(ServerProperties.class);

Expand All @@ -75,8 +79,13 @@ static void setup() throws IOException {
db = new FileDB(Path.of("reload/"), eventBus, (path) -> Map.of(), configuration);
db.init();

configurationManagement = new ConfigurationManagement(db, configuration, scheduler, eventBus);
configurationManagement.init(0, 5, TimeUnit.SECONDS);
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
scheduler = schedulerFactory.getScheduler();
scheduler.start();


configurationManagement = new ConfigurationManagement(db, configuration, new SingleCronJobScheduler(scheduler, new CronJobContext()), eventBus);
configurationManagement.init("0/5 * * * * ?");
}

@BeforeEach
Expand Down

0 comments on commit 42497d8

Please sign in to comment.