优化结构

This commit is contained in:
Yvan
2026-08-22 12:13:16 +08:00
parent d987244d0e
commit b27a66e9d3
200 changed files with 1794 additions and 1715 deletions
+23 -24
View File
@@ -5,31 +5,30 @@ import (
"encoding/json"
"errors"
"io"
"kra/internal/biz/system"
"log/slog"
"sync"
"sync/atomic"
"testing"
"time"
"kra/internal/biz"
)
type workerTaskRepo struct {
mu sync.Mutex
items []*biz.TimedTask
items []*system.TimedTask
listErr error
logs chan *biz.TimedTaskLog
logs chan *system.TimedTaskLog
}
func (r *workerTaskRepo) CreateTask(context.Context, *biz.TimedTask) error { return nil }
func (r *workerTaskRepo) UpdateTask(context.Context, *biz.TimedTask) error { return nil }
func (r *workerTaskRepo) DeleteTask(context.Context, uint) error { return nil }
func (r *workerTaskRepo) ToggleTask(context.Context, uint, bool) error { return nil }
func (r *workerTaskRepo) CleanupLogs(context.Context) error { return nil }
func (r *workerTaskRepo) CreateTask(context.Context, *system.TimedTask) error { return nil }
func (r *workerTaskRepo) UpdateTask(context.Context, *system.TimedTask) error { return nil }
func (r *workerTaskRepo) DeleteTask(context.Context, uint) error { return nil }
func (r *workerTaskRepo) ToggleTask(context.Context, uint, bool) error { return nil }
func (r *workerTaskRepo) CleanupLogs(context.Context) error { return nil }
func (r *workerTaskRepo) TaskNameExists(context.Context, string, uint) (bool, error) {
return false, nil
}
func (r *workerTaskRepo) FindTask(_ context.Context, id uint) (*biz.TimedTask, error) {
func (r *workerTaskRepo) FindTask(_ context.Context, id uint) (*system.TimedTask, error) {
r.mu.Lock()
defer r.mu.Unlock()
for _, item := range r.items {
@@ -39,31 +38,31 @@ func (r *workerTaskRepo) FindTask(_ context.Context, id uint) (*biz.TimedTask, e
}
return nil, errors.New("task not found")
}
func (r *workerTaskRepo) ListTasks(context.Context, int, int, *biz.TimedTask) ([]*biz.TimedTask, int64, error) {
func (r *workerTaskRepo) ListTasks(context.Context, int, int, *system.TimedTask) ([]*system.TimedTask, int64, error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.listErr != nil {
return nil, 0, r.listErr
}
items := make([]*biz.TimedTask, 0, len(r.items))
items := make([]*system.TimedTask, 0, len(r.items))
for _, item := range r.items {
items = append(items, cloneTimedTask(item))
}
return items, int64(len(items)), nil
}
func (r *workerTaskRepo) RecordTaskLog(_ context.Context, value *biz.TimedTaskLog) error {
func (r *workerTaskRepo) RecordTaskLog(_ context.Context, value *system.TimedTaskLog) error {
if r.logs != nil {
copy := *value
r.logs <- &copy
}
return nil
}
func (r *workerTaskRepo) ListTaskLogs(context.Context, int, int, uint, string) ([]*biz.TimedTaskLog, int64, error) {
func (r *workerTaskRepo) ListTaskLogs(context.Context, int, int, uint, string) ([]*system.TimedTaskLog, int64, error) {
return nil, 0, nil
}
func newTestTaskScheduler(repo *workerTaskRepo) *TaskScheduler {
tasks := biz.NewTaskUsecase(repo)
tasks := system.NewTaskUsecase(repo)
executor := &TaskExecutor{tasks: tasks}
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
return NewTaskScheduler(tasks, nil, executor, logger)
@@ -72,7 +71,7 @@ func newTestTaskScheduler(repo *workerTaskRepo) *TaskScheduler {
func TestReloadPreservesExistingScheduleOnPreparationFailure(t *testing.T) {
repo := &workerTaskRepo{}
scheduler := newTestTaskScheduler(repo)
old := &biz.TimedTask{ID: 1, Name: "old", Spec: "0 0 * * *", Enabled: true}
old := &system.TimedTask{ID: 1, Name: "old", Spec: "0 0 * * *", Enabled: true}
if err := scheduler.Schedule(old); err != nil {
t.Fatalf("schedule old task: %v", err)
}
@@ -90,7 +89,7 @@ func TestReloadPreservesExistingScheduleOnPreparationFailure(t *testing.T) {
t.Run("invalid cron", func(t *testing.T) {
repo.mu.Lock()
repo.listErr = nil
repo.items = []*biz.TimedTask{{ID: 2, Name: "invalid", Spec: "bad cron", Enabled: true}}
repo.items = []*system.TimedTask{{ID: 2, Name: "invalid", Spec: "bad cron", Enabled: true}}
repo.mu.Unlock()
if err := scheduler.Reload(context.Background()); err == nil {
t.Fatal("Reload() error = nil, want cron error")
@@ -122,7 +121,7 @@ func TestConcurrentScheduleKeepsSingleEntry(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
if err := scheduler.Schedule(&biz.TimedTask{ID: 7, Name: "same", Spec: "0 0 * * *", Enabled: true}); err != nil {
if err := scheduler.Schedule(&system.TimedTask{ID: 7, Name: "same", Spec: "0 0 * * *", Enabled: true}); err != nil {
t.Errorf("Schedule() error = %v", err)
}
}()
@@ -136,15 +135,15 @@ func TestManualTriggerSkipsOverlappingExecution(t *testing.T) {
started := make(chan struct{}, 2)
release := make(chan struct{})
var calls atomic.Int32
biz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
system.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
calls.Add(1)
started <- struct{}{}
<-release
return nil
})
task := &biz.TimedTask{ID: 9, Name: "single", ExecutorType: biz.TaskExecutorMethod, MethodName: methodName}
repo := &workerTaskRepo{items: []*biz.TimedTask{task}, logs: make(chan *biz.TimedTaskLog, 2)}
task := &system.TimedTask{ID: 9, Name: "single", ExecutorType: system.TaskExecutorMethod, MethodName: methodName}
repo := &workerTaskRepo{items: []*system.TimedTask{task}, logs: make(chan *system.TimedTaskLog, 2)}
scheduler := newTestTaskScheduler(repo)
if err := scheduler.TriggerID(context.Background(), task.ID); err != nil {
t.Fatalf("first manual trigger failed: %v", err)
@@ -193,15 +192,15 @@ func TestAutomaticAndManualTriggersShareOverlapGate(t *testing.T) {
started := make(chan struct{}, 1)
release := make(chan struct{})
var calls atomic.Int32
biz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
system.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
calls.Add(1)
started <- struct{}{}
<-release
return nil
})
task := &biz.TimedTask{ID: 10, Name: "shared-gate", ExecutorType: biz.TaskExecutorMethod, MethodName: methodName}
repo := &workerTaskRepo{logs: make(chan *biz.TimedTaskLog, 1)}
task := &system.TimedTask{ID: 10, Name: "shared-gate", ExecutorType: system.TaskExecutorMethod, MethodName: methodName}
repo := &workerTaskRepo{logs: make(chan *system.TimedTaskLog, 1)}
scheduler := newTestTaskScheduler(repo)
if !scheduler.dispatch(task, "auto", true) {
t.Fatal("automatic dispatch was rejected while idle")