feat: 实现基于 Redis 的延迟任务队列和工作器,支持周期性交易处理等后台任务调度。

This commit is contained in:
2026-01-28 16:36:56 +08:00
parent a9ee8856ba
commit 5ff680ee43
6 changed files with 884 additions and 0 deletions

View File

@@ -5,10 +5,12 @@ import (
"log"
"os"
"path/filepath"
"time"
"accounting-app/internal/cache"
"accounting-app/internal/config"
"accounting-app/internal/database"
"accounting-app/internal/mq"
"accounting-app/internal/repository"
"accounting-app/internal/router"
"accounting-app/internal/service"
@@ -97,6 +99,7 @@ func main() {
if err != nil {
// Redis not available - fall back to old system
log.Printf("Warning: Redis connection failed (%v), falling back to non-cached exchange rate system", err)
log.Printf("Warning: Recurring transaction MQ system disabled (requires Redis)")
// Use old scheduler
scheduler := service.NewExchangeRateScheduler(yunAPIClient, cfg.SyncInterval)
@@ -116,6 +119,22 @@ func main() {
// Start the new SyncScheduler in background
// This will perform initial sync immediately (Requirement 3.1)
go syncScheduler.Start(ctx)
// Initialize and start MQ task system for recurring transactions
recurringTaskSystem, err := mq.InitRecurringTaskSystem(
ctx,
redisClient.Client(),
db,
5*time.Second, // Poll interval: 检查延迟任务的间隔
2, // Worker count: 并发处理任务的数量
)
if err != nil {
log.Printf("Warning: Failed to initialize recurring task system: %v", err)
} else {
// Start MQ worker in background
go recurringTaskSystem.Start(ctx)
log.Println("Recurring transaction MQ system started")
}
}
// Get port from config or environment