49 lines
1.8 KiB
Java
49 lines
1.8 KiB
Java
package com.yunhai.userbase.task;
|
||
|
||
import com.yunhai.userbase.service.impl.SignKeyCacheService;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.scheduling.annotation.Async;
|
||
import org.springframework.scheduling.annotation.Scheduled;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
/**
|
||
* 签名密钥共享缓存的对账/同步定时任务。<br>
|
||
* <br>
|
||
* 每天凌晨 3:05 执行(让 {@link OperationLogCleanupTask} 的 3:00 先跑完,避免同一时间打库):
|
||
* <ol>
|
||
* <li>把 application_info 表全量同步到共享 Redis;</li>
|
||
* <li>清理 DB 已删除、但 Redis 残留的旧应用 key;</li>
|
||
* <li>清空本机 AppSecretProvider / LocalAppIdentityProvider 的本地密钥缓存,
|
||
* 避免长驻进程本地缓存和 DB 不一致。</li>
|
||
* </ol>
|
||
* <p>
|
||
* 注:应用日常新增/删除/重置密钥会通过
|
||
* {@link com.yunhai.userbase.service.impl.ApplicationInfoService} 的事务事件
|
||
* 实时触发 {@link SignKeyCacheService} 的 syncOne/removeOne,
|
||
* 本任务仅作为「每日对账兜底」,防止 Redis 故障或人工介入导致的偏差。
|
||
*/
|
||
@Component
|
||
public class SignKeyCacheDailyTask {
|
||
|
||
private static final Logger log = LoggerFactory.getLogger(SignKeyCacheDailyTask.class);
|
||
|
||
@Autowired
|
||
private SignKeyCacheService signKeyCacheService;
|
||
|
||
/**
|
||
* 每天凌晨 3:05 执行,错开 OperationLogCleanupTask 的 3:00。
|
||
*/
|
||
@Scheduled(cron = "0 5 3 * * ?")
|
||
@Async
|
||
public void syncAll() {
|
||
try {
|
||
int n = signKeyCacheService.refreshAll();
|
||
log.info("[SignKeyCache] daily sync-all done, synced apps={}", n);
|
||
} catch (Exception e) {
|
||
log.warn("[SignKeyCache] daily sync-all failed: {}", e.getMessage());
|
||
}
|
||
}
|
||
}
|