优化结构

This commit is contained in:
Yvan
2026-08-24 13:25:16 +08:00
parent 7a9a738c6a
commit 0b6b8ddf1a
72 changed files with 2475 additions and 1443 deletions
+2 -5
View File
@@ -9,6 +9,7 @@ import (
"time"
"github.com/robfig/cron/v3"
platformtask "kra/pkg/task"
)
type TimedTask struct {
@@ -77,13 +78,9 @@ type TaskUsecase struct {
methods TaskMethodRegistry
}
func NewTaskUsecase(repo TaskRepo) *TaskUsecase {
return NewTaskUsecaseWithRegistry(repo, DefaultTaskMethodRegistry())
}
func NewTaskUsecaseWithRegistry(repo TaskRepo, methods TaskMethodRegistry) *TaskUsecase {
if methods == nil {
methods = DefaultTaskMethodRegistry()
methods = platformtask.NewRegistry()
}
return &TaskUsecase{TaskRepo: repo, methods: methods}
}
-17
View File
@@ -6,23 +6,6 @@ type TaskMethodFunc = platformtask.MethodFunc
type TaskMethod = platformtask.Method
type TaskMethodRegistry interface {
Register(platformtask.Method)
Lookup(string) (TaskMethodFunc, bool)
List() []TaskMethod
}
var defaultTaskMethods = platformtask.NewRegistry()
// RegisterTaskMethod remains as a compatibility helper for tests and callers
// that have not moved to constructor injection yet.
func RegisterTaskMethod(name, description string, fn TaskMethodFunc) {
defaultTaskMethods.Register(platformtask.Method{Name: name, Description: description, Run: fn})
}
func TaskMethodByName(name string) (TaskMethodFunc, bool) {
return defaultTaskMethods.Lookup(name)
}
func RegisteredTaskMethods() []TaskMethod { return defaultTaskMethods.List() }
func DefaultTaskMethodRegistry() TaskMethodRegistry { return defaultTaskMethods }
+9 -6
View File
@@ -4,11 +4,14 @@ import (
"context"
"encoding/json"
"errors"
platformtask "kra/pkg/task"
"sync"
"testing"
"time"
)
var testTaskMethods = platformtask.NewRegistry()
type applicationTaskRepo struct {
mu sync.Mutex
deleted bool
@@ -112,14 +115,14 @@ func (r *applicationTaskRuntime) Unsubscribe(uint, chan []byte) {}
func validApplicationTask(id uint) *TimedTask {
const methodName = "biz-test-application-task"
RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error { return nil })
testTaskMethods.Register(platformtask.Method{Name: methodName, Description: "test", Run: func(context.Context, json.RawMessage) error { return nil }})
return &TimedTask{ID: id, Name: "test", Spec: "0 0 * * *", ExecutorType: TaskExecutorMethod, MethodName: methodName, Enabled: true}
}
func TestTaskApplicationRepairsRuntimeAfterScheduleFailure(t *testing.T) {
repo := &applicationTaskRepo{createdID: 41}
runtime := &applicationTaskRuntime{scheduleErr: errors.New("schedule failed")}
uc := NewTaskApplicationUsecase(NewTaskUsecase(repo), runtime)
uc := NewTaskApplicationUsecase(NewTaskUsecaseWithRegistry(repo, testTaskMethods), runtime)
ctx, cancel := context.WithCancel(context.Background())
cancel()
@@ -137,7 +140,7 @@ func TestTaskApplicationRepairsRuntimeAfterScheduleFailure(t *testing.T) {
func TestTaskApplicationReturnsScheduleErrorWhenRepairFails(t *testing.T) {
repo := &applicationTaskRepo{createdID: 42}
runtime := &applicationTaskRuntime{scheduleErr: errors.New("schedule failed"), reloadErr: errors.New("reload failed")}
uc := NewTaskApplicationUsecase(NewTaskUsecase(repo), runtime)
uc := NewTaskApplicationUsecase(NewTaskUsecaseWithRegistry(repo, testTaskMethods), runtime)
err := uc.Create(context.Background(), validApplicationTask(0))
if !errors.Is(err, ErrTaskSchedule) {
@@ -158,7 +161,7 @@ func TestTaskApplicationUpdateAndToggleUseRuntimeRepair(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
repo := &applicationTaskRepo{}
runtime := &applicationTaskRuntime{scheduleErr: errors.New("schedule failed")}
uc := NewTaskApplicationUsecase(NewTaskUsecase(repo), runtime)
uc := NewTaskApplicationUsecase(NewTaskUsecaseWithRegistry(repo, testTaskMethods), runtime)
if err := test.run(uc); err != nil {
t.Fatalf("operation error = %v", err)
}
@@ -172,7 +175,7 @@ func TestTaskApplicationUpdateAndToggleUseRuntimeRepair(t *testing.T) {
func TestTaskApplicationDeleteRemovesRuntimeAfterDatabase(t *testing.T) {
repo := &applicationTaskRepo{}
runtime := &applicationTaskRuntime{repo: repo}
uc := NewTaskApplicationUsecase(NewTaskUsecase(repo), runtime)
uc := NewTaskApplicationUsecase(NewTaskUsecaseWithRegistry(repo, testTaskMethods), runtime)
if err := uc.Delete(context.Background(), 8); err != nil {
t.Fatalf("Delete() error = %v", err)
}
@@ -187,7 +190,7 @@ func TestTaskApplicationDeleteRemovesRuntimeAfterDatabase(t *testing.T) {
func TestTaskApplicationDeleteKeepsRuntimeWhenDatabaseFails(t *testing.T) {
repo := &applicationTaskRepo{deleteErr: errors.New("delete failed")}
runtime := &applicationTaskRuntime{repo: repo}
uc := NewTaskApplicationUsecase(NewTaskUsecase(repo), runtime)
uc := NewTaskApplicationUsecase(NewTaskUsecaseWithRegistry(repo, testTaskMethods), runtime)
if err := uc.Delete(context.Background(), 8); err == nil {
t.Fatal("Delete() error = nil, want database error")
}