From fc19450ffce241d12fdd493a442f77b0808d6f6d Mon Sep 17 00:00:00 2001 From: Arnaud Mengus <44212923+isontheline@users.noreply.github.com> Date: Thu, 30 May 2024 08:48:01 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20JobScheduler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/ai/dragon/config/JobRunrConfig.java | 12 ++++++++++++ .../main/java/ai/dragon/service/SiloJobService.java | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/backend/src/main/java/ai/dragon/config/JobRunrConfig.java b/backend/src/main/java/ai/dragon/config/JobRunrConfig.java index 61730b78..9c7f8421 100644 --- a/backend/src/main/java/ai/dragon/config/JobRunrConfig.java +++ b/backend/src/main/java/ai/dragon/config/JobRunrConfig.java @@ -1,6 +1,8 @@ package ai.dragon.config; +import org.jobrunr.configuration.JobRunr; import org.jobrunr.jobs.mappers.JobMapper; +import org.jobrunr.scheduling.JobScheduler; import org.jobrunr.storage.InMemoryStorageProvider; import org.jobrunr.storage.StorageProvider; import org.springframework.context.annotation.Bean; @@ -14,4 +16,14 @@ public StorageProvider storageProvider(JobMapper jobMapper) { storageProvider.setJobMapper(jobMapper); return storageProvider; } + + @Bean + public JobScheduler jobScheduler(StorageProvider storageProvider) { + return JobRunr.configure() + .useStorageProvider(storageProvider) + .useBackgroundJobServer() + .useDashboard() + .initialize() + .getJobScheduler(); + } } diff --git a/backend/src/main/java/ai/dragon/service/SiloJobService.java b/backend/src/main/java/ai/dragon/service/SiloJobService.java index 940a7961..bbf79dbe 100644 --- a/backend/src/main/java/ai/dragon/service/SiloJobService.java +++ b/backend/src/main/java/ai/dragon/service/SiloJobService.java @@ -6,6 +6,7 @@ import org.dizitart.no2.collection.events.EventType; import org.jobrunr.jobs.context.JobRunrDashboardLogger; import org.jobrunr.scheduling.BackgroundJob; +import org.jobrunr.scheduling.JobScheduler; import org.jobrunr.scheduling.RecurringJobBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,6 +27,9 @@ public class SiloJobService { @Autowired private SiloRepository siloRepository; + @Autowired + private JobScheduler jobScheduler; + private EntityChangeListener entityChangeListener; @PostConstruct @@ -43,6 +47,7 @@ public void onChangeEvent(CollectionEventInfo collectionEventInfo, UUID uuid) // Create Recurrent Job for each Silo siloRepository.find().forEach(siloEntity -> { logger.info(String.format("Creating Silo Ingestor Job : %s", siloEntity.getUuid().toString())); + BackgroundJob.setJobScheduler(jobScheduler); RecurringJobBuilder jobBuilder = RecurringJobBuilder.aRecurringJob() .withId(siloEntity.getUuid().toString()) .withName("Silo Ingestor Job") @@ -55,5 +60,7 @@ public void onChangeEvent(CollectionEventInfo collectionEventInfo, UUID uuid) @PreDestroy private void destroy() { siloRepository.unsubscribe(entityChangeListener); + + // TODO Stop all Recurrent Jobs } }