Use queue to schedule jobs
This commit is contained in:
+80
-14
@@ -2,47 +2,113 @@ package com.adityachandel.booklore.quartz;
|
||||
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.dto.request.MetadataRefreshRequest;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.*;
|
||||
import org.quartz.impl.matchers.KeyMatcher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class JobSchedulerService {
|
||||
|
||||
private final Scheduler scheduler;
|
||||
private final BlockingQueue<MetadataRefreshRequest> jobQueue = new LinkedBlockingQueue<>();
|
||||
private boolean isJobRunning = false;
|
||||
|
||||
public void scheduleMetadataRefreshV2(MetadataRefreshRequest request) {
|
||||
scheduleJob(request);
|
||||
public synchronized void scheduleMetadataRefreshV2(MetadataRefreshRequest request) {
|
||||
log.info("Received request to schedule metadata refresh: {}", request);
|
||||
jobQueue.offer(request);
|
||||
log.debug("Added request to job queue. Queue size: {}", jobQueue.size());
|
||||
processQueue();
|
||||
}
|
||||
|
||||
private <T> void scheduleJob(T request) {
|
||||
private synchronized void processQueue() {
|
||||
if (isJobRunning) {
|
||||
log.debug("A job is already running. Queue processing is paused.");
|
||||
return;
|
||||
}
|
||||
if (jobQueue.isEmpty()) {
|
||||
log.debug("Job queue is empty. Nothing to process.");
|
||||
return;
|
||||
}
|
||||
|
||||
isJobRunning = true;
|
||||
MetadataRefreshRequest request = jobQueue.poll();
|
||||
log.info("Processing job from queue. Remaining queue size: {}", jobQueue.size());
|
||||
if (request != null) {
|
||||
String jobId = generateUniqueJobId(request);
|
||||
try {
|
||||
log.info("Scheduling job with ID: {}", jobId);
|
||||
scheduleJob(request, jobId);
|
||||
} catch (Exception e) {
|
||||
isJobRunning = false;
|
||||
log.error("Failed to schedule job with ID: {}. Error: {}", jobId, e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private <T> void scheduleJob(T request, String jobId) {
|
||||
try {
|
||||
JobDataMap jobDataMap = new JobDataMap();
|
||||
jobDataMap.put("request", request);
|
||||
|
||||
JobDetail jobDetail = JobBuilder.newJob(RefreshMetadataJob.class)
|
||||
.withIdentity("metadataRefreshJobV2", "metadataRefreshJobV2")
|
||||
.withIdentity(jobId, "metadataRefreshJobGroup")
|
||||
.usingJobData(jobDataMap)
|
||||
.build();
|
||||
|
||||
Trigger trigger = TriggerBuilder.newTrigger()
|
||||
.forJob(jobDetail)
|
||||
.withIdentity("metadataRefreshJobV2", "metadataRefreshJobV2")
|
||||
.withIdentity(jobId, "metadataRefreshJobGroup")
|
||||
.startNow()
|
||||
.build();
|
||||
|
||||
scheduler.scheduleJob(jobDetail, trigger);
|
||||
log.info("Job scheduled successfully with ID: {}", jobId);
|
||||
|
||||
scheduler.getListenerManager().addJobListener(new JobListener() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "JobCompletionListener";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jobToBeExecuted(JobExecutionContext context) {
|
||||
log.debug("Job is about to be executed. JobKey: {}", context.getJobDetail().getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jobExecutionVetoed(JobExecutionContext context) {
|
||||
log.warn("Job execution was vetoed. JobKey: {}", context.getJobDetail().getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
|
||||
log.info("Job executed successfully. JobKey: {}", context.getJobDetail().getKey());
|
||||
if (jobException != null) {
|
||||
log.error("Job execution encountered an error: {}", jobException.getMessage(), jobException);
|
||||
}
|
||||
isJobRunning = false;
|
||||
log.debug("Job completion handled. Processing next job in the queue.");
|
||||
processQueue();
|
||||
}
|
||||
}, KeyMatcher.keyEquals(jobDetail.getKey()));
|
||||
|
||||
} catch (SchedulerException e) {
|
||||
handleSchedulerException(e);
|
||||
log.error("Error while scheduling job with ID: {}. Error: {}", jobId, e.getMessage(), e);
|
||||
throw ApiError.SCHEDULE_REFRESH_ERROR.createException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSchedulerException(Exception e) {
|
||||
if (e.getMessage() != null && e.getMessage().contains("already exists")) {
|
||||
throw ApiError.ANOTHER_METADATA_JOB_RUNNING.createException();
|
||||
}
|
||||
throw ApiError.SCHEDULE_REFRESH_ERROR.createException(e.getMessage());
|
||||
private String generateUniqueJobId(MetadataRefreshRequest request) {
|
||||
String jobId = "metadataRefreshJob_" + System.currentTimeMillis();
|
||||
log.debug("Generated unique job ID: {}", jobId);
|
||||
return jobId;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -100,7 +100,7 @@ public class BookMetadataService {
|
||||
try {
|
||||
Map<MetadataProvider, FetchedBookMetadata> metadataMap = fetchMetadataForBook(providers, bookEntity);
|
||||
if (providers.contains(GoodReads)) {
|
||||
Thread.sleep(Duration.ofSeconds(ThreadLocalRandom.current().nextInt(2, 6)).toMillis());
|
||||
Thread.sleep(ThreadLocalRandom.current().nextLong(500, 1500));
|
||||
}
|
||||
FetchedBookMetadata fetchedBookMetadata = getRawOrCombinedMetadata(request, metadataMap);
|
||||
updateBookMetadata(bookEntity, fetchedBookMetadata, shouldRefreshCovers(request));
|
||||
|
||||
@@ -409,6 +409,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
this.bookFilterComponent.activeCategory = null;
|
||||
this.bookFilterComponent.activeAuthor = null;
|
||||
this.clearSearch();
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
|
||||
Reference in New Issue
Block a user