diff --git a/cmd/server/main.go b/cmd/server/main.go index 5dee426..7bfec2b 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,7 +5,6 @@ import ( "log" "os" "path/filepath" - "time" "accounting-app/internal/cache" "accounting-app/internal/config" @@ -125,8 +124,8 @@ func main() { ctx, redisClient.Client(), db, - 5*time.Second, // Poll interval: 检查延迟任务的间隔 - 2, // Worker count: 并发处理任务的数量 + cfg.MQPollInterval, // Poll interval from config + cfg.MQWorkerCount, // Worker count from config ) if err != nil { log.Printf("Warning: Failed to initialize recurring task system: %v", err) diff --git a/internal/config/config.go b/internal/config/config.go index 17db8b5..869631f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -58,6 +58,10 @@ type Config struct { MaxImageSize int64 AllowedImageTypes string MaxImagesPerTx int + + // Task Queue configuration + MQWorkerCount int + MQPollInterval time.Duration } // Load loads configuration from environment variables @@ -113,6 +117,10 @@ func Load() *Config { MaxImageSize: getEnvInt64("MAX_IMAGE_SIZE", 10*1024*1024), // 10MB AllowedImageTypes: getEnv("ALLOWED_IMAGE_TYPES", "image/jpeg,image/png,image/heic"), MaxImagesPerTx: getEnvInt("MAX_IMAGES_PER_TX", 9), + + // Task Queue + MQWorkerCount: getEnvInt("MQ_WORKER_COUNT", 2), + MQPollInterval: getEnvDuration("MQ_POLL_INTERVAL", 5*time.Second), } // Ensure data directory exists diff --git a/internal/mq/logger.go b/internal/mq/logger.go new file mode 100644 index 0000000..32835b1 --- /dev/null +++ b/internal/mq/logger.go @@ -0,0 +1,67 @@ +package mq + +import ( + "fmt" + "log" +) + +// LogLevel 日志级别 +type LogLevel string + +const ( + LevelInfo LogLevel = "INFO" + LevelError LogLevel = "ERROR" + LevelWarn LogLevel = "WARN" +) + +// Logger 简单的结构化日志封装 +// 格式: [Component] [Level] Message key1=value1 key2=value2 +type Logger struct { + Component string +} + +func NewLogger(component string) *Logger { + return &Logger{Component: component} +} + +func (l *Logger) Info(msg string, keysAndValues ...interface{}) { + l.log(LevelInfo, msg, keysAndValues...) +} + +func (l *Logger) Warn(msg string, keysAndValues ...interface{}) { + l.log(LevelWarn, msg, keysAndValues...) +} + +func (l *Logger) Error(msg string, keysAndValues ...interface{}) { + l.log(LevelError, msg, keysAndValues...) +} + +func (l *Logger) log(level LogLevel, msg string, args ...interface{}) { + // 构建 key=value 字符串 + var kvStr string + for i := 0; i < len(args); i += 2 { + key := args[i] + var val interface{} = "" + if i+1 < len(args) { + val = args[i+1] + } + if kvStr != "" { + kvStr += " " + } + kvStr += logFormatKV(key, val) + } + + if kvStr != "" { + log.Printf("[%s] [%s] %s %s", l.Component, level, msg, kvStr) + } else { + log.Printf("[%s] [%s] %s", l.Component, level, msg) + } +} + +func logFormatKV(key, val interface{}) string { + return logStr(key) + "=" + logStr(val) +} + +func logStr(v interface{}) string { + return fmt.Sprint(v) +}