package com.rongyichuang.review.service;
|
|
import com.rongyichuang.review.dto.response.ReviewExportJobStatus;
|
import com.rongyichuang.review.dto.response.ReviewExportResponse;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import java.util.*;
|
import java.util.concurrent.*;
|
|
/**
|
* 异步评审导出任务服务
|
*/
|
@Service
|
@Slf4j
|
public class ReviewExportJobService {
|
|
private final ReviewExportService reviewExportService;
|
|
private final ExecutorService executor = Executors.newFixedThreadPool(Math.max(2, Runtime.getRuntime().availableProcessors() / 2));
|
|
private final ConcurrentHashMap<String, ReviewExportJobStatus> jobStatusMap = new ConcurrentHashMap<>();
|
|
public ReviewExportJobService(ReviewExportService reviewExportService) {
|
this.reviewExportService = reviewExportService;
|
}
|
|
/**
|
* 启动导出任务
|
*/
|
public String startJob(Long activityId, List<Long> stageIds) {
|
String jobId = UUID.randomUUID().toString();
|
ReviewExportJobStatus init = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.PENDING, null, null, 0);
|
jobStatusMap.put(jobId, init);
|
|
executor.submit(() -> {
|
ReviewExportJobStatus running = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.RUNNING, null, null, 10);
|
jobStatusMap.put(jobId, running);
|
try {
|
ReviewExportResponse res = reviewExportService.exportReviewZip(activityId, stageIds);
|
if (res != null && res.isSuccess()) {
|
ReviewExportJobStatus done = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.SUCCEEDED, res.getUrl(), res.getMessage(), 100);
|
jobStatusMap.put(jobId, done);
|
} else {
|
String msg = res != null ? res.getMessage() : "导出失败";
|
ReviewExportJobStatus failed = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.FAILED, null, msg, 100);
|
jobStatusMap.put(jobId, failed);
|
}
|
} catch (Exception e) {
|
log.error("导出任务执行失败, jobId: {}", jobId, e);
|
ReviewExportJobStatus failed = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.FAILED, null, e.getMessage(), 100);
|
jobStatusMap.put(jobId, failed);
|
}
|
});
|
|
return jobId;
|
}
|
|
/**
|
* 查询任务状态
|
*/
|
public ReviewExportJobStatus getStatus(String jobId) {
|
return jobStatusMap.get(jobId);
|
}
|
|
}
|