|
|
@@ -21,7 +21,6 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
@@ -38,7 +37,6 @@ public class AdminController {
|
|
|
private final RetryService retryService;
|
|
|
private final ManualTencentCallbackService manualTencentCallbackService;
|
|
|
private final StringRedisTemplate redisTemplate;
|
|
|
- private final Map<String, RedisMaintenanceJob> redisMaintenanceJobs = new ConcurrentHashMap<>();
|
|
|
|
|
|
public AdminController(ConversionClient conversionClient,
|
|
|
@Nullable ConversionSyncRunner conversionSyncRunner,
|
|
|
@@ -193,7 +191,7 @@ public class AdminController {
|
|
|
String jobId = UUID.randomUUID().toString();
|
|
|
RedisMaintenanceJob job = new RedisMaintenanceJob(jobId, "RUNNING", ttlLessThanSeconds, scanCount, maxKeys,
|
|
|
0, 0, null, List.of());
|
|
|
- redisMaintenanceJobs.put(jobId, job);
|
|
|
+ saveRedisMaintenanceJob(job);
|
|
|
List<String> taskPatterns = List.copyOf(patterns);
|
|
|
long taskTtlLessThanSeconds = ttlLessThanSeconds;
|
|
|
long taskScanCount = scanCount;
|
|
|
@@ -213,7 +211,7 @@ public class AdminController {
|
|
|
|
|
|
@GetMapping("/redis/delete-bid-media-expiring-before/{jobId}")
|
|
|
public Object getDeleteBidMediaExpiringBeforeJob(@PathVariable("jobId") String jobId) {
|
|
|
- RedisMaintenanceJob job = redisMaintenanceJobs.get(jobId);
|
|
|
+ Map<Object, Object> job = readRedisMaintenanceJob(jobId);
|
|
|
if (job == null) {
|
|
|
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "job not found");
|
|
|
}
|
|
|
@@ -237,16 +235,16 @@ public class AdminController {
|
|
|
));
|
|
|
totalScanned += result.scanned();
|
|
|
totalDeleted += result.deleted();
|
|
|
- redisMaintenanceJobs.put(jobId, new RedisMaintenanceJob(jobId, "RUNNING", ttlLessThanSeconds,
|
|
|
+ saveRedisMaintenanceJob(new RedisMaintenanceJob(jobId, "RUNNING", ttlLessThanSeconds,
|
|
|
scanCount, maxKeys, totalScanned, totalDeleted, null, List.copyOf(results)));
|
|
|
if (totalScanned >= maxKeys) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- redisMaintenanceJobs.put(jobId, new RedisMaintenanceJob(jobId, "COMPLETED", ttlLessThanSeconds,
|
|
|
+ saveRedisMaintenanceJob(new RedisMaintenanceJob(jobId, "COMPLETED", ttlLessThanSeconds,
|
|
|
scanCount, maxKeys, totalScanned, totalDeleted, null, List.copyOf(results)));
|
|
|
} catch (Exception e) {
|
|
|
- redisMaintenanceJobs.put(jobId, new RedisMaintenanceJob(jobId, "FAILED", ttlLessThanSeconds,
|
|
|
+ saveRedisMaintenanceJob(new RedisMaintenanceJob(jobId, "FAILED", ttlLessThanSeconds,
|
|
|
scanCount, maxKeys, totalScanned, totalDeleted, rootMessage(e), List.copyOf(results)));
|
|
|
}
|
|
|
}
|
|
|
@@ -367,6 +365,35 @@ public class AdminController {
|
|
|
return new DeleteExpiringResult(scanned, deleted, false);
|
|
|
}
|
|
|
|
|
|
+ private void saveRedisMaintenanceJob(RedisMaintenanceJob job) {
|
|
|
+ try {
|
|
|
+ String key = redisJobKey(job.jobId());
|
|
|
+ Map<String, String> values = Map.of(
|
|
|
+ "jobId", job.jobId(),
|
|
|
+ "status", job.status(),
|
|
|
+ "ttlLessThanSeconds", String.valueOf(job.ttlLessThanSeconds()),
|
|
|
+ "scanCount", String.valueOf(job.scanCount()),
|
|
|
+ "maxKeys", String.valueOf(job.maxKeys()),
|
|
|
+ "scanned", String.valueOf(job.scanned()),
|
|
|
+ "deleted", String.valueOf(job.deleted()),
|
|
|
+ "error", job.error() == null ? "" : job.error(),
|
|
|
+ "patterns", job.patterns().toString()
|
|
|
+ );
|
|
|
+ redisTemplate.opsForHash().putAll(key, values);
|
|
|
+ redisTemplate.expire(key, Duration.ofHours(2));
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ // Job 状态只用于管理查询,写状态失败不影响清理任务继续执行。
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<Object, Object> readRedisMaintenanceJob(String jobId) {
|
|
|
+ Map<Object, Object> payload = redisTemplate.opsForHash().entries(redisJobKey(jobId));
|
|
|
+ if (payload == null || payload.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return payload;
|
|
|
+ }
|
|
|
+
|
|
|
private static List<String> parseStringList(Object raw) {
|
|
|
if (!(raw instanceof Iterable<?> iterable)) {
|
|
|
return List.of();
|
|
|
@@ -412,6 +439,10 @@ public class AdminController {
|
|
|
return List.of(-1, -2, -3, -4, -5, -6, -7);
|
|
|
}
|
|
|
|
|
|
+ private static String redisJobKey(String jobId) {
|
|
|
+ return "adx:admin:redis-maintenance-job:" + jobId;
|
|
|
+ }
|
|
|
+
|
|
|
private static String rootMessage(Throwable e) {
|
|
|
Throwable cur = e;
|
|
|
while (cur.getCause() != null) {
|