88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package exec
|
||
|
||
import (
|
||
"context"
|
||
"sync"
|
||
|
||
"everypublish/shared/proto"
|
||
)
|
||
|
||
// Task 执行任务(由 task.push 转换而来)
|
||
type Task struct {
|
||
TaskID string
|
||
Platform string
|
||
AccountID string
|
||
AccountName string
|
||
Title string
|
||
Content string
|
||
Tags []string
|
||
MaterialURLs []string
|
||
ScheduleAt int64
|
||
Priority int
|
||
}
|
||
|
||
// Result 执行结果(回传 task.result)
|
||
type Result struct {
|
||
TaskID string
|
||
Status string
|
||
PublishedURL string
|
||
Receipts []string
|
||
Error string
|
||
}
|
||
|
||
// Executor 平台执行器(每个平台一个实现;D10 起接入真实平台)
|
||
type Executor interface {
|
||
Platform() string
|
||
Execute(ctx context.Context, task Task) Result
|
||
}
|
||
|
||
// ChallengeSolver 能自动完成挑战的平台执行器(扫码/验证码)。
|
||
// onQR:平台生成二维码时回调(把 qrToken/qrUrl 推给壳展示,可为 nil)。
|
||
type ChallengeSolver interface {
|
||
SolveChallenge(ctx context.Context, ch proto.Challenge, onQR func(qrToken, qrURL string)) (solved bool, value string)
|
||
}
|
||
|
||
// Registry 执行器注册表(并发安全)
|
||
type Registry struct {
|
||
mu sync.RWMutex
|
||
m map[string]Executor
|
||
fallback Executor
|
||
}
|
||
|
||
// NewRegistry 创建注册表
|
||
func NewRegistry() *Registry {
|
||
return &Registry{m: make(map[string]Executor)}
|
||
}
|
||
|
||
// Register 注册平台执行器
|
||
func (r *Registry) Register(e Executor) {
|
||
r.mu.Lock()
|
||
defer r.mu.Unlock()
|
||
r.m[e.Platform()] = e
|
||
}
|
||
|
||
// SetFallback 注册兜底执行器(未接入平台用)
|
||
func (r *Registry) SetFallback(e Executor) {
|
||
r.mu.Lock()
|
||
defer r.mu.Unlock()
|
||
r.fallback = e
|
||
}
|
||
|
||
// Get 取执行器(无匹配返回 nil)
|
||
func (r *Registry) Get(platform string) Executor {
|
||
r.mu.RLock()
|
||
defer r.mu.RUnlock()
|
||
if e, ok := r.m[platform]; ok {
|
||
return e
|
||
}
|
||
return r.fallback
|
||
}
|
||
|
||
// Solve 取挑战解决器(nil 表示不支持自动解决)
|
||
func (r *Registry) Solve(platform string) ChallengeSolver {
|
||
if s, ok := r.Get(platform).(ChallengeSolver); ok {
|
||
return s
|
||
}
|
||
return nil
|
||
}
|