This commit is contained in:
Yvan
2026-08-18 15:04:07 +08:00
parent 10ccc005be
commit 1ef8ecb7b2
42 changed files with 3027 additions and 296 deletions
+21 -13
View File
@@ -154,39 +154,47 @@ func NewTaskApplicationUsecase(tasks *TaskUsecase, runtime TaskRuntime) *TaskApp
return &TaskApplicationUsecase{tasks: tasks, runtime: runtime}
}
func (uc *TaskApplicationUsecase) syncRuntime(ctx context.Context, id uint) error {
if ctx == nil {
ctx = context.Background()
}
if err := uc.runtime.ScheduleID(ctx, id); err != nil {
repairCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
if reloadErr := uc.runtime.Reload(repairCtx); reloadErr != nil {
return &TaskScheduleError{Err: errors.Join(err, fmt.Errorf("重载任务运行时失败: %w", reloadErr))}
}
}
return nil
}
func (uc *TaskApplicationUsecase) Create(ctx context.Context, value *TimedTask) error {
if err := uc.tasks.Create(ctx, value); err != nil {
return err
}
if err := uc.runtime.ScheduleID(ctx, value.ID); err != nil {
return &TaskScheduleError{Err: err}
}
return nil
return uc.syncRuntime(ctx, value.ID)
}
func (uc *TaskApplicationUsecase) Update(ctx context.Context, value *TimedTask) error {
if err := uc.tasks.Update(ctx, value); err != nil {
return err
}
if err := uc.runtime.ScheduleID(ctx, value.ID); err != nil {
return &TaskScheduleError{Err: err}
}
return nil
return uc.syncRuntime(ctx, value.ID)
}
func (uc *TaskApplicationUsecase) Delete(ctx context.Context, id uint) error {
if err := uc.tasks.DeleteTask(ctx, id); err != nil {
return err
}
uc.runtime.Remove(id)
return uc.tasks.DeleteTask(ctx, id)
return nil
}
func (uc *TaskApplicationUsecase) Toggle(ctx context.Context, id uint, enabled bool) error {
if err := uc.tasks.ToggleTask(ctx, id, enabled); err != nil {
return err
}
if err := uc.runtime.ScheduleID(ctx, id); err != nil {
return &TaskScheduleError{Err: err}
}
return nil
return uc.syncRuntime(ctx, id)
}
func (uc *TaskApplicationUsecase) Trigger(ctx context.Context, id uint) error {