优化结构
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -15,23 +14,25 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
taskbiz "kra/internal/biz/task"
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
type TaskExecutor struct {
|
||||
tasks *system.TaskUsecase
|
||||
tasks *taskbiz.TaskUsecase
|
||||
media *system.MediaUsecase
|
||||
runtime *conf.Runtime
|
||||
methods system.TaskMethodRegistry
|
||||
methods taskbiz.TaskMethodRegistry
|
||||
}
|
||||
|
||||
func NewTaskExecutor(tasks *system.TaskUsecase, media *system.MediaUsecase, runtime *conf.Runtime) *TaskExecutor {
|
||||
return NewTaskExecutorWithRegistry(tasks, media, runtime, system.DefaultTaskMethodRegistry())
|
||||
func NewTaskExecutor(tasks *taskbiz.TaskUsecase, media *system.MediaUsecase, runtime *conf.Runtime) *TaskExecutor {
|
||||
return NewTaskExecutorWithRegistry(tasks, media, runtime, taskbiz.DefaultTaskMethodRegistry())
|
||||
}
|
||||
|
||||
func NewTaskExecutorWithRegistry(tasks *system.TaskUsecase, media *system.MediaUsecase, runtime *conf.Runtime, methods system.TaskMethodRegistry) *TaskExecutor {
|
||||
func NewTaskExecutorWithRegistry(tasks *taskbiz.TaskUsecase, media *system.MediaUsecase, runtime *conf.Runtime, methods taskbiz.TaskMethodRegistry) *TaskExecutor {
|
||||
if methods == nil {
|
||||
methods = system.DefaultTaskMethodRegistry()
|
||||
methods = taskbiz.DefaultTaskMethodRegistry()
|
||||
}
|
||||
return &TaskExecutor{tasks: tasks, media: media, runtime: runtime, methods: methods}
|
||||
}
|
||||
@@ -61,7 +62,7 @@ func taskHTTPClient(allowPrivate bool) *http.Client {
|
||||
return &http.Client{Timeout: 30 * time.Second, Transport: &http.Transport{Proxy: nil, DialContext: dialer.DialContext}}
|
||||
}
|
||||
|
||||
func (e *TaskExecutor) runHTTP(ctx context.Context, task *system.TimedTask) (string, error) {
|
||||
func (e *TaskExecutor) runHTTP(ctx context.Context, task *taskbiz.TimedTask) (string, error) {
|
||||
parsed, err := url.Parse(task.HTTPURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("URL 非法: %w", err)
|
||||
@@ -109,13 +110,13 @@ func truncateTaskText(value string) string {
|
||||
return value[:limit] + "...(截断)"
|
||||
}
|
||||
|
||||
func (e *TaskExecutor) runMethod(ctx context.Context, task *system.TimedTask) error {
|
||||
var method system.TaskMethodFunc
|
||||
func (e *TaskExecutor) runMethod(ctx context.Context, task *taskbiz.TimedTask) error {
|
||||
var method taskbiz.TaskMethodFunc
|
||||
var ok bool
|
||||
if e.methods != nil {
|
||||
method, ok = e.methods.Lookup(task.MethodName)
|
||||
} else {
|
||||
method, ok = system.TaskMethodByName(task.MethodName)
|
||||
method, ok = taskbiz.TaskMethodByName(task.MethodName)
|
||||
}
|
||||
if !ok {
|
||||
return fmt.Errorf("方法 %s 未注册(需通过 platform/task.Registry 注册)", task.MethodName)
|
||||
@@ -145,12 +146,12 @@ func (e *TaskExecutor) runMethod(ctx context.Context, task *system.TimedTask) er
|
||||
}
|
||||
}
|
||||
|
||||
func (e *TaskExecutor) Run(ctx context.Context, task *system.TimedTask, trigger string) (log *system.TimedTaskLog) {
|
||||
func (e *TaskExecutor) Run(ctx context.Context, task *taskbiz.TimedTask, trigger string) (log *taskbiz.TimedTaskLog) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
started := time.Now()
|
||||
log = &system.TimedTaskLog{TaskID: task.ID, TaskName: task.Name, TriggerType: trigger, StartedAt: started, Status: "success"}
|
||||
log = &taskbiz.TimedTaskLog{TaskID: task.ID, TaskName: task.Name, TriggerType: trigger, StartedAt: started, Status: "success"}
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
log.Status = "fail"
|
||||
@@ -165,9 +166,9 @@ func (e *TaskExecutor) Run(ctx context.Context, task *system.TimedTask, trigger
|
||||
}()
|
||||
var err error
|
||||
switch task.ExecutorType {
|
||||
case system.TaskExecutorMethod:
|
||||
case taskbiz.TaskExecutorMethod:
|
||||
err = e.runMethod(ctx, task)
|
||||
case system.TaskExecutorHTTP:
|
||||
case taskbiz.TaskExecutorHTTP:
|
||||
runCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
log.Output, err = e.runHTTP(runCtx, task)
|
||||
cancel()
|
||||
|
||||
Reference in New Issue
Block a user