mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-06 07:36:37 +08:00
refactor(core): align with cordis spatiotemporal composability architecture
- Purify core micro-kernel by removing context hardcoded helpers and reverse dependencies - Eliminate init() side effects in infra plugins with reversible lifecycle disposal - Completely isolate plugins by removing cross-plugin imports and using core/contracts - Introduce TaskService and RiskControlService contracts for unified cross-plugin APIs - Regenerate Swagger documentation and update developer guide matrix - Achieve 0 violations in check_cordis_architecture.sh and 100% test pass
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package driver_asynq_cron
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"Wavelet/core"
|
||||
"Wavelet/core/contracts"
|
||||
)
|
||||
|
||||
var (
|
||||
dbMu sync.RWMutex
|
||||
dbSvc contracts.DBService
|
||||
)
|
||||
|
||||
func setDBService(s contracts.DBService) {
|
||||
dbMu.Lock()
|
||||
defer dbMu.Unlock()
|
||||
dbSvc = s
|
||||
}
|
||||
|
||||
func getDB(ctx context.Context) *gorm.DB {
|
||||
if c, ok := ctx.(*core.Context); ok && c != nil {
|
||||
if s, err := core.Inject[contracts.DBService](c); err == nil && s != nil {
|
||||
return s.DB(ctx)
|
||||
}
|
||||
}
|
||||
dbMu.RLock()
|
||||
s := dbSvc
|
||||
dbMu.RUnlock()
|
||||
if s != nil {
|
||||
return s.DB(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/hibiken/asynq"
|
||||
|
||||
"Wavelet/core"
|
||||
"Wavelet/plugins/drivers/driver_asynq_worker"
|
||||
"Wavelet/core/contracts"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
@@ -66,7 +66,7 @@ type Plugin struct {
|
||||
// New creates a new Asynq Cron Scheduler driver plugin.
|
||||
func New(opts ...Option) *Plugin {
|
||||
p := &Plugin{
|
||||
redisOpt: driver_asynq_worker.RedisOpt,
|
||||
redisOpt: RedisOpt,
|
||||
location: time.Local,
|
||||
}
|
||||
|
||||
@@ -90,6 +90,30 @@ func (p *Plugin) Apply(ctx *core.Context) error {
|
||||
p.coreCtx = ctx
|
||||
p.mu.Unlock()
|
||||
|
||||
// Bind DBService
|
||||
if db, err := core.Inject[contracts.DBService](ctx); err == nil && db != nil {
|
||||
setDBService(db)
|
||||
} else {
|
||||
core.When[contracts.DBService](ctx, func(db contracts.DBService) {
|
||||
setDBService(db)
|
||||
})
|
||||
}
|
||||
|
||||
// Bind TaskService
|
||||
if taskSvc, err := core.Inject[contracts.TaskService](ctx); err == nil && taskSvc != nil {
|
||||
setTaskService(taskSvc)
|
||||
} else {
|
||||
core.When[contracts.TaskService](ctx, func(taskSvc contracts.TaskService) {
|
||||
setTaskService(taskSvc)
|
||||
})
|
||||
}
|
||||
|
||||
ctx.OnDispose(func() error {
|
||||
setDBService(nil)
|
||||
setTaskService(nil)
|
||||
return nil
|
||||
})
|
||||
|
||||
// Register migrations for w_schedules table
|
||||
ctx.Migrations().Register("driver_asynq_cron", cronMigrations)
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ package driver_asynq_cron
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
db "Wavelet/plugins/infra/database"
|
||||
)
|
||||
|
||||
// Schedule 定时任务配置表
|
||||
@@ -30,7 +28,11 @@ func (Schedule) TableName() string {
|
||||
// ListActiveSchedules 查询所有已启用的定时任务配置
|
||||
func ListActiveSchedules(ctx context.Context) ([]Schedule, error) {
|
||||
var schedules []Schedule
|
||||
if err := db.DB(ctx).Where("is_active = ?", true).Find(&schedules).Error; err != nil {
|
||||
db := getDB(ctx)
|
||||
if db == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if err := db.Where("is_active = ?", true).Find(&schedules).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return schedules, nil
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
|
||||
"Wavelet/core/contracts"
|
||||
"Wavelet/pkg/logger"
|
||||
"Wavelet/plugins/drivers/driver_asynq_worker"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -22,11 +22,22 @@ var (
|
||||
schedulerMutex sync.Mutex
|
||||
quitChan chan struct{}
|
||||
schedulerOnce sync.Once
|
||||
taskSvcMu sync.RWMutex
|
||||
taskSvcInstance contracts.TaskService
|
||||
// RedisOpt is the Redis connection option for the scheduler.
|
||||
RedisOpt asynq.RedisConnOpt
|
||||
)
|
||||
|
||||
// GetAsynqClient 获取全局 AsynqClient
|
||||
func GetAsynqClient() *asynq.Client {
|
||||
return driver_asynq_worker.AsynqClient
|
||||
func setTaskService(s contracts.TaskService) {
|
||||
taskSvcMu.Lock()
|
||||
defer taskSvcMu.Unlock()
|
||||
taskSvcInstance = s
|
||||
}
|
||||
|
||||
func getTaskService() contracts.TaskService {
|
||||
taskSvcMu.RLock()
|
||||
defer taskSvcMu.RUnlock()
|
||||
return taskSvcInstance
|
||||
}
|
||||
|
||||
// StartScheduler 启动调度器 (该函数阻塞,直到调度器退出)
|
||||
@@ -92,27 +103,39 @@ func ReloadScheduler() error {
|
||||
|
||||
// 3. 实例化新的调度器
|
||||
newScheduler := asynq.NewScheduler(
|
||||
driver_asynq_worker.RedisOpt,
|
||||
RedisOpt,
|
||||
&asynq.SchedulerOpts{
|
||||
Location: location,
|
||||
},
|
||||
)
|
||||
|
||||
// 4. 遍历并注册任务
|
||||
taskSvc := getTaskService()
|
||||
for _, s := range schedules {
|
||||
meta := driver_asynq_worker.GetTaskMeta(s.TaskType)
|
||||
if meta == nil {
|
||||
continue // 忽略排程配置中无效的任务类型
|
||||
taskName := s.TaskType
|
||||
maxRetry := 3
|
||||
queue := "default"
|
||||
|
||||
if taskSvc != nil {
|
||||
if meta, ok := taskSvc.GetTaskMeta(s.TaskType); ok {
|
||||
taskName = meta.Name
|
||||
if meta.MaxRetry > 0 {
|
||||
maxRetry = meta.MaxRetry
|
||||
}
|
||||
if meta.Queue != "" {
|
||||
queue = meta.Queue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 构造 Asynq 载荷。定时任务使用对应 Meta 中的 Asynq 标识,同时将数据库中保存的 json 作为参数
|
||||
t := asynq.NewTask(meta.AsynqTask, []byte(s.Payload))
|
||||
// 构造 Asynq 载荷
|
||||
t := asynq.NewTask(taskName, []byte(s.Payload))
|
||||
|
||||
if _, err := newScheduler.Register(
|
||||
s.Cron,
|
||||
t,
|
||||
asynq.MaxRetry(meta.MaxRetry),
|
||||
asynq.Queue(meta.Queue),
|
||||
asynq.MaxRetry(maxRetry),
|
||||
asynq.Queue(queue),
|
||||
); err != nil {
|
||||
// 定时任务配置可能有误(如 Cron 格式不被 Asynq 识别),记录日志并跳过
|
||||
logger.ErrorF(context.Background(), "[Scheduler] 注册定时任务失败 id=%d name=%s: %v", s.ID, s.Name, err)
|
||||
|
||||
Reference in New Issue
Block a user