47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"kra/pkg/database/migration"
|
|
platformmodule "kra/pkg/module"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Migrations() []migration.Step {
|
|
return []migration.Step{{
|
|
ID: "202608220001_task_schema",
|
|
Migrate: func(db *gorm.DB) error {
|
|
return migration.CreateMissingTables(db, &TaskPO{}, &TaskLogPO{})
|
|
},
|
|
}}
|
|
}
|
|
|
|
// SeedDefaults inserts module-contributed timed tasks without making the
|
|
// system data package own task table shapes.
|
|
func SeedDefaults(ctx context.Context, db *gorm.DB, defaults []platformmodule.TimedTask) error {
|
|
if db == nil {
|
|
return nil
|
|
}
|
|
if len(defaults) == 0 {
|
|
defaults = []platformmodule.TimedTask{
|
|
{Name: "ClearDB", Description: "定时清理数据库过期日志", Spec: "@daily", MethodName: "ClearDB", Enabled: true},
|
|
{Name: "CleanStaleUploads", Description: "定时清理过期大文件上传会话", Spec: "@hourly", MethodName: "CleanStaleUploads", Enabled: true},
|
|
}
|
|
}
|
|
return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
for _, item := range defaults {
|
|
if strings.TrimSpace(item.Name) == "" {
|
|
continue
|
|
}
|
|
row := TaskPO{Name: item.Name, Description: item.Description, Spec: item.Spec, WithSeconds: item.WithSeconds, ExecutorType: "method", MethodName: item.MethodName, Enabled: item.Enabled}
|
|
if err := tx.Where("name = ?", row.Name).FirstOrCreate(&row).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|