优化结构

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
+13 -13
View File
@@ -5,17 +5,17 @@ import (
"encoding/json"
"errors"
"fmt"
"kra/internal/biz/system"
"log/slog"
"sync"
"time"
"github.com/robfig/cron/v3"
"kra/internal/biz"
)
type TaskScheduler struct {
tasks *biz.TaskUsecase
authorities *biz.AuthorityUsecase
tasks *system.TaskUsecase
authorities *system.AuthorityUsecase
executor *TaskExecutor
logger *slog.Logger
standard *cron.Cron
@@ -38,13 +38,13 @@ type scheduledEntry struct {
entry cron.EntryID
}
func NewTaskScheduler(tasks *biz.TaskUsecase, authorities *biz.AuthorityUsecase, executor *TaskExecutor, logger *slog.Logger) *TaskScheduler {
func NewTaskScheduler(tasks *system.TaskUsecase, authorities *system.AuthorityUsecase, executor *TaskExecutor, logger *slog.Logger) *TaskScheduler {
idle := make(chan struct{})
close(idle)
return &TaskScheduler{tasks: tasks, authorities: authorities, executor: executor, logger: logger.With("mod", "timedTask"), standard: cron.New(), seconds: cron.New(cron.WithSeconds()), entries: map[uint]scheduledEntry{}, running: map[uint]struct{}{}, idle: idle, subscribers: map[uint]map[chan []byte]struct{}{}}
}
func NewTaskRuntime(scheduler *TaskScheduler) biz.TaskRuntime { return scheduler }
func NewTaskRuntime(scheduler *TaskScheduler) system.TaskRuntime { return scheduler }
func (s *TaskScheduler) Start(ctx context.Context) error {
runContext, cancel := context.WithCancel(ctx)
@@ -116,7 +116,7 @@ func (s *TaskScheduler) Reload(ctx context.Context) error {
return err
}
type preparedTask struct {
task *biz.TimedTask
task *system.TimedTask
schedule cron.Schedule
}
prepared := make([]preparedTask, 0, len(items))
@@ -181,7 +181,7 @@ func (s *TaskScheduler) finishRun(id uint) {
}
}
func (s *TaskScheduler) dispatch(task *biz.TimedTask, trigger string, async bool) bool {
func (s *TaskScheduler) dispatch(task *system.TimedTask, trigger string, async bool) bool {
ctx, ok := s.beginRun(task.ID)
if !ok {
s.logger.Warn("timed task skipped because it is already running or the scheduler is stopping", "task_id", task.ID, "task_name", task.Name, "trigger_type", trigger)
@@ -199,7 +199,7 @@ func (s *TaskScheduler) dispatch(task *biz.TimedTask, trigger string, async bool
return true
}
func (s *TaskScheduler) run(ctx context.Context, task *biz.TimedTask, trigger string) {
func (s *TaskScheduler) run(ctx context.Context, task *system.TimedTask, trigger string) {
log := s.executor.Run(ctx, task, trigger)
attributes := []any{"task_id", log.TaskID, "task_name", log.TaskName, "trigger_type", log.TriggerType, "status", log.Status, "duration_ms", log.DurationMS, "started_at", log.StartedAt, "finished_at", log.FinishedAt}
if log.ErrorMsg != "" {
@@ -222,7 +222,7 @@ func (s *TaskScheduler) run(ctx context.Context, task *biz.TimedTask, trigger st
}
}
func (s *TaskScheduler) Schedule(task *biz.TimedTask) error {
func (s *TaskScheduler) Schedule(task *system.TimedTask) error {
if task == nil {
return errors.New("定时任务不能为空")
}
@@ -244,21 +244,21 @@ func (s *TaskScheduler) Schedule(task *biz.TimedTask) error {
return nil
}
func parseTaskSchedule(task *biz.TimedTask) (cron.Schedule, error) {
func parseTaskSchedule(task *system.TimedTask) (cron.Schedule, error) {
if task.WithSeconds {
return cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor).Parse(task.Spec)
}
return cron.ParseStandard(task.Spec)
}
func cloneTimedTask(task *biz.TimedTask) *biz.TimedTask {
func cloneTimedTask(task *system.TimedTask) *system.TimedTask {
copy := *task
copy.Params = append([]byte(nil), task.Params...)
copy.HTTPHeader = append([]byte(nil), task.HTTPHeader...)
return &copy
}
func (s *TaskScheduler) scheduleLocked(task *biz.TimedTask, schedule cron.Schedule) {
func (s *TaskScheduler) scheduleLocked(task *system.TimedTask, schedule cron.Schedule) {
run := func() {
s.dispatch(task, "auto", false)
}
@@ -317,7 +317,7 @@ func (s *TaskScheduler) NextRuns() map[uint]time.Time {
return out
}
func (s *TaskScheduler) Trigger(task *biz.TimedTask) bool {
func (s *TaskScheduler) Trigger(task *system.TimedTask) bool {
if task == nil {
return false
}