优化结构
This commit is contained in:
@@ -23,9 +23,6 @@ type TaskExecutor struct {
|
||||
}
|
||||
|
||||
func NewTaskExecutorWithRegistry(tasks *taskbiz.TaskUsecase, methods taskbiz.TaskMethodRegistry) *TaskExecutor {
|
||||
if methods == nil {
|
||||
methods = taskbiz.DefaultTaskMethodRegistry()
|
||||
}
|
||||
return &TaskExecutor{tasks: tasks, methods: methods}
|
||||
}
|
||||
|
||||
@@ -139,8 +136,6 @@ func (e *TaskExecutor) runMethod(ctx context.Context, task *taskbiz.TimedTask) e
|
||||
var ok bool
|
||||
if e.methods != nil {
|
||||
method, ok = e.methods.Lookup(task.MethodName)
|
||||
} else {
|
||||
method, ok = taskbiz.TaskMethodByName(task.MethodName)
|
||||
}
|
||||
if !ok {
|
||||
return fmt.Errorf("方法 %s 未注册(需通过 platform/task.Registry 注册)", task.MethodName)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
taskbiz "kra/internal/biz/task"
|
||||
platformtask "kra/pkg/task"
|
||||
)
|
||||
|
||||
func TestPrivateIP(t *testing.T) {
|
||||
@@ -25,11 +26,12 @@ func TestPrivateIP(t *testing.T) {
|
||||
|
||||
func TestRunMethodReportsParentDeadlineAsTimeout(t *testing.T) {
|
||||
const methodName = "worker-test-parent-deadline"
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(ctx context.Context, _ json.RawMessage) error {
|
||||
methods := platformtask.NewRegistry()
|
||||
methods.Register(platformtask.Method{Name: methodName, Description: "test", Run: func(ctx context.Context, _ json.RawMessage) error {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
})
|
||||
executor := &TaskExecutor{}
|
||||
}})
|
||||
executor := &TaskExecutor{methods: methods}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := executor.runMethod(ctx, &taskbiz.TimedTask{MethodName: methodName}); !errors.Is(err, errTaskTimeout) {
|
||||
@@ -39,10 +41,11 @@ func TestRunMethodReportsParentDeadlineAsTimeout(t *testing.T) {
|
||||
|
||||
func TestRunMethodConvertsPanicToError(t *testing.T) {
|
||||
const methodName = "worker-test-panic"
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
methods := platformtask.NewRegistry()
|
||||
methods.Register(platformtask.Method{Name: methodName, Description: "test", Run: func(context.Context, json.RawMessage) error {
|
||||
panic("boom")
|
||||
})
|
||||
executor := &TaskExecutor{}
|
||||
}})
|
||||
executor := &TaskExecutor{methods: methods}
|
||||
err := executor.runMethod(context.Background(), &taskbiz.TimedTask{MethodName: methodName})
|
||||
if err == nil || !strings.Contains(err.Error(), "panic: boom") {
|
||||
t.Fatalf("runMethod() error = %v, want recovered panic", err)
|
||||
@@ -52,12 +55,13 @@ func TestRunMethodConvertsPanicToError(t *testing.T) {
|
||||
func TestRunMethodUsesParentContext(t *testing.T) {
|
||||
const methodName = "worker-test-parent-context"
|
||||
started := make(chan struct{})
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(ctx context.Context, _ json.RawMessage) error {
|
||||
methods := platformtask.NewRegistry()
|
||||
methods.Register(platformtask.Method{Name: methodName, Description: "test", Run: func(ctx context.Context, _ json.RawMessage) error {
|
||||
close(started)
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
})
|
||||
executor := &TaskExecutor{}
|
||||
}})
|
||||
executor := &TaskExecutor{methods: methods}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
@@ -82,11 +86,12 @@ func TestRunMethodUsesParentContext(t *testing.T) {
|
||||
func TestRunMethodHonorsDeadlineWhenMethodIgnoresContext(t *testing.T) {
|
||||
const methodName = "worker-test-ignores-context"
|
||||
release := make(chan struct{})
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
methods := platformtask.NewRegistry()
|
||||
methods.Register(platformtask.Method{Name: methodName, Description: "test", Run: func(context.Context, json.RawMessage) error {
|
||||
<-release
|
||||
return nil
|
||||
})
|
||||
executor := &TaskExecutor{}
|
||||
}})
|
||||
executor := &TaskExecutor{methods: methods}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := executor.runMethod(ctx, &taskbiz.TimedTask{MethodName: methodName}); !errors.Is(err, errTaskTimeout) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"kra/internal/biz/system"
|
||||
taskbiz "kra/internal/biz/task"
|
||||
|
||||
"kra/internal/conf"
|
||||
"kra/internal/config"
|
||||
platformtask "kra/pkg/task"
|
||||
)
|
||||
|
||||
@@ -17,10 +17,10 @@ type TaskMethods struct {
|
||||
tasks *taskbiz.TaskUsecase
|
||||
maintenance *system.MaintenanceUsecase
|
||||
media *system.MediaUsecase
|
||||
runtime *conf.Runtime
|
||||
runtime *config.Store
|
||||
}
|
||||
|
||||
func NewTaskMethods(tasks *taskbiz.TaskUsecase, maintenance *system.MaintenanceUsecase, media *system.MediaUsecase, runtime *conf.Runtime) *TaskMethods {
|
||||
func NewTaskMethods(tasks *taskbiz.TaskUsecase, maintenance *system.MaintenanceUsecase, media *system.MediaUsecase, runtime *config.Store) *TaskMethods {
|
||||
return &TaskMethods{tasks: tasks, maintenance: maintenance, media: media, runtime: runtime}
|
||||
}
|
||||
|
||||
@@ -53,12 +53,14 @@ func (methods *TaskMethods) RegisterTasks(registry *platformtask.Registry) {
|
||||
return errors.New("CleanStaleUploads 任务依赖未就绪")
|
||||
}
|
||||
ttl := 24
|
||||
var config *conf.AdminBackend
|
||||
var admin *config.Admin
|
||||
if methods.runtime != nil {
|
||||
config = methods.runtime.Admin()
|
||||
if snapshot := methods.runtime.Snapshot(); snapshot != nil {
|
||||
admin = snapshot.Admin
|
||||
}
|
||||
}
|
||||
if config != nil && config.Media != nil && config.Media.SessionTtl > 0 {
|
||||
ttl = int(config.Media.SessionTtl)
|
||||
if admin != nil && admin.Media != nil && admin.Media.SessionTTL > 0 {
|
||||
ttl = admin.Media.SessionTTL
|
||||
}
|
||||
return methods.media.CleanupStale(ctx, ttl)
|
||||
},
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
taskbiz "kra/internal/biz/task"
|
||||
platformtask "kra/pkg/task"
|
||||
)
|
||||
|
||||
type workerTaskRepo struct {
|
||||
@@ -63,8 +64,9 @@ func (r *workerTaskRepo) ListTaskLogs(context.Context, int, int, uint, string) (
|
||||
}
|
||||
|
||||
func newTestTaskScheduler(repo *workerTaskRepo) *TaskScheduler {
|
||||
tasks := taskbiz.NewTaskUsecase(repo)
|
||||
executor := &TaskExecutor{tasks: tasks}
|
||||
methods := platformtask.NewRegistry()
|
||||
tasks := taskbiz.NewTaskUsecaseWithRegistry(repo, methods)
|
||||
executor := &TaskExecutor{tasks: tasks, methods: methods}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
return NewTaskScheduler(tasks, nil, executor, logger)
|
||||
}
|
||||
@@ -136,16 +138,18 @@ func TestManualTriggerSkipsOverlappingExecution(t *testing.T) {
|
||||
started := make(chan struct{}, 2)
|
||||
release := make(chan struct{})
|
||||
var calls atomic.Int32
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
methods := platformtask.NewRegistry()
|
||||
methods.Register(platformtask.Method{Name: methodName, Description: "test", Run: func(context.Context, json.RawMessage) error {
|
||||
calls.Add(1)
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return nil
|
||||
})
|
||||
}})
|
||||
|
||||
task := &taskbiz.TimedTask{ID: 9, Name: "single", ExecutorType: taskbiz.TaskExecutorMethod, MethodName: methodName}
|
||||
repo := &workerTaskRepo{items: []*taskbiz.TimedTask{task}, logs: make(chan *taskbiz.TimedTaskLog, 2)}
|
||||
scheduler := newTestTaskScheduler(repo)
|
||||
tasks := taskbiz.NewTaskUsecaseWithRegistry(repo, methods)
|
||||
scheduler := NewTaskScheduler(tasks, nil, &TaskExecutor{tasks: tasks, methods: methods}, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
if err := scheduler.TriggerID(context.Background(), task.ID); err != nil {
|
||||
t.Fatalf("first manual trigger failed: %v", err)
|
||||
}
|
||||
@@ -193,16 +197,18 @@ func TestAutomaticAndManualTriggersShareOverlapGate(t *testing.T) {
|
||||
started := make(chan struct{}, 1)
|
||||
release := make(chan struct{})
|
||||
var calls atomic.Int32
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
methods := platformtask.NewRegistry()
|
||||
methods.Register(platformtask.Method{Name: methodName, Description: "test", Run: func(context.Context, json.RawMessage) error {
|
||||
calls.Add(1)
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return nil
|
||||
})
|
||||
}})
|
||||
|
||||
task := &taskbiz.TimedTask{ID: 10, Name: "shared-gate", ExecutorType: taskbiz.TaskExecutorMethod, MethodName: methodName}
|
||||
repo := &workerTaskRepo{logs: make(chan *taskbiz.TimedTaskLog, 1)}
|
||||
scheduler := newTestTaskScheduler(repo)
|
||||
tasks := taskbiz.NewTaskUsecaseWithRegistry(repo, methods)
|
||||
scheduler := NewTaskScheduler(tasks, nil, &TaskExecutor{tasks: tasks, methods: methods}, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
if !scheduler.dispatch(task, "auto", true) {
|
||||
t.Fatal("automatic dispatch was rejected while idle")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user