feat: 添加数据库集成、定时任务调度器和事件Hook体系
- 新增数据库配置项(DB_TYPE, DB_HOST, DB_PORT等),支持MySQL和PostgreSQL - 集成GORM实现数据库连接和自动迁移功能 - 添加定时任务调度器(cmd/scheduler),基于robfig/cron实现秒级调度 - 实现事件Hook体系,支持同步/异步处理和优先级排序 - 更新构建脚本,编译server、cli、scheduler三个二进制文件 - 配置systemd服务管理定时任务调度器 - 重构项目结构,新增crontab和hooks目录模块 - 更新README文档,完善各组件使用说明和部署配置
This commit is contained in:
+61
-2
@@ -70,8 +70,10 @@ func (f *colorFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
}
|
||||
|
||||
var (
|
||||
instance *logrus.Logger
|
||||
once sync.Once
|
||||
instance *logrus.Logger
|
||||
once sync.Once
|
||||
cronInstance *logrus.Logger
|
||||
cronOnce sync.Once
|
||||
)
|
||||
|
||||
func GetLogger() *logrus.Logger {
|
||||
@@ -150,6 +152,63 @@ func joinToString(parts ...interface{}) string {
|
||||
return strings.Join(strs, " ")
|
||||
}
|
||||
|
||||
// GetCronLogger 返回定时任务专用 logger,写入独立日志文件
|
||||
func GetCronLogger() *logrus.Logger {
|
||||
cronOnce.Do(func() {
|
||||
cronInstance = logrus.New()
|
||||
|
||||
switch strings.ToLower(os.Getenv("LOG_LEVEL")) {
|
||||
case "debug":
|
||||
cronInstance.SetLevel(logrus.DebugLevel)
|
||||
case "info":
|
||||
cronInstance.SetLevel(logrus.InfoLevel)
|
||||
case "warn":
|
||||
cronInstance.SetLevel(logrus.WarnLevel)
|
||||
case "error":
|
||||
cronInstance.SetLevel(logrus.ErrorLevel)
|
||||
default:
|
||||
cronInstance.SetLevel(logrus.InfoLevel)
|
||||
}
|
||||
|
||||
cronInstance.SetFormatter(&colorFormatter{})
|
||||
cronInstance.SetReportCaller(true)
|
||||
|
||||
logDir := os.Getenv("LOG_SAVE_PATH")
|
||||
if logDir == "" {
|
||||
logDir = "./logs"
|
||||
}
|
||||
if err := os.MkdirAll(logDir, 0755); err != nil {
|
||||
cronInstance.SetOutput(os.Stdout)
|
||||
return
|
||||
}
|
||||
|
||||
logFile := logDir + "/cron_" + time.Now().Format("20060102") + ".log"
|
||||
f, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
cronInstance.SetOutput(os.Stdout)
|
||||
return
|
||||
}
|
||||
cronInstance.SetOutput(io.MultiWriter(os.Stdout, f))
|
||||
})
|
||||
return cronInstance
|
||||
}
|
||||
|
||||
func CronDebug(title string, content ...interface{}) {
|
||||
GetCronLogger().WithField("title", title).Debug(joinToString(content...))
|
||||
}
|
||||
|
||||
func CronInfo(title string, content ...interface{}) {
|
||||
GetCronLogger().WithField("title", title).Info(joinToString(content...))
|
||||
}
|
||||
|
||||
func CronWarn(title string, content ...interface{}) {
|
||||
GetCronLogger().WithField("title", title).Warn(joinToString(content...))
|
||||
}
|
||||
|
||||
func CronError(title string, content ...interface{}) {
|
||||
GetCronLogger().WithField("title", title).Error(joinToString(content...))
|
||||
}
|
||||
|
||||
func Debug(title string, content ...interface{}) {
|
||||
GetLogger().WithField("title", title).Debug(joinToString(content...))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user