mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-05-11 18:10:23 +08:00
基础修复: - 新增节点离线检测:每 15s 扫描,超 45s 未心跳的远程节点自动置离线 - 节点删除前检查关联任务,避免孤立备份任务 - BackupTaskRepository 新增 CountByNodeID/ListByNodeID Master 端 Agent 协议: - 新增 AgentCommand 模型与命令队列仓储(pending/dispatched/succeeded/failed/timeout) - 新增 AgentService:任务下发、命令轮询、结果回收、超时扫描 - 新增专用 Agent HTTP API(X-Agent-Token 认证): /api/agent/heartbeat /api/agent/commands/poll /api/agent/commands/:id/result /api/agent/tasks/:id /api/agent/records/:id - BackupExecutionService 支持 node 路由:task.NodeID 指向远程节点时自动入队派发 Agent CLI(backupx agent 子命令): - 配置:YAML 文件 / 环境变量 / CLI 参数,优先级 CLI > 文件 > 环境 - 心跳循环 + 命令轮询循环 + 优雅退出 - 本地复用 BackupRunner 与 storage registry 执行备份并直接上传 - 支持 run_task 和 list_dir 两种命令 远程目录浏览: - NodeService 支持通过 Agent RPC 列出远程节点目录(15s 超时) 前端: - NodesPage 添加节点后展示 Agent 启动命令和环境变量配置 文档: - README 中英文重写"多节点集群"章节,含架构图、步骤、限制、CLI 参考
65 lines
2.2 KiB
Go
65 lines
2.2 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"backupx/server/internal/repository"
|
|
servicepkg "backupx/server/internal/service"
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"backupx/server/internal/model"
|
|
)
|
|
|
|
type fakeTaskRepository struct {
|
|
items []model.BackupTask
|
|
}
|
|
|
|
func (r *fakeTaskRepository) List(context.Context, repository.BackupTaskListOptions) ([]model.BackupTask, error) {
|
|
return nil, nil
|
|
}
|
|
func (r *fakeTaskRepository) FindByID(context.Context, uint) (*model.BackupTask, error) {
|
|
return nil, nil
|
|
}
|
|
func (r *fakeTaskRepository) FindByName(context.Context, string) (*model.BackupTask, error) {
|
|
return nil, nil
|
|
}
|
|
func (r *fakeTaskRepository) ListSchedulable(context.Context) ([]model.BackupTask, error) {
|
|
return r.items, nil
|
|
}
|
|
func (r *fakeTaskRepository) Count(context.Context) (int64, error) { return 0, nil }
|
|
func (r *fakeTaskRepository) CountEnabled(context.Context) (int64, error) { return 0, nil }
|
|
func (r *fakeTaskRepository) CountByStorageTargetID(context.Context, uint) (int64, error) {
|
|
return 0, nil
|
|
}
|
|
func (r *fakeTaskRepository) CountByNodeID(context.Context, uint) (int64, error) {
|
|
return 0, nil
|
|
}
|
|
func (r *fakeTaskRepository) ListByNodeID(context.Context, uint) ([]model.BackupTask, error) {
|
|
return nil, nil
|
|
}
|
|
func (r *fakeTaskRepository) Create(context.Context, *model.BackupTask) error { return nil }
|
|
func (r *fakeTaskRepository) Update(context.Context, *model.BackupTask) error { return nil }
|
|
func (r *fakeTaskRepository) Delete(context.Context, uint) error { return nil }
|
|
|
|
type fakeRunner struct{ taskIDs []uint }
|
|
|
|
func (r *fakeRunner) RunTaskByID(_ context.Context, id uint) (*servicepkg.BackupRecordDetail, error) {
|
|
r.taskIDs = append(r.taskIDs, id)
|
|
return nil, nil
|
|
}
|
|
|
|
func TestServiceSyncTaskAndTrigger(t *testing.T) {
|
|
repo := &fakeTaskRepository{}
|
|
runner := &fakeRunner{}
|
|
service := NewService(repo, runner, nil)
|
|
if err := service.SyncTask(context.Background(), &model.BackupTask{ID: 1, Enabled: true, CronExpr: "*/1 * * * * *"}); err != nil {
|
|
t.Fatalf("SyncTask returned error: %v", err)
|
|
}
|
|
service.cron.Start()
|
|
defer service.cron.Stop()
|
|
time.Sleep(1100 * time.Millisecond)
|
|
if len(runner.taskIDs) == 0 {
|
|
t.Fatalf("expected scheduled runner to be triggered")
|
|
}
|
|
}
|