|
|
@@ -28,15 +28,15 @@ export class RisSyncService {
|
|
|
|
|
|
this.isRunning = true;
|
|
|
console.log('[RisSyncService] 自动同步服务启动');
|
|
|
-
|
|
|
+
|
|
|
// 立即执行一次同步
|
|
|
await this.performSync();
|
|
|
-
|
|
|
+
|
|
|
// 设置定时器
|
|
|
this.timer = setInterval(() => {
|
|
|
this.performSync();
|
|
|
}, this.options.interval);
|
|
|
-
|
|
|
+
|
|
|
console.log(`[RisSyncService] 定时器已设置,间隔: ${this.options.interval}ms (${this.options.interval / 60000}分钟)`);
|
|
|
}
|
|
|
|
|
|
@@ -59,42 +59,44 @@ export class RisSyncService {
|
|
|
private async performSync() {
|
|
|
try {
|
|
|
console.log('[RisSyncService] ========== 开始同步RIS数据 ==========');
|
|
|
-
|
|
|
+
|
|
|
// 通知开始同步
|
|
|
if (this.options.onSyncStart) {
|
|
|
this.options.onSyncStart();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 获取今天的时间范围
|
|
|
const timeRange = RisTimeUtils.getTodayRange();
|
|
|
console.log('[RisSyncService] 同步时间范围:', {
|
|
|
start: timeRange.start,
|
|
|
end: timeRange.end
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
// 构建同步请求参数
|
|
|
const syncParams: RisSyncRequest = {
|
|
|
start_time: timeRange.start,
|
|
|
end_time: timeRange.end,
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 执行同步
|
|
|
const result = await syncRis(syncParams);
|
|
|
-
|
|
|
+
|
|
|
console.log(`[RisSyncService] ✅ 同步成功,共同步 ${result.data.count} 条数据`);
|
|
|
-
|
|
|
+
|
|
|
// 重置重试计数
|
|
|
this.retryCount = 0;
|
|
|
-
|
|
|
+
|
|
|
// 回调通知
|
|
|
if (this.options.onSyncComplete) {
|
|
|
this.options.onSyncComplete(true, result.data.count);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
console.log('[RisSyncService] ========== 同步完成 ==========');
|
|
|
} catch (error) {
|
|
|
console.error('[RisSyncService] ❌ 同步失败:', error);
|
|
|
-
|
|
|
+ if (this.options.onSyncComplete) {
|
|
|
+ this.options.onSyncComplete(false, 0);
|
|
|
+ }
|
|
|
// 错误重试逻辑
|
|
|
this.handleSyncError(error);
|
|
|
}
|
|
|
@@ -105,11 +107,11 @@ export class RisSyncService {
|
|
|
*/
|
|
|
private async handleSyncError(error: any) {
|
|
|
this.retryCount++;
|
|
|
-
|
|
|
+
|
|
|
if (this.retryCount <= this.maxRetries) {
|
|
|
const retryDelay = this.calculateRetryDelay(this.retryCount);
|
|
|
console.log(`[RisSyncService] 将在 ${retryDelay / 1000} 秒后重试 (第 ${this.retryCount}/${this.maxRetries} 次)`);
|
|
|
-
|
|
|
+
|
|
|
// 延迟后重试
|
|
|
setTimeout(() => {
|
|
|
if (this.isRunning) {
|
|
|
@@ -119,7 +121,7 @@ export class RisSyncService {
|
|
|
} else {
|
|
|
console.error('[RisSyncService] 已达到最大重试次数,等待下次定时同步');
|
|
|
this.retryCount = 0;
|
|
|
-
|
|
|
+
|
|
|
// 回调通知失败
|
|
|
if (this.options.onSyncComplete) {
|
|
|
this.options.onSyncComplete(false, 0);
|