From c35ff96397bae30c46197a915c9e7d50368b2caf Mon Sep 17 00:00:00 2001 From: DullJZ <79080562+DullJZ@users.noreply.github.com> Date: Mon, 3 Nov 2025 22:38:09 +0800 Subject: [PATCH] Fix: Prevent duplicate archiving with idempotency tracking --- internal/scheduler/monthly_archiver.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/scheduler/monthly_archiver.go b/internal/scheduler/monthly_archiver.go index 2302b9c..82c2698 100644 --- a/internal/scheduler/monthly_archiver.go +++ b/internal/scheduler/monthly_archiver.go @@ -9,9 +9,10 @@ import ( // MonthlyArchiver 月度统计归档器 type MonthlyArchiver struct { - storage *storage.Service - ticker *time.Ticker - stopChan chan struct{} + storage *storage.Service + ticker *time.Ticker + stopChan chan struct{} + lastArchivedDate string // 格式: "2025-01" - 记录上次归档的月份 } // NewMonthlyArchiver 创建月度归档器 @@ -52,10 +53,13 @@ func (m *MonthlyArchiver) Stop() { // checkAndArchive 检查并归档统计数据 func (m *MonthlyArchiver) checkAndArchive() { now := time.Now() + lastMonth := now.AddDate(0, -1, 0) + lastMonthKey := lastMonth.Format("2006-01") - // 如果是每月的第一天凌晨,归档上个月的数据 - if now.Day() == 1 && now.Hour() < 1 { + // 如果是每月的第一天,且上个月还未归档,则归档上个月的数据 + if now.Day() == 1 && m.lastArchivedDate != lastMonthKey { m.archiveLastMonth() + m.lastArchivedDate = lastMonthKey } // 每天都归档当前月份(实时更新)