diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/configuration/ConfigurationManagement.java b/cms-api/src/main/java/com/github/thmarx/cms/api/configuration/ConfigurationManagement.java index fef2c6a8..d4ad1452 100644 --- a/cms-api/src/main/java/com/github/thmarx/cms/api/configuration/ConfigurationManagement.java +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/configuration/ConfigurationManagement.java @@ -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; @@ -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 watched_configurations = new ArrayList<>(); @@ -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 configClass) throws IOException { @@ -114,8 +114,9 @@ private List 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 { diff --git a/cms-server/src/main/java/com/github/thmarx/cms/server/JettyServer.java b/cms-server/src/main/java/com/github/thmarx/cms/server/JettyServer.java index 1184f1e8..519ad5e6 100644 --- a/cms-server/src/main/java/com/github/thmarx/cms/server/JettyServer.java +++ b/cms-server/src/main/java/com/github/thmarx/cms/server/JettyServer.java @@ -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 vhosts = new ArrayList<>(); @@ -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) -> { @@ -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); } @@ -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(); @@ -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); diff --git a/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java b/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java index 843800c2..f9b820c6 100644 --- a/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java +++ b/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java @@ -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() { @@ -173,7 +170,7 @@ public List 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()); diff --git a/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteGlobalModule.java b/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteGlobalModule.java index c275c1f6..e3224156 100644 --- a/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteGlobalModule.java +++ b/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteGlobalModule.java @@ -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; diff --git a/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteModule.java b/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteModule.java index a16cde1a..6c0bc5be 100644 --- a/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteModule.java +++ b/cms-server/src/main/java/com/github/thmarx/cms/server/configs/SiteModule.java @@ -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; @@ -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; @@ -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); @@ -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(); diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index dbfc7bb5..2bbcad44 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -18,6 +18,10 @@ com.github.thmarx.cms cms-filesystem + + com.github.thmarx.cms + cms-core + \ No newline at end of file diff --git a/integration-tests/src/test/java/com/github/thmarx/cms/integration/tests/ConfigurationManagementReloadTest.java b/integration-tests/src/test/java/com/github/thmarx/cms/integration/tests/ConfigurationManagementReloadTest.java index ad0e8f65..ad37dc7d 100644 --- a/integration-tests/src/test/java/com/github/thmarx/cms/integration/tests/ConfigurationManagementReloadTest.java +++ b/integration-tests/src/test/java/com/github/thmarx/cms/integration/tests/ConfigurationManagementReloadTest.java @@ -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; @@ -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; /** * @@ -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); @@ -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