From 8bde6e3636dd45aeac3aeb2b0e5e07b7e272a88b Mon Sep 17 00:00:00 2001 From: Arnaud Mengus Date: Tue, 28 May 2024 15:24:38 +0000 Subject: [PATCH] Begin JobRunr Ingestor Job --- .../ai/dragon/service/IngestorService.java | 23 +++++++++++ .../java/ai/dragon/service/JobService.java | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 backend/src/main/java/ai/dragon/service/IngestorService.java create mode 100644 backend/src/main/java/ai/dragon/service/JobService.java diff --git a/backend/src/main/java/ai/dragon/service/IngestorService.java b/backend/src/main/java/ai/dragon/service/IngestorService.java new file mode 100644 index 00000000..084c7e0c --- /dev/null +++ b/backend/src/main/java/ai/dragon/service/IngestorService.java @@ -0,0 +1,23 @@ +package ai.dragon.service; + +import org.jobrunr.jobs.annotations.Job; +import org.jobrunr.jobs.annotations.Recurring; +import org.jobrunr.jobs.context.JobContext; +import org.jobrunr.jobs.context.JobRunrDashboardLogger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +@Service +public class IngestorService { + private final Logger logger = new JobRunrDashboardLogger(LoggerFactory.getLogger(this.getClass())); + + public static final String INGESTOR_RECURRING_JOB_ID = "ingestor-recurring-job"; + + @Recurring(id = INGESTOR_RECURRING_JOB_ID, cron = "* * * * *") + @Job(name = "Ingestor Recurring Job") + public void executeSampleJob(JobContext jobContext) { + logger.info("testinfo"); + jobContext.logger().info("new info"); + } +} diff --git a/backend/src/main/java/ai/dragon/service/JobService.java b/backend/src/main/java/ai/dragon/service/JobService.java new file mode 100644 index 00000000..4253627f --- /dev/null +++ b/backend/src/main/java/ai/dragon/service/JobService.java @@ -0,0 +1,40 @@ +package ai.dragon.service; + +import org.jobrunr.jobs.Job; +import org.jobrunr.jobs.RecurringJob; +import org.jobrunr.storage.JobNotFoundException; +import org.jobrunr.storage.RecurringJobsResult; +import org.jobrunr.storage.StorageProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class JobService { + private RecurringJobsResult recurringJobsResult; + + @Autowired + private StorageProvider storageProvider; + + public void onApplicationStartup() { + this.triggerRecurringJob(IngestorService.INGESTOR_RECURRING_JOB_ID); + } + + public void triggerRecurringJob(String id) { + final RecurringJob recurringJob = recurringJobResults() + .stream() + .filter(rj -> rj.getId().equals(id)) + .findFirst() + .orElseThrow(() -> new JobNotFoundException(id)); + + final Job job = recurringJob.toEnqueuedJob(); + storageProvider.save(job); + } + + private RecurringJobsResult recurringJobResults() { + if (recurringJobsResult == null + || storageProvider.recurringJobsUpdated(recurringJobsResult.getLastModifiedHash())) { + recurringJobsResult = storageProvider.getRecurringJobs(); + } + return recurringJobsResult; + } +}