优化结构
This commit is contained in:
@@ -5,30 +5,31 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
taskbiz "kra/internal/biz/task"
|
||||
)
|
||||
|
||||
type workerTaskRepo struct {
|
||||
mu sync.Mutex
|
||||
items []*system.TimedTask
|
||||
items []*taskbiz.TimedTask
|
||||
listErr error
|
||||
logs chan *system.TimedTaskLog
|
||||
logs chan *taskbiz.TimedTaskLog
|
||||
}
|
||||
|
||||
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) CreateTask(context.Context, *taskbiz.TimedTask) error { return nil }
|
||||
func (r *workerTaskRepo) UpdateTask(context.Context, *taskbiz.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) (*system.TimedTask, error) {
|
||||
func (r *workerTaskRepo) FindTask(_ context.Context, id uint) (*taskbiz.TimedTask, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
for _, item := range r.items {
|
||||
@@ -38,31 +39,31 @@ func (r *workerTaskRepo) FindTask(_ context.Context, id uint) (*system.TimedTask
|
||||
}
|
||||
return nil, errors.New("task not found")
|
||||
}
|
||||
func (r *workerTaskRepo) ListTasks(context.Context, int, int, *system.TimedTask) ([]*system.TimedTask, int64, error) {
|
||||
func (r *workerTaskRepo) ListTasks(context.Context, int, int, *taskbiz.TimedTask) ([]*taskbiz.TimedTask, int64, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.listErr != nil {
|
||||
return nil, 0, r.listErr
|
||||
}
|
||||
items := make([]*system.TimedTask, 0, len(r.items))
|
||||
items := make([]*taskbiz.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 *system.TimedTaskLog) error {
|
||||
func (r *workerTaskRepo) RecordTaskLog(_ context.Context, value *taskbiz.TimedTaskLog) error {
|
||||
if r.logs != nil {
|
||||
copy := *value
|
||||
r.logs <- ©
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (r *workerTaskRepo) ListTaskLogs(context.Context, int, int, uint, string) ([]*system.TimedTaskLog, int64, error) {
|
||||
func (r *workerTaskRepo) ListTaskLogs(context.Context, int, int, uint, string) ([]*taskbiz.TimedTaskLog, int64, error) {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func newTestTaskScheduler(repo *workerTaskRepo) *TaskScheduler {
|
||||
tasks := system.NewTaskUsecase(repo)
|
||||
tasks := taskbiz.NewTaskUsecase(repo)
|
||||
executor := &TaskExecutor{tasks: tasks}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
return NewTaskScheduler(tasks, nil, executor, logger)
|
||||
@@ -71,7 +72,7 @@ func newTestTaskScheduler(repo *workerTaskRepo) *TaskScheduler {
|
||||
func TestReloadPreservesExistingScheduleOnPreparationFailure(t *testing.T) {
|
||||
repo := &workerTaskRepo{}
|
||||
scheduler := newTestTaskScheduler(repo)
|
||||
old := &system.TimedTask{ID: 1, Name: "old", Spec: "0 0 * * *", Enabled: true}
|
||||
old := &taskbiz.TimedTask{ID: 1, Name: "old", Spec: "0 0 * * *", Enabled: true}
|
||||
if err := scheduler.Schedule(old); err != nil {
|
||||
t.Fatalf("schedule old task: %v", err)
|
||||
}
|
||||
@@ -89,7 +90,7 @@ func TestReloadPreservesExistingScheduleOnPreparationFailure(t *testing.T) {
|
||||
t.Run("invalid cron", func(t *testing.T) {
|
||||
repo.mu.Lock()
|
||||
repo.listErr = nil
|
||||
repo.items = []*system.TimedTask{{ID: 2, Name: "invalid", Spec: "bad cron", Enabled: true}}
|
||||
repo.items = []*taskbiz.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")
|
||||
@@ -121,7 +122,7 @@ func TestConcurrentScheduleKeepsSingleEntry(t *testing.T) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := scheduler.Schedule(&system.TimedTask{ID: 7, Name: "same", Spec: "0 0 * * *", Enabled: true}); err != nil {
|
||||
if err := scheduler.Schedule(&taskbiz.TimedTask{ID: 7, Name: "same", Spec: "0 0 * * *", Enabled: true}); err != nil {
|
||||
t.Errorf("Schedule() error = %v", err)
|
||||
}
|
||||
}()
|
||||
@@ -135,15 +136,15 @@ func TestManualTriggerSkipsOverlappingExecution(t *testing.T) {
|
||||
started := make(chan struct{}, 2)
|
||||
release := make(chan struct{})
|
||||
var calls atomic.Int32
|
||||
system.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
calls.Add(1)
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return nil
|
||||
})
|
||||
|
||||
task := &system.TimedTask{ID: 9, Name: "single", ExecutorType: system.TaskExecutorMethod, MethodName: methodName}
|
||||
repo := &workerTaskRepo{items: []*system.TimedTask{task}, logs: make(chan *system.TimedTaskLog, 2)}
|
||||
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)
|
||||
if err := scheduler.TriggerID(context.Background(), task.ID); err != nil {
|
||||
t.Fatalf("first manual trigger failed: %v", err)
|
||||
@@ -192,15 +193,15 @@ func TestAutomaticAndManualTriggersShareOverlapGate(t *testing.T) {
|
||||
started := make(chan struct{}, 1)
|
||||
release := make(chan struct{})
|
||||
var calls atomic.Int32
|
||||
system.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
calls.Add(1)
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return nil
|
||||
})
|
||||
|
||||
task := &system.TimedTask{ID: 10, Name: "shared-gate", ExecutorType: system.TaskExecutorMethod, MethodName: methodName}
|
||||
repo := &workerTaskRepo{logs: make(chan *system.TimedTaskLog, 1)}
|
||||
task := &taskbiz.TimedTask{ID: 10, Name: "shared-gate", ExecutorType: taskbiz.TaskExecutorMethod, MethodName: methodName}
|
||||
repo := &workerTaskRepo{logs: make(chan *taskbiz.TimedTaskLog, 1)}
|
||||
scheduler := newTestTaskScheduler(repo)
|
||||
if !scheduler.dispatch(task, "auto", true) {
|
||||
t.Fatal("automatic dispatch was rejected while idle")
|
||||
|
||||
Reference in New Issue
Block a user