feat: 完善后端并新增 Vue3 WebUI
后端: - 修复登录校验、DB 并发(单例+WAL+纯Go sqlite)、会话密钥持久化等核心缺陷 - 新增 Timer CRUD/签到/手动触发、执行日志、仪表盘、驱动元数据等 API - 统一驱动接口: 带 context 的执行器、真实错误返回、敏感字段掩码、连通性测试 - 补全钉钉执行器; Timer 触发/提醒去重; 配置支持默认值与环境变量覆盖 前端: - Vite+Vue3+TS+Pinia+Element Plus, 由驱动元数据动态渲染配置表单 - 登录/仪表盘(倒计时环)/定时器/规则/账号/日志/设置页面, 支持暗色模式 - 主题色 #66ccff, 信号波纹 logo, go:embed 单二进制部署 构建: Dockerfile 增加前端构建阶段; 补充各驱动配置文档 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+25
-14
@@ -1,9 +1,13 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/drivers/bilibili"
|
||||
"github.com/ssdomei232/goodBaby/drivers/dingtalk"
|
||||
"github.com/ssdomei232/goodBaby/drivers/email"
|
||||
"github.com/ssdomei232/goodBaby/drivers/github"
|
||||
"github.com/ssdomei232/goodBaby/drivers/onebot"
|
||||
@@ -12,8 +16,8 @@ import (
|
||||
|
||||
// RuleExecutor 规则执行器接口
|
||||
type RuleExecutor interface {
|
||||
// Execute 执行规则
|
||||
Execute(rule *model.Rule) error
|
||||
// Execute 执行规则,ctx 决定了重试的最长时间
|
||||
Execute(ctx context.Context, rule *model.Rule) error
|
||||
// GetType 获取执行器支持的规则类型
|
||||
GetType() string
|
||||
}
|
||||
@@ -36,13 +40,13 @@ func (er *ExecutorRegistry) Register(executor RuleExecutor) {
|
||||
}
|
||||
|
||||
// Execute 根据规则类型执行规则
|
||||
func (er *ExecutorRegistry) Execute(rule *model.Rule) error {
|
||||
func (er *ExecutorRegistry) Execute(ctx context.Context, rule *model.Rule) error {
|
||||
executor, exists := er.executors[rule.Type]
|
||||
if !exists {
|
||||
return fmt.Errorf("不支持的规则类型: %s", rule.Type)
|
||||
}
|
||||
|
||||
return executor.Execute(rule)
|
||||
return executor.Execute(ctx, rule)
|
||||
}
|
||||
|
||||
// GetSupportedTypes 获取所有支持的规则类型
|
||||
@@ -51,24 +55,31 @@ func (er *ExecutorRegistry) GetSupportedTypes() []string {
|
||||
for t := range er.executors {
|
||||
types = append(types, t)
|
||||
}
|
||||
sort.Strings(types)
|
||||
return types
|
||||
}
|
||||
|
||||
var globalExecutorRegistry *ExecutorRegistry
|
||||
var (
|
||||
registryOnce sync.Once
|
||||
globalExecutorRegistry *ExecutorRegistry
|
||||
)
|
||||
|
||||
// InitExecutorRegistry 初始化执行器注册表并注册所有执行器
|
||||
func InitExecutorRegistry() *ExecutorRegistry {
|
||||
registry := NewExecutorRegistry()
|
||||
registryOnce.Do(func() {
|
||||
registry := NewExecutorRegistry()
|
||||
|
||||
// 注册所有规则执行器
|
||||
registry.Register(&bilibili.BilibiliDynamicExecutor{})
|
||||
registry.Register(&email.EmailExecutor{})
|
||||
registry.Register(&github.GithubMakeRepoPublicExecutor{})
|
||||
registry.Register(&onebot.OneBotExecutor{})
|
||||
// 未来添加新规则类型时,在这里注册即可
|
||||
// 注册所有规则执行器
|
||||
registry.Register(&bilibili.BilibiliDynamicExecutor{})
|
||||
registry.Register(&email.EmailExecutor{})
|
||||
registry.Register(&github.GithubMakeRepoPublicExecutor{})
|
||||
registry.Register(&onebot.OneBotExecutor{})
|
||||
registry.Register(&dingtalk.DingTalkExecutor{})
|
||||
// 未来添加新规则类型时,在这里注册即可
|
||||
|
||||
globalExecutorRegistry = registry
|
||||
return registry
|
||||
globalExecutorRegistry = registry
|
||||
})
|
||||
return globalExecutorRegistry
|
||||
}
|
||||
|
||||
// GetGlobalExecutorRegistry 获取全局执行器注册表
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/handler/db"
|
||||
"github.com/ssdomei232/goodBaby/handler/logstore"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// runner 执行需要被执行的 Rule
|
||||
// Runner 执行某个 Timer 下所有启用的规则。
|
||||
//
|
||||
// 触发前先把 Timer 标记为已触发,避免规则执行期间(可能长达数小时)
|
||||
// 被下一轮检查重复触发;用户重新签到后 Triggered 会被重置。
|
||||
func Runner(timer *model.Timer) {
|
||||
gormDB, err := db.GetGormDB()
|
||||
if err != nil {
|
||||
@@ -17,22 +25,61 @@ func Runner(timer *model.Timer) {
|
||||
|
||||
// 1. 获取所有需要执行的 Rule
|
||||
var rules []model.Rule
|
||||
if err := gormDB.Where("timer_id = ?", timer.ID).Find(&rules).Error; err != nil {
|
||||
if err := gormDB.Where("timer_id = ? AND enabled = ?", timer.ID, true).Find(&rules).Error; err != nil {
|
||||
log.Printf("failed to get rules: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 执行每个 Rule
|
||||
for _, rule := range rules {
|
||||
go executeRule(&rule)
|
||||
// 2. 标记已触发
|
||||
now := time.Now().Unix()
|
||||
if err := gormDB.Model(&model.Timer{}).Where("id = ?", timer.ID).
|
||||
Updates(map[string]any{"triggered": true, "last_trigger": now}).Error; err != nil {
|
||||
log.Printf("标记 Timer(ID: %d) 已触发失败: %v", timer.ID, err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Timer %s (ID: %d) 已到期,开始执行 %d 条规则", timer.Name, timer.ID, len(rules))
|
||||
|
||||
// 3. 并发执行每个 Rule
|
||||
var wg sync.WaitGroup
|
||||
for i := range rules {
|
||||
rule := rules[i]
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
ExecuteRule(&rule, model.TriggerTimer)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// executeRule 执行单个规则
|
||||
func executeRule(rule *model.Rule) {
|
||||
executorRegistry := GetGlobalExecutorRegistry()
|
||||
// ExecuteRule 执行单个规则并记录执行日志
|
||||
func ExecuteRule(rule *model.Rule, trigger string) error {
|
||||
ctx, cancel := retry.ExecutionContext()
|
||||
defer cancel()
|
||||
return ExecuteRuleWithContext(ctx, rule, trigger)
|
||||
}
|
||||
|
||||
if err := executorRegistry.Execute(rule); err != nil {
|
||||
// ExecuteRuleWithContext 使用给定 context 执行规则,手动测试时可传入较短的超时
|
||||
func ExecuteRuleWithContext(ctx context.Context, rule *model.Rule, trigger string) error {
|
||||
err := GetGlobalExecutorRegistry().Execute(ctx, rule)
|
||||
|
||||
message := "执行成功"
|
||||
if err != nil {
|
||||
message = err.Error()
|
||||
log.Printf("执行规则失败 [ID: %d, Type: %s]: %v", rule.ID, rule.Type, err)
|
||||
}
|
||||
|
||||
logstore.Record(&model.ExecutionLog{
|
||||
UID: rule.UID,
|
||||
RuleID: rule.ID,
|
||||
RuleName: rule.Name,
|
||||
RuleType: rule.Type,
|
||||
TimerID: rule.TimerID,
|
||||
Trigger: trigger,
|
||||
Success: err == nil,
|
||||
Message: message,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user