Files
EasyGoAdmin_GoFrame_EleVue/internal/logic/job_handler.go
T
2026-10-20 11:37:25 +08:00

35 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package logic
import (
"context"
"fmt"
)
// ============================================================================
// 定时任务本地处理方法注册表
//
// 任务执行目标两种形态(见 executeJob):
// - url 以 http:// 或 https:// 开头:HTTP 调用目标地址
// - 其余取值:视为本地处理方法名,按方法名从注册表查找执行
//
// 具体任务实现在 internal/task 包,于 init 中调用 RegisterJobHandler 注册。
// 依赖方向单向:task → logic,logic 不得反向依赖 task。
// ============================================================================
// jobHandler 本地任务处理方法
// 入参为任务参数(JSON 字符串,可为空),返回值为可写入任务日志的执行摘要
type jobHandler func(ctx context.Context, params string) (string, error)
// jobHandlers 本地处理方法注册表
// 键为方法名,与任务记录的 url 字段值一一对应
var jobHandlers = map[string]jobHandler{}
// RegisterJobHandler 注册本地任务处理方法
// 方法名需全局唯一,重复注册时进程直接 panic 暴露配置错误
func RegisterJobHandler(name string, h jobHandler) {
if _, exists := jobHandlers[name]; exists {
panic(fmt.Sprintf("定时任务处理方法重复注册: %s", name))
}
jobHandlers[name] = h
}