1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
@Service public class ESBatchProcessingMonitorService { private static final Logger logger = LoggerFactory.getLogger(ESBatchProcessingMonitorService.class); private final Map<String, BatchProcessingProgress> progressMap = new ConcurrentHashMap<>();
public BatchProcessingProgress startMonitoring(String taskId, int totalCount, int batchSize) { BatchProcessingProgress progress = new BatchProcessingProgress(); progress.setTaskId(taskId); progress.setTotalCount(totalCount); progress.setBatchSize(batchSize); progress.setStartTime(System.currentTimeMillis()); progress.setStatus(BatchProcessingStatus.RUNNING); progressMap.put(taskId, progress); logger.info("开始监控批次处理任务: {}, 总数量: {}, 批次大小: {}", taskId, totalCount, batchSize); return progress; }
public void updateProgress(String taskId, int processedCount, int successCount, int failureCount) { BatchProcessingProgress progress = progressMap.get(taskId); if (progress != null) { progress.setProcessedCount(processedCount); progress.setSuccessCount(successCount); progress.setFailureCount(failureCount); progress.setLastUpdateTime(System.currentTimeMillis()); double percentage = (double) processedCount / progress.getTotalCount() * 100; progress.setPercentage(percentage); long duration = progress.getLastUpdateTime() - progress.getStartTime(); if (duration > 0) { double speed = (double) processedCount / duration * 1000; progress.setProcessingSpeed(speed); } logger.info("任务 {} 进度更新: {}/{} ({}%), 成功: {}, 失败: {}, 速度: {}/s", taskId, processedCount, progress.getTotalCount(), String.format("%.2f", percentage), successCount, failureCount, String.format("%.2f", progress.getProcessingSpeed())); } }
public void completeTask(String taskId, BatchProcessingResult result) { BatchProcessingProgress progress = progressMap.get(taskId); if (progress != null) { progress.setStatus(BatchProcessingStatus.COMPLETED); progress.setEndTime(System.currentTimeMillis()); progress.setResult(result); logger.info("任务 {} 完成,总耗时: {}ms, 成功: {}, 失败: {}, 成功率: {}%", taskId, progress.getDuration(), result.getSuccessCount(), result.getFailureCount(), String.format("%.2f", result.getSuccessRate())); } }
public void failTask(String taskId, String error) { BatchProcessingProgress progress = progressMap.get(taskId); if (progress != null) { progress.setStatus(BatchProcessingStatus.FAILED); progress.setEndTime(System.currentTimeMillis()); progress.setError(error); logger.error("任务 {} 失败: {}", taskId, error); } }
public BatchProcessingProgress getProgress(String taskId) { return progressMap.get(taskId); }
public Map<String, BatchProcessingProgress> getAllProgress() { return new HashMap<>(progressMap); }
public void cleanupTask(String taskId) { progressMap.remove(taskId); logger.info("清理任务: {}", taskId); } }
|