Initial commit: TaskPool React panel
- React frontend with route-level code splitting - Backend rebranded from Baihu to TaskPool - DB brand migration script and local compatibility
This commit is contained in:
+844
@@ -0,0 +1,844 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/engigu/taskpool/internal/constant"
|
||||
"github.com/engigu/taskpool/internal/executor"
|
||||
"github.com/engigu/taskpool/internal/logger"
|
||||
"github.com/engigu/taskpool/internal/utils"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// WebSocket 消息类型
|
||||
const (
|
||||
WSTypeHeartbeat = constant.WSTypeHeartbeat
|
||||
WSTypeHeartbeatAck = constant.WSTypeHeartbeatAck
|
||||
WSTypeTasks = constant.WSTypeTasks
|
||||
WSTypeTaskResult = constant.WSTypeTaskResult
|
||||
WSTypeUpdate = constant.WSTypeUpdate
|
||||
WSTypeConnected = constant.WSTypeConnected
|
||||
WSTypeDisabled = constant.WSTypeDisabled
|
||||
WSTypeEnabled = constant.WSTypeEnabled
|
||||
WSTypeFetchTasks = constant.WSTypeFetchTasks
|
||||
WSTypeTaskLog = constant.WSTypeTaskLog
|
||||
WSTypeExecute = constant.WSTypeExecute
|
||||
WSTypeTaskHeartbeat = constant.WSTypeTaskHeartbeat
|
||||
WSTypeStop = constant.WSTypeStop
|
||||
)
|
||||
|
||||
type WSMessage struct {
|
||||
Type string `json:"type"`
|
||||
Data json.RawMessage `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type AgentTask struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Command string `json:"command"`
|
||||
PreCommand string `json:"pre_command"`
|
||||
PostCommand string `json:"post_command"`
|
||||
Schedule string `json:"schedule"`
|
||||
Cron string `json:"cron"`
|
||||
Timeout int `json:"timeout"`
|
||||
WorkDir string `json:"work_dir"`
|
||||
Envs string `json:"envs"`
|
||||
Languages []map[string]string `json:"languages"`
|
||||
RandomRange int `json:"random_range"`
|
||||
Secrets []string `json:"secrets"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetID() string {
|
||||
return t.ID
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetName() string {
|
||||
return t.Name
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetCommand() string {
|
||||
return t.Command
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetPreCommand() string {
|
||||
return t.PreCommand
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetPostCommand() string {
|
||||
return t.PostCommand
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetTimeout() int {
|
||||
return t.Timeout
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetWorkDir() string {
|
||||
return t.WorkDir
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetEnvs() string {
|
||||
return t.Envs
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetEnvVars() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetSecrets() []string {
|
||||
return t.Secrets
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetLanguages() []map[string]string {
|
||||
return t.Languages
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetUseMise() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *AgentTask) UseMise() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetSchedule() string {
|
||||
if t.Schedule != "" {
|
||||
return t.Schedule
|
||||
}
|
||||
return t.Cron
|
||||
}
|
||||
|
||||
func (t *AgentTask) GetRandomRange() int {
|
||||
return t.RandomRange
|
||||
}
|
||||
|
||||
type TaskResult struct {
|
||||
TaskID string `json:"task_id"`
|
||||
LogID string `json:"log_id"`
|
||||
AgentID string `json:"agent_id"` // 仅用于 HTTP 上报时后端补充
|
||||
Command string `json:"command"`
|
||||
Output string `json:"output"`
|
||||
Error string `json:"error"`
|
||||
Status string `json:"status"`
|
||||
Duration int64 `json:"duration"`
|
||||
ExitCode int `json:"exit_code"`
|
||||
StartTime int64 `json:"start_time"`
|
||||
EndTime int64 `json:"end_time"`
|
||||
}
|
||||
|
||||
type Agent struct {
|
||||
config *Config
|
||||
configFile string
|
||||
machineID string
|
||||
scheduler *executor.Scheduler
|
||||
cronManager *executor.CronManager
|
||||
tasks map[string]*AgentTask // 本地任务缓存,用于执行 lookup
|
||||
lastTaskCount int
|
||||
mu sync.RWMutex
|
||||
client *http.Client
|
||||
wsConn *websocket.Conn
|
||||
wsMu sync.Mutex
|
||||
stopCh chan struct{}
|
||||
wsStopCh chan struct{} // 用于停止当前 WebSocket 相关的 goroutine
|
||||
taskLogs map[string][]string // 记录最近的日志行,用于失败显示
|
||||
logMu sync.Mutex // taskLogs 的锁
|
||||
schedulerStarted bool // 调度器是否已经启动
|
||||
}
|
||||
|
||||
func NewAgent(config *Config, configFile string) *Agent {
|
||||
a := &Agent{
|
||||
config: config,
|
||||
configFile: configFile,
|
||||
machineID: utils.GenerateMachineID(),
|
||||
tasks: make(map[string]*AgentTask),
|
||||
client: &http.Client{Timeout: 30 * time.Second},
|
||||
stopCh: make(chan struct{}),
|
||||
lastTaskCount: -1,
|
||||
taskLogs: make(map[string][]string),
|
||||
}
|
||||
|
||||
// 初始化调度器
|
||||
handler := &AgentHandler{agent: a}
|
||||
schedCfg := executor.SchedulerConfig{
|
||||
WorkerCount: runtime.NumCPU(),
|
||||
QueueSize: 100,
|
||||
RateInterval: 100 * time.Millisecond,
|
||||
Verbose: true,
|
||||
}
|
||||
a.scheduler = executor.NewScheduler(schedCfg, handler)
|
||||
a.scheduler.SetLogger(logger.NewSchedulerLogger())
|
||||
a.cronManager = executor.NewCronManager(a.scheduler)
|
||||
a.cronManager.SetLogger(logger.NewSchedulerLogger())
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// AgentHandler 实现 executor.SchedulerEventHandler
|
||||
type AgentHandler struct {
|
||||
agent *Agent
|
||||
}
|
||||
|
||||
func (h *AgentHandler) OnTaskScheduled(req *executor.ExecutionRequest) {}
|
||||
|
||||
func (h *AgentHandler) OnTaskExecuting(req *executor.ExecutionRequest) (io.Writer, io.Writer, error) {
|
||||
if req.LogID != "" {
|
||||
writer := &RealTimeLogWriter{agent: h.agent, logID: req.LogID}
|
||||
return writer, writer, nil
|
||||
}
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
func (h *AgentHandler) OnTaskHeartbeat(req *executor.ExecutionRequest, duration int64) {
|
||||
if req.LogID != "" {
|
||||
h.agent.sendWSMessage(WSTypeTaskHeartbeat, map[string]interface{}{
|
||||
"log_id": req.LogID,
|
||||
"duration": duration,
|
||||
})
|
||||
}
|
||||
|
||||
// 每分钟打印一次任务还在运行的日志,提升长任务的存在感
|
||||
if duration >= 60000 && (duration/60000 > (duration-3000)/60000) {
|
||||
logger.Infof("[Scheduler] 任务 #%s 仍在运行中... (已耗时: %v)",
|
||||
req.TaskID, (time.Duration(duration) * time.Millisecond).Round(time.Second))
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AgentHandler) OnTaskStarted(req *executor.ExecutionRequest) {}
|
||||
|
||||
func (h *AgentHandler) OnTaskCompleted(req *executor.ExecutionRequest, result *executor.ExecutionResult) {
|
||||
h.agent.sendTaskResult(&TaskResult{
|
||||
TaskID: req.TaskID,
|
||||
LogID: result.LogID,
|
||||
Command: req.Command,
|
||||
Output: result.Output,
|
||||
Error: result.Error,
|
||||
Status: result.Status,
|
||||
Duration: result.Duration,
|
||||
ExitCode: result.ExitCode,
|
||||
StartTime: result.StartTime.Unix(),
|
||||
EndTime: result.EndTime.Unix(),
|
||||
})
|
||||
|
||||
if result.Status == constant.TaskStatusFailed {
|
||||
h.agent.printLastLogs(result.LogID)
|
||||
}
|
||||
h.agent.clearTaskLog(result.LogID)
|
||||
}
|
||||
|
||||
func (h *AgentHandler) OnTaskFailed(req *executor.ExecutionRequest, err error) {
|
||||
errMsg := fmt.Sprintf("任务执行失败: %v", err)
|
||||
// 先发送日志,确保服务端能收到错误信息
|
||||
h.agent.sendWSMessage(WSTypeTaskLog, map[string]interface{}{
|
||||
"log_id": req.LogID,
|
||||
"content": errMsg,
|
||||
})
|
||||
|
||||
h.agent.sendTaskResult(&TaskResult{
|
||||
TaskID: req.TaskID,
|
||||
LogID: req.LogID,
|
||||
Command: req.Command,
|
||||
Output: "",
|
||||
Error: err.Error(),
|
||||
Status: constant.TaskStatusFailed,
|
||||
Duration: 0,
|
||||
ExitCode: 1,
|
||||
StartTime: time.Now().Unix(),
|
||||
EndTime: time.Now().Unix(),
|
||||
})
|
||||
|
||||
h.agent.printLastLogs(req.LogID)
|
||||
h.agent.clearTaskLog(req.LogID)
|
||||
}
|
||||
|
||||
func (h *AgentHandler) OnCronNextRun(req *executor.ExecutionRequest, nextRun time.Time) {}
|
||||
|
||||
func (a *Agent) Start() error {
|
||||
if a.config.Token == "" {
|
||||
return fmt.Errorf("缺少令牌,请在配置文件中设置 token")
|
||||
}
|
||||
|
||||
logger.Infof("机器识别码: %s", a.machineID[:16]+"...")
|
||||
// 调度器暂不在此启动,等待 WebSocket 连接成功并获取到调度配置后再启动
|
||||
go a.wsLoop()
|
||||
|
||||
logger.Info("Agent 已启动 (时区: Asia/Shanghai, 模式: WebSocket)")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Agent) Stop() {
|
||||
close(a.stopCh)
|
||||
a.closeWS()
|
||||
|
||||
a.mu.Lock()
|
||||
started := a.schedulerStarted
|
||||
a.schedulerStarted = false
|
||||
a.mu.Unlock()
|
||||
|
||||
if started {
|
||||
a.cronManager.Stop()
|
||||
a.scheduler.Stop()
|
||||
}
|
||||
logger.Info("Agent 已停止")
|
||||
}
|
||||
|
||||
// wsLoop WebSocket 连接循环
|
||||
func (a *Agent) wsLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-a.stopCh:
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
if err := a.connectWS(); err != nil {
|
||||
logger.Warnf("WebSocket 连接失败: %v,5秒后重试...", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
a.readWS()
|
||||
|
||||
logger.Warn("WebSocket 连接断开,5秒后重连...")
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) connectWS() error {
|
||||
serverURL := a.config.ServerURL
|
||||
wsURL := strings.Replace(serverURL, "http://", "ws://", 1)
|
||||
wsURL = strings.Replace(wsURL, "https://", "wss://", 1)
|
||||
wsURL = fmt.Sprintf("%s/api/agent/ws?token=%s&machine_id=%s", wsURL, url.QueryEscape(a.config.Token), url.QueryEscape(a.machineID))
|
||||
|
||||
logger.Infof("正在连接 WebSocket: %s", wsURL)
|
||||
logger.Infof("Token: %s..., MachineID: %s...", a.config.Token[:8], a.machineID[:16])
|
||||
|
||||
dialer := websocket.Dialer{HandshakeTimeout: 10 * time.Second}
|
||||
conn, resp, err := dialer.Dial(wsURL, nil)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
logger.Errorf("WebSocket 握手失败: HTTP %d, Body: %s", resp.StatusCode, string(bodyBytes))
|
||||
resp.Body.Close()
|
||||
} else {
|
||||
logger.Errorf("WebSocket 连接失败: %v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
a.wsMu.Lock()
|
||||
a.wsConn = conn
|
||||
a.wsStopCh = make(chan struct{})
|
||||
a.wsMu.Unlock()
|
||||
|
||||
logger.Info("WebSocket 已连接")
|
||||
a.sendHeartbeat()
|
||||
go a.heartbeatLoop()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Agent) closeWS() {
|
||||
a.wsMu.Lock()
|
||||
defer a.wsMu.Unlock()
|
||||
if a.wsStopCh != nil {
|
||||
close(a.wsStopCh)
|
||||
a.wsStopCh = nil
|
||||
}
|
||||
if a.wsConn != nil {
|
||||
a.wsConn.Close()
|
||||
a.wsConn = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) readWS() {
|
||||
defer func() {
|
||||
logger.Info("readWS 退出,准备关闭连接")
|
||||
a.closeWS()
|
||||
}()
|
||||
|
||||
for {
|
||||
a.wsMu.Lock()
|
||||
conn := a.wsConn
|
||||
a.wsMu.Unlock()
|
||||
|
||||
if conn == nil {
|
||||
logger.Warn("readWS: wsConn 为 nil")
|
||||
return
|
||||
}
|
||||
|
||||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
logger.Warnf("WebSocket 读取错误: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var msg WSMessage
|
||||
if err := json.Unmarshal(message, &msg); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
a.handleWSMessage(&msg)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) handleWSMessage(msg *WSMessage) {
|
||||
switch msg.Type {
|
||||
case WSTypeConnected:
|
||||
a.handleConnected(msg.Data)
|
||||
case WSTypeHeartbeatAck:
|
||||
a.handleHeartbeatAck(msg.Data)
|
||||
case WSTypeTasks:
|
||||
a.handleTasks(msg.Data)
|
||||
case WSTypeUpdate:
|
||||
logger.Info("收到更新指令,开始更新...")
|
||||
go a.selfUpdate()
|
||||
case WSTypeDisabled:
|
||||
logger.Warn("Agent 已被禁用,清空所有任务")
|
||||
a.clearAllTasks()
|
||||
case WSTypeEnabled:
|
||||
logger.Info("Agent 已被启用,主动拉取任务")
|
||||
a.fetchTasks()
|
||||
case WSTypeExecute:
|
||||
a.handleExecute(msg.Data)
|
||||
case WSTypeStop:
|
||||
a.handleStop(msg.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) fetchTasks() {
|
||||
logger.Info("正在从服务器拉取任务列表...")
|
||||
if err := a.sendWSMessage(WSTypeFetchTasks, map[string]interface{}{}); err != nil {
|
||||
logger.Warnf("请求任务列表失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) handleConnected(data json.RawMessage) {
|
||||
var resp struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
Name string `json:"name"`
|
||||
IsNewAgent bool `json:"is_new_agent"`
|
||||
MachineID string `json:"machine_id"`
|
||||
SchedulerConfig map[string]interface{} `json:"scheduler_config"`
|
||||
}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp.IsNewAgent {
|
||||
logger.Infof("注册成功: Agent #%s, 机器码: %s", resp.AgentID, a.machineID[:16]+"...")
|
||||
} else {
|
||||
logger.Infof("连接成功: Agent #%s (已存在), 机器码: %s", resp.AgentID, a.machineID[:16]+"...")
|
||||
}
|
||||
|
||||
// 更新调度器配置
|
||||
if resp.SchedulerConfig != nil {
|
||||
a.updateSchedulerConfig(resp.SchedulerConfig)
|
||||
}
|
||||
|
||||
a.fetchTasks()
|
||||
}
|
||||
|
||||
func (a *Agent) updateSchedulerConfig(config map[string]interface{}) {
|
||||
// 获取当前配置作为基础
|
||||
currentCfg := a.scheduler.GetConfig()
|
||||
newCfg := currentCfg
|
||||
|
||||
// 更新配置项
|
||||
if val, ok := config["worker_count"]; ok {
|
||||
if v, ok := val.(float64); ok { // JSON 数字解析为 float64
|
||||
newCfg.WorkerCount = int(v)
|
||||
}
|
||||
}
|
||||
if val, ok := config["queue_size"]; ok {
|
||||
if v, ok := val.(float64); ok {
|
||||
newCfg.QueueSize = int(v)
|
||||
}
|
||||
}
|
||||
if val, ok := config["rate_interval"]; ok {
|
||||
if v, ok := val.(float64); ok {
|
||||
newCfg.RateInterval = time.Duration(v) * time.Millisecond
|
||||
}
|
||||
}
|
||||
if val, ok := config["strict_queue"]; ok {
|
||||
if v, ok := val.(bool); ok {
|
||||
newCfg.StrictQueue = v
|
||||
}
|
||||
}
|
||||
|
||||
a.mu.Lock()
|
||||
started := a.schedulerStarted
|
||||
a.schedulerStarted = true
|
||||
a.mu.Unlock()
|
||||
|
||||
if !started {
|
||||
logger.Infof("首次连接成功,启动调度器配置: workers=%d, queue=%d, rate=%v, strict=%t",
|
||||
newCfg.WorkerCount, newCfg.QueueSize, newCfg.RateInterval, newCfg.StrictQueue)
|
||||
// 用下发的最新配置加载并启动调度器与计划任务管理器
|
||||
a.scheduler.Reload(newCfg)
|
||||
a.cronManager.Start()
|
||||
} else if newCfg != currentCfg {
|
||||
logger.Infof("收到调度配置更新: workers=%d, queue=%d, rate=%v, strict=%t",
|
||||
newCfg.WorkerCount, newCfg.QueueSize, newCfg.RateInterval, newCfg.StrictQueue)
|
||||
a.scheduler.Reload(newCfg)
|
||||
} else {
|
||||
logger.Infof("当前调度配置未改变: workers=%d, queue=%d, rate=%v, strict=%t",
|
||||
newCfg.WorkerCount, newCfg.QueueSize, newCfg.RateInterval, newCfg.StrictQueue)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) handleHeartbeatAck(data json.RawMessage) {
|
||||
var resp struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
Name string `json:"name"`
|
||||
NeedUpdate bool `json:"need_update"`
|
||||
ForceUpdate bool `json:"force_update"`
|
||||
LatestVersion string `json:"latest_version"`
|
||||
}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp.NeedUpdate && (a.config.AutoUpdate || resp.ForceUpdate) {
|
||||
logger.Infof("发现新版本 %s,开始更新...", resp.LatestVersion)
|
||||
go a.selfUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) handleTasks(data json.RawMessage) {
|
||||
var resp struct {
|
||||
Tasks []AgentTask `json:"tasks"`
|
||||
}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
newCount := len(resp.Tasks)
|
||||
if newCount != a.lastTaskCount || newCount == 0 {
|
||||
logger.Infof("任务列表同步成功: 共获取到 %d 个任务", newCount)
|
||||
a.lastTaskCount = newCount
|
||||
}
|
||||
|
||||
a.updateTasks(resp.Tasks)
|
||||
}
|
||||
|
||||
func (a *Agent) handleExecute(data json.RawMessage) {
|
||||
var req struct {
|
||||
TaskID string `json:"task_id"`
|
||||
LogID string `json:"log_id"`
|
||||
Envs string `json:"envs"`
|
||||
Secrets []string `json:"secrets"`
|
||||
Command string `json:"command"`
|
||||
PreCommand string `json:"pre_command"`
|
||||
PostCommand string `json:"post_command"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &req); err != nil {
|
||||
logger.Errorf("解析立即执行请求失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 查找任务
|
||||
a.mu.RLock()
|
||||
task, exists := a.tasks[req.TaskID]
|
||||
a.mu.RUnlock()
|
||||
|
||||
if !exists {
|
||||
logger.Warnf("任务 #%s 不存在,无法执行", req.TaskID)
|
||||
return
|
||||
}
|
||||
|
||||
// 准备执行请求
|
||||
// 如果消息中携带了环境变量或指令,则优先使用(确保即时生效)
|
||||
envs := task.Envs
|
||||
if req.Envs != "" {
|
||||
envs = req.Envs
|
||||
}
|
||||
|
||||
command := task.Command
|
||||
if req.Command != "" {
|
||||
command = req.Command
|
||||
}
|
||||
|
||||
preCommand := task.PreCommand
|
||||
if req.PreCommand != "" {
|
||||
preCommand = req.PreCommand
|
||||
}
|
||||
|
||||
postCommand := task.PostCommand
|
||||
if req.PostCommand != "" {
|
||||
postCommand = req.PostCommand
|
||||
}
|
||||
|
||||
execReq := &executor.ExecutionRequest{
|
||||
TaskID: task.ID,
|
||||
LogID: req.LogID,
|
||||
Name: task.Name,
|
||||
Command: command,
|
||||
PreCommand: preCommand,
|
||||
PostCommand: postCommand,
|
||||
WorkDir: task.WorkDir,
|
||||
Envs: executor.ParseEnvVars(envs),
|
||||
Secrets: req.Secrets,
|
||||
Timeout: task.Timeout,
|
||||
Languages: task.Languages,
|
||||
UseMise: task.UseMise(),
|
||||
Type: executor.TaskTypeManual,
|
||||
}
|
||||
|
||||
// 立即执行任务(加入队列)
|
||||
a.scheduler.EnqueueOrExecute(execReq)
|
||||
}
|
||||
|
||||
func (a *Agent) handleStop(data json.RawMessage) {
|
||||
var req struct {
|
||||
LogID string `json:"log_id"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &req); err != nil {
|
||||
logger.Errorf("解析停止请求失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof("[Agent] 收到停止指令 LogID: %s", req.LogID)
|
||||
if a.scheduler.StopLog(req.LogID) {
|
||||
logger.Infof("[Agent] 任务执行 #%s 已成功停止", req.LogID)
|
||||
} else {
|
||||
logger.Warnf("[Agent] 任务执行 #%s 停止失败(可能已完成或不在运行队列中)", req.LogID)
|
||||
}
|
||||
}
|
||||
|
||||
// RealTimeLogWriter 实时日志写入器,通过 WebSocket 发送日志
|
||||
type RealTimeLogWriter struct {
|
||||
agent *Agent
|
||||
logID string
|
||||
}
|
||||
|
||||
func (w *RealTimeLogWriter) Write(p []byte) (n int, err error) {
|
||||
if len(p) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 记录到本地缓存,用于失败时显示
|
||||
w.agent.addTaskLog(w.logID, p)
|
||||
|
||||
// 构造消息
|
||||
msg := map[string]interface{}{
|
||||
"log_id": w.logID,
|
||||
"content": string(p),
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
if err := w.agent.sendWSMessage(WSTypeTaskLog, msg); err != nil {
|
||||
// 如果发送失败,不阻塞程序执行,只记录日志
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (a *Agent) sendWSMessage(msgType string, data interface{}) error {
|
||||
a.wsMu.Lock()
|
||||
defer a.wsMu.Unlock()
|
||||
|
||||
if a.wsConn == nil {
|
||||
return fmt.Errorf("WebSocket 未连接")
|
||||
}
|
||||
|
||||
dataBytes, _ := json.Marshal(data)
|
||||
msg := WSMessage{Type: msgType, Data: dataBytes}
|
||||
msgBytes, _ := json.Marshal(msg)
|
||||
|
||||
a.wsConn.SetWriteDeadline(time.Now().Add(10 * time.Second))
|
||||
if err := a.wsConn.WriteMessage(websocket.TextMessage, msgBytes); err != nil {
|
||||
logger.Warnf("发送消息失败 (%s): %v", msgType, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Agent) heartbeatLoop() {
|
||||
ticker := time.NewTicker(time.Duration(a.config.Interval) * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
a.wsMu.Lock()
|
||||
wsStopCh := a.wsStopCh
|
||||
a.wsMu.Unlock()
|
||||
|
||||
if wsStopCh == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-a.stopCh:
|
||||
return
|
||||
case <-wsStopCh:
|
||||
return
|
||||
case <-ticker.C:
|
||||
a.wsMu.Lock()
|
||||
conn := a.wsConn
|
||||
a.wsMu.Unlock()
|
||||
if conn == nil {
|
||||
return
|
||||
}
|
||||
a.sendHeartbeat()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) sendHeartbeat() {
|
||||
hostname, _ := os.Hostname()
|
||||
data := map[string]interface{}{
|
||||
"version": Version,
|
||||
"build_time": BuildTime,
|
||||
"hostname": hostname,
|
||||
"os": runtime.GOOS,
|
||||
"arch": runtime.GOARCH,
|
||||
"auto_update": a.config.AutoUpdate,
|
||||
}
|
||||
if err := a.sendWSMessage(WSTypeHeartbeat, data); err != nil {
|
||||
logger.Warnf("发送心跳失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) sendTaskResult(result *TaskResult) {
|
||||
if err := a.sendWSMessage(WSTypeTaskResult, result); err != nil {
|
||||
logger.Warnf("发送任务结果失败: %v,尝试 HTTP 上报", err)
|
||||
a.reportResultHTTP(result)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) reportResultHTTP(result *TaskResult) error {
|
||||
resp, err := a.doRequest("POST", "/api/agent/report", result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Agent) updateTasks(tasks []AgentTask) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
newTasks := make(map[string]*AgentTask)
|
||||
for i := range tasks {
|
||||
newTasks[tasks[i].ID] = &tasks[i]
|
||||
}
|
||||
|
||||
// 1. 移除不再存在的任务
|
||||
for id := range a.tasks {
|
||||
if _, exists := newTasks[id]; !exists {
|
||||
a.cronManager.RemoveTask(id)
|
||||
delete(a.tasks, id)
|
||||
logger.Infof("移除调度任务 #%s", id)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 添加或更新任务
|
||||
for id, task := range newTasks {
|
||||
oldTask, exists := a.tasks[id]
|
||||
if !exists || oldTask.Schedule != task.Schedule || oldTask.Command != task.Command ||
|
||||
oldTask.PreCommand != task.PreCommand || oldTask.PostCommand != task.PostCommand ||
|
||||
oldTask.Enabled != task.Enabled || oldTask.Timeout != task.Timeout ||
|
||||
oldTask.WorkDir != task.WorkDir || oldTask.Envs != task.Envs ||
|
||||
oldTask.RandomRange != task.RandomRange {
|
||||
if task.Enabled {
|
||||
err := a.cronManager.AddTask(task)
|
||||
if err != nil {
|
||||
logger.Errorf("添加调度任务 #%s 失败: %v", id, err)
|
||||
continue
|
||||
}
|
||||
logger.Infof("已添加调度任务 #%s %s (%s)", id, task.Name, task.GetSchedule())
|
||||
} else {
|
||||
a.cronManager.RemoveTask(id)
|
||||
logger.Infof("调度任务 #%s 已禁用", id)
|
||||
}
|
||||
a.tasks[id] = task
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) clearAllTasks() {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
for id := range a.tasks {
|
||||
a.cronManager.RemoveTask(id)
|
||||
logger.Infof("移除任务 #%s", id)
|
||||
}
|
||||
|
||||
a.tasks = make(map[string]*AgentTask)
|
||||
a.lastTaskCount = 0
|
||||
logger.Info("所有任务已清空")
|
||||
}
|
||||
|
||||
func (a *Agent) addTaskLog(logID string, p []byte) {
|
||||
if logID == "" {
|
||||
return
|
||||
}
|
||||
a.logMu.Lock()
|
||||
defer a.logMu.Unlock()
|
||||
|
||||
content := string(p)
|
||||
lines := strings.Split(strings.TrimSuffix(content, "\n"), "\n")
|
||||
|
||||
a.taskLogs[logID] = append(a.taskLogs[logID], lines...)
|
||||
if len(a.taskLogs[logID]) > 50 {
|
||||
a.taskLogs[logID] = a.taskLogs[logID][len(a.taskLogs[logID])-50:]
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) printLastLogs(logID string) {
|
||||
if logID == "" {
|
||||
return
|
||||
}
|
||||
a.logMu.Lock()
|
||||
lines, ok := a.taskLogs[logID]
|
||||
a.logMu.Unlock()
|
||||
|
||||
if !ok || len(lines) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Errorf("--- 任务 #%s 失败日志预览 (最近 %d 行) ---", logID, len(lines))
|
||||
for _, line := range lines {
|
||||
fmt.Println(" " + line)
|
||||
}
|
||||
logger.Errorf("--- 任务 #%s 结束 ---", logID)
|
||||
}
|
||||
|
||||
func (a *Agent) clearTaskLog(logID string) {
|
||||
if logID == "" {
|
||||
return
|
||||
}
|
||||
a.logMu.Lock()
|
||||
defer a.logMu.Unlock()
|
||||
delete(a.taskLogs, logID)
|
||||
}
|
||||
|
||||
// executeTask 已被 AgentHandler.OnTaskCompleted 代替,此处删除旧实现
|
||||
|
||||
func (a *Agent) doRequest(method, path string, body interface{}) (*http.Response, error) {
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
data, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bodyReader = bytes.NewReader(data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, a.config.ServerURL+path, bodyReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+a.config.Token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Machine-ID", a.machineID)
|
||||
|
||||
return a.client.Do(req)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
[agent]
|
||||
# 主服务器地址(http/https,Agent 会自动转换为 WebSocket 连接)
|
||||
# 如果主服务配置了url_prefix, 这里要也要加上路径
|
||||
server_url = http://192.168.1.100:8052
|
||||
# 比如 url_prefix=/taskpool
|
||||
; server_url = http://192.168.1.100:8052/taskpool
|
||||
|
||||
# Agent 名称(留空则使用主机名)
|
||||
name =
|
||||
# 注册令牌(首次注册时填写,注册成功后会自动替换为认证 Token)
|
||||
token =
|
||||
# 心跳间隔(秒),默认 30
|
||||
interval = 30
|
||||
# 自动更新(true/false)
|
||||
auto_update = true
|
||||
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
// Config Agent 配置
|
||||
type Config struct {
|
||||
ServerURL string
|
||||
Name string
|
||||
Token string
|
||||
Interval int
|
||||
AutoUpdate bool
|
||||
}
|
||||
|
||||
func loadConfigFile(path string, config *Config) error {
|
||||
cfg, err := ini.Load(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
section := cfg.Section("agent")
|
||||
if v := section.Key("server_url").String(); v != "" {
|
||||
config.ServerURL = v
|
||||
}
|
||||
if v := section.Key("name").String(); v != "" {
|
||||
config.Name = v
|
||||
}
|
||||
if v := section.Key("token").String(); v != "" {
|
||||
config.Token = v
|
||||
}
|
||||
if v := section.Key("interval").String(); v != "" {
|
||||
if i, err := strconv.Atoi(v); err == nil && i > 0 {
|
||||
config.Interval = i
|
||||
}
|
||||
}
|
||||
if v := section.Key("auto_update").String(); v != "" {
|
||||
config.AutoUpdate = v == "true" || v == "1"
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveConfigFile(path string, config *Config) error {
|
||||
dir := filepath.Dir(path)
|
||||
if dir != "" && dir != "." {
|
||||
os.MkdirAll(dir, 0755)
|
||||
}
|
||||
|
||||
cfg := ini.Empty()
|
||||
section := cfg.Section("agent")
|
||||
section.Key("server_url").SetValue(config.ServerURL)
|
||||
section.Key("name").SetValue(config.Name)
|
||||
section.Key("token").SetValue(config.Token)
|
||||
section.Key("interval").SetValue(strconv.Itoa(config.Interval))
|
||||
if config.AutoUpdate {
|
||||
section.Key("auto_update").SetValue("true")
|
||||
} else {
|
||||
section.Key("auto_update").SetValue("false")
|
||||
}
|
||||
|
||||
return cfg.SaveTo(path)
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/engigu/taskpool/internal/systime"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
// 日志实例
|
||||
var loggerInstance *zap.Logger
|
||||
var log *zap.SugaredLogger
|
||||
|
||||
// ANSI 颜色代码
|
||||
const (
|
||||
colorReset = "\033[0m"
|
||||
colorRed = "\033[31m"
|
||||
colorYellow = "\033[33m"
|
||||
colorBlue = "\033[36m"
|
||||
colorGray = "\033[37m"
|
||||
)
|
||||
|
||||
// customCore 实现 zapcore.Core 以提供与 logrus 一模一样的格式
|
||||
type customCore struct {
|
||||
level zapcore.LevelEnabler
|
||||
writer zapcore.WriteSyncer
|
||||
}
|
||||
|
||||
func (c *customCore) Enabled(l zapcore.Level) bool {
|
||||
return c.level.Enabled(l)
|
||||
}
|
||||
|
||||
func (c *customCore) With(fields []zapcore.Field) zapcore.Core {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *customCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||
if c.Enabled(ent.Level) {
|
||||
return ce.AddCore(ent, c)
|
||||
}
|
||||
return ce
|
||||
}
|
||||
|
||||
func (c *customCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
|
||||
// 统一使用东八区时间
|
||||
timestamp := systime.InCST(ent.Time).Format("2006-01-02 15:04:05")
|
||||
level := strings.ToUpper(ent.Level.String())
|
||||
|
||||
var levelColor string
|
||||
switch ent.Level {
|
||||
case zapcore.DebugLevel:
|
||||
levelColor = colorGray
|
||||
case zapcore.InfoLevel:
|
||||
levelColor = colorBlue
|
||||
case zapcore.WarnLevel:
|
||||
levelColor = colorYellow
|
||||
case zapcore.ErrorLevel, zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel:
|
||||
levelColor = colorRed
|
||||
default:
|
||||
levelColor = colorBlue
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("[%s]%s[%s]%s %s\n", timestamp, levelColor, level, colorReset, ent.Message)
|
||||
_, err := c.writer.Write([]byte(msg))
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *customCore) Sync() error {
|
||||
return c.writer.Sync()
|
||||
}
|
||||
|
||||
func initLogger(logFile string, fileOnly bool) {
|
||||
logDir := filepath.Dir(logFile)
|
||||
if logDir != "" && logDir != "." {
|
||||
os.MkdirAll(logDir, 0755)
|
||||
}
|
||||
|
||||
lumberjackLogger := &lumberjack.Logger{
|
||||
Filename: logFile,
|
||||
MaxSize: 5,
|
||||
MaxBackups: 3,
|
||||
MaxAge: 0,
|
||||
Compress: false,
|
||||
}
|
||||
|
||||
var output zapcore.WriteSyncer
|
||||
// fileOnly 模式下只输出到文件(daemon 模式或重启模式)
|
||||
if fileOnly {
|
||||
output = zapcore.AddSync(lumberjackLogger)
|
||||
} else {
|
||||
// 前台运行时同时输出到终端和文件
|
||||
output = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(lumberjackLogger))
|
||||
}
|
||||
|
||||
core := &customCore{
|
||||
level: zap.NewAtomicLevelAt(zap.InfoLevel),
|
||||
writer: output,
|
||||
}
|
||||
|
||||
loggerInstance = zap.New(core)
|
||||
log = loggerInstance.Sugar()
|
||||
}
|
||||
+437
@@ -0,0 +1,437 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
internalLogger "github.com/engigu/taskpool/internal/logger"
|
||||
"github.com/engigu/taskpool/internal/systime"
|
||||
"github.com/engigu/taskpool/internal/utils"
|
||||
)
|
||||
|
||||
const ServiceName = "taskpool-agent"
|
||||
const ServiceDesc = "TaskPool Agent Service"
|
||||
|
||||
// 版本信息(通过 ldflags 注入)
|
||||
var (
|
||||
Version = "dev"
|
||||
BuildTime = ""
|
||||
)
|
||||
|
||||
// 全局配置
|
||||
var (
|
||||
configFile = "config.ini"
|
||||
logFile = "logs/agent.log"
|
||||
dataDir = "data"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 强制设置全局时区为东八区
|
||||
time.Local = systime.CST
|
||||
exePath, _ := os.Executable()
|
||||
exeDir := filepath.Dir(exePath)
|
||||
os.Chdir(exeDir)
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
printUsage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cmd := os.Args[1]
|
||||
|
||||
// 解析额外参数
|
||||
for i := 2; i < len(os.Args); i++ {
|
||||
switch os.Args[i] {
|
||||
case "-c", "--config":
|
||||
if i+1 < len(os.Args) {
|
||||
configFile = os.Args[i+1]
|
||||
i++
|
||||
}
|
||||
case "-l", "--log":
|
||||
if i+1 < len(os.Args) {
|
||||
logFile = os.Args[i+1]
|
||||
i++
|
||||
}
|
||||
case "-d", "--daemon":
|
||||
isDaemon = true
|
||||
case "--restart":
|
||||
isRestart = true
|
||||
}
|
||||
}
|
||||
|
||||
switch cmd {
|
||||
case "start":
|
||||
cmdStart()
|
||||
case "run":
|
||||
cmdRun()
|
||||
case "stop":
|
||||
cmdStop()
|
||||
case "status":
|
||||
cmdStatus()
|
||||
case "tasks":
|
||||
cmdTasks()
|
||||
case "logs":
|
||||
cmdLogs()
|
||||
case "install":
|
||||
cmdInstall()
|
||||
case "uninstall":
|
||||
cmdUninstall()
|
||||
case "version", "-v", "--version":
|
||||
fmt.Printf("TaskPool Agent v%s\n", Version)
|
||||
if BuildTime != "" {
|
||||
fmt.Printf("Build Time: %s\n", BuildTime)
|
||||
}
|
||||
case "help", "-h", "--help":
|
||||
printUsage()
|
||||
default:
|
||||
fmt.Printf("未知命令: %s\n", cmd)
|
||||
printUsage()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func printUsage() {
|
||||
var binName = filepath.Base(os.Args[0])
|
||||
if binName == "main" || binName == "debug" {
|
||||
binName = "taskpool-agent"
|
||||
}
|
||||
|
||||
fmt.Printf(`TaskPool Agent v%s
|
||||
|
||||
用法:
|
||||
%s <命令> [选项]
|
||||
|
||||
命令:
|
||||
start 启动 Agent(后台运行)
|
||||
run 前台运行 Agent
|
||||
stop 停止 Agent
|
||||
status 查看运行状态
|
||||
tasks 查看已下发的任务列表
|
||||
logs 查看日志(实时跟踪)
|
||||
install 安装为系统服务(开机自启)
|
||||
uninstall 卸载系统服务
|
||||
version 显示版本信息
|
||||
help 显示帮助信息
|
||||
|
||||
选项:
|
||||
-c, --config <file> 配置文件路径 (默认: config.ini)
|
||||
-l, --log <file> 日志文件路径 (默认: logs/agent.log)
|
||||
|
||||
示例:
|
||||
%s start
|
||||
%s run
|
||||
%s stop
|
||||
%s logs
|
||||
%s start -c /etc/taskpool/config.ini
|
||||
%s install
|
||||
%s status
|
||||
%s tasks
|
||||
`, Version, binName, binName, binName, binName, binName, binName, binName, binName, binName)
|
||||
}
|
||||
|
||||
// daemon 模式标记
|
||||
var isDaemon = false
|
||||
|
||||
// 是否从 daemon 重启(用于自动更新后重启)
|
||||
var isRestart = false
|
||||
|
||||
func cmdStart() {
|
||||
// 检查是否已经在运行(使用文件锁)
|
||||
pid := readPidFile()
|
||||
if pid != 0 && isProcessRunning(pid) {
|
||||
fmt.Printf("Agent 已在运行 (PID: %d)\n", pid)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果不是 daemon 子进程,则启动 daemon
|
||||
if !isDaemon {
|
||||
startDaemon()
|
||||
return
|
||||
}
|
||||
|
||||
// 以下是 daemon 子进程的逻辑
|
||||
// 尝试获取文件锁
|
||||
if !tryLock() {
|
||||
fmt.Println("Agent 已在运行(无法获取锁)")
|
||||
return
|
||||
}
|
||||
defer unlock()
|
||||
|
||||
initLogger(logFile, true)
|
||||
internalLogger.SetOutput(loggerInstance)
|
||||
|
||||
config := &Config{Interval: 30}
|
||||
if err := loadConfigFile(configFile, config); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
log.Warnf("加载配置文件失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 从环境变量加载
|
||||
if v := os.Getenv("AGENT_SERVER"); v != "" {
|
||||
config.ServerURL = v
|
||||
}
|
||||
if v := os.Getenv("AGENT_NAME"); v != "" {
|
||||
config.Name = v
|
||||
}
|
||||
|
||||
if config.ServerURL == "" {
|
||||
log.Fatal("请在配置文件中设置 server_url")
|
||||
}
|
||||
if config.Name == "" {
|
||||
hostname, _ := os.Hostname()
|
||||
config.Name = hostname
|
||||
}
|
||||
|
||||
log.Infof("TaskPool Agent Version: %s", Version)
|
||||
if BuildTime != "" {
|
||||
log.Infof("构建时间: %s", BuildTime)
|
||||
}
|
||||
log.Infof("服务器: %s", config.ServerURL)
|
||||
log.Infof("名称: %s", config.Name)
|
||||
|
||||
writePidFile()
|
||||
|
||||
agent := NewAgent(config, configFile)
|
||||
if err := agent.Start(); err != nil {
|
||||
log.Fatalf("启动失败: %v", err)
|
||||
}
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
log.Info("正在停止...")
|
||||
agent.Stop()
|
||||
removePidFile()
|
||||
}
|
||||
|
||||
// cmdRun 前台运行
|
||||
func cmdRun() {
|
||||
// 检查是否已经在运行(重启模式下跳过检查)
|
||||
if !isRestart {
|
||||
pid := readPidFile()
|
||||
if pid != 0 && isProcessRunning(pid) {
|
||||
fmt.Printf("Agent 已在运行 (PID: %d)\n", pid)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试获取文件锁
|
||||
if !tryLock() {
|
||||
fmt.Println("Agent 已在运行(无法获取锁)")
|
||||
return
|
||||
}
|
||||
defer unlock()
|
||||
|
||||
// 前台模式始终输出到终端+文件
|
||||
initLogger(logFile, false)
|
||||
internalLogger.SetOutput(loggerInstance)
|
||||
|
||||
config := &Config{Interval: 30}
|
||||
if err := loadConfigFile(configFile, config); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
log.Warnf("加载配置文件失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if v := os.Getenv("AGENT_SERVER"); v != "" {
|
||||
config.ServerURL = v
|
||||
}
|
||||
if v := os.Getenv("AGENT_NAME"); v != "" {
|
||||
config.Name = v
|
||||
}
|
||||
|
||||
if config.ServerURL == "" {
|
||||
log.Fatal("请在配置文件中设置 server_url")
|
||||
}
|
||||
if config.Name == "" {
|
||||
hostname, _ := os.Hostname()
|
||||
config.Name = hostname
|
||||
}
|
||||
|
||||
log.Infof("TaskPool Agent Version: %s", Version)
|
||||
if BuildTime != "" {
|
||||
log.Infof("构建时间: %s", BuildTime)
|
||||
}
|
||||
log.Infof("服务器: %s", config.ServerURL)
|
||||
log.Infof("名称: %s", config.Name)
|
||||
|
||||
writePidFile()
|
||||
|
||||
agent := NewAgent(config, configFile)
|
||||
if err := agent.Start(); err != nil {
|
||||
log.Fatalf("启动失败: %v", err)
|
||||
}
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
log.Info("正在停止...")
|
||||
agent.Stop()
|
||||
removePidFile()
|
||||
}
|
||||
|
||||
func startDaemon() {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
fmt.Printf("获取可执行文件路径失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 构建子进程参数,添加 --daemon 标记
|
||||
args := []string{"start", "--daemon"}
|
||||
for i := 2; i < len(os.Args); i++ {
|
||||
if os.Args[i] != "--daemon" && os.Args[i] != "-d" {
|
||||
args = append(args, os.Args[i])
|
||||
}
|
||||
}
|
||||
|
||||
// 打开 /dev/null 用于丢弃输出(日志由 logger 写入文件)
|
||||
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
fmt.Printf("打开 /dev/null 失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 启动子进程
|
||||
cmd := &exec.Cmd{
|
||||
Path: exePath,
|
||||
Args: append([]string{exePath}, args...),
|
||||
Dir: filepath.Dir(exePath),
|
||||
Stdout: devNull,
|
||||
Stderr: devNull,
|
||||
}
|
||||
|
||||
// 设置进程组,使子进程独立运行 (跨平台兼容写法)
|
||||
attr := &syscall.SysProcAttr{}
|
||||
if field := reflect.ValueOf(attr).Elem().FieldByName("Setsid"); field.IsValid() {
|
||||
field.SetBool(true)
|
||||
}
|
||||
cmd.SysProcAttr = attr
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
fmt.Printf("启动失败: %v\n", err)
|
||||
devNull.Close()
|
||||
return
|
||||
}
|
||||
|
||||
devNull.Close()
|
||||
fmt.Printf("Agent 已启动 (PID: %d)\n", cmd.Process.Pid)
|
||||
fmt.Printf("日志文件: %s\n", logFile)
|
||||
}
|
||||
|
||||
func cmdTasks() {
|
||||
config := &Config{Interval: 30}
|
||||
if err := loadConfigFile(configFile, config); err != nil {
|
||||
fmt.Printf("加载配置文件失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if config.ServerURL == "" {
|
||||
fmt.Println("错误: 缺少服务器地址,请在配置文件中设置 server_url")
|
||||
return
|
||||
}
|
||||
|
||||
if config.Token == "" {
|
||||
fmt.Println("错误: 缺少令牌,请在配置文件中设置 token")
|
||||
return
|
||||
}
|
||||
|
||||
agent := &Agent{
|
||||
config: config,
|
||||
machineID: utils.GenerateMachineID(),
|
||||
client: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
|
||||
resp, err := agent.doRequest("GET", "/api/agent/tasks", nil)
|
||||
if err != nil {
|
||||
fmt.Printf("获取任务列表失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Printf("获取任务列表失败 (HTTP %d): %s\n", resp.StatusCode, string(body))
|
||||
return
|
||||
}
|
||||
|
||||
// 解析服务端响应(包含 code/msg/data 包装)
|
||||
var apiResp struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
Tasks []AgentTask `json:"tasks"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
fmt.Printf("解析响应失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if apiResp.Code != 200 {
|
||||
fmt.Printf("获取任务列表失败: %s\n", apiResp.Msg)
|
||||
return
|
||||
}
|
||||
|
||||
tasks := apiResp.Data.Tasks
|
||||
if len(tasks) == 0 {
|
||||
fmt.Println("当前没有下发的任务")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("共 %d 个任务:\n\n", len(tasks))
|
||||
for i, task := range tasks {
|
||||
fmt.Printf("[%d] ID: %s\n", i+1, task.ID)
|
||||
fmt.Printf(" 名称: %s\n", task.Name)
|
||||
fmt.Printf(" Cron: %s\n", task.Schedule)
|
||||
fmt.Printf(" 命令: %s\n", task.Command)
|
||||
if task.WorkDir != "" {
|
||||
fmt.Printf(" 工作目录: %s\n", task.WorkDir)
|
||||
}
|
||||
fmt.Printf(" 启用: %v\n", task.Enabled)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func cmdLogs() {
|
||||
// 检查日志文件是否存在
|
||||
if _, err := os.Stat(logFile); os.IsNotExist(err) {
|
||||
fmt.Printf("日志文件不存在: %s\n", logFile)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("日志文件: %s\n", logFile)
|
||||
fmt.Println("按 Ctrl+C 退出")
|
||||
|
||||
// 使用 tail -f 实时跟踪日志
|
||||
cmd := exec.Command("tail", "-f", "-n", "50", logFile)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
// 处理中断信号
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-quit
|
||||
if cmd.Process != nil {
|
||||
cmd.Process.Kill()
|
||||
}
|
||||
}()
|
||||
|
||||
cmd.Run()
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/gofrs/flock"
|
||||
)
|
||||
|
||||
// ========== PID 文件管理 ==========
|
||||
|
||||
var fileLock *flock.Flock
|
||||
|
||||
func getPidFile() string {
|
||||
return filepath.Join(dataDir, "agent.pid")
|
||||
}
|
||||
|
||||
func getLockFile() string {
|
||||
return filepath.Join(dataDir, "agent.lock")
|
||||
}
|
||||
|
||||
// tryLock 尝试获取文件锁,确保只有一个实例运行
|
||||
func tryLock() bool {
|
||||
os.MkdirAll(dataDir, 0755)
|
||||
|
||||
fileLock = flock.New(getLockFile())
|
||||
locked, err := fileLock.TryLock()
|
||||
if err != nil || !locked {
|
||||
fileLock = nil
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// unlock 释放文件锁
|
||||
func unlock() {
|
||||
if fileLock != nil {
|
||||
fileLock.Unlock()
|
||||
fileLock = nil
|
||||
os.Remove(getLockFile())
|
||||
}
|
||||
}
|
||||
|
||||
func writePidFile() {
|
||||
os.MkdirAll(dataDir, 0755)
|
||||
pidFile := getPidFile()
|
||||
os.WriteFile(pidFile, []byte(strconv.Itoa(os.Getpid())), 0644)
|
||||
}
|
||||
|
||||
func readPidFile() int {
|
||||
pidFile := getPidFile()
|
||||
data, err := os.ReadFile(pidFile)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
pid, _ := strconv.Atoi(string(data))
|
||||
return pid
|
||||
}
|
||||
|
||||
func removePidFile() {
|
||||
os.Remove(getPidFile())
|
||||
}
|
||||
|
||||
// ========== 命令实现 ==========
|
||||
|
||||
func cmdStop() {
|
||||
pid := readPidFile()
|
||||
if pid == 0 {
|
||||
fmt.Println("Agent 未运行")
|
||||
return
|
||||
}
|
||||
|
||||
process, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
fmt.Printf("找不到进程 %d\n", pid)
|
||||
removePidFile()
|
||||
return
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
err = process.Kill()
|
||||
} else {
|
||||
err = process.Signal(syscall.SIGTERM)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("停止失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Agent 已停止")
|
||||
removePidFile()
|
||||
}
|
||||
|
||||
func cmdStatus() {
|
||||
pid := readPidFile()
|
||||
if pid == 0 {
|
||||
fmt.Println("状态: 未运行")
|
||||
return
|
||||
}
|
||||
|
||||
if !isProcessRunning(pid) {
|
||||
fmt.Println("状态: 未运行")
|
||||
removePidFile()
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("状态: 运行中 (PID: %d)\n", pid)
|
||||
}
|
||||
|
||||
func isProcessRunning(pid int) bool {
|
||||
process, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Unix 系统发送信号 0 检查进程
|
||||
if runtime.GOOS != "windows" {
|
||||
err = process.Signal(syscall.Signal(0))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// Windows 下 FindProcess 成功即表示进程存在
|
||||
return true
|
||||
}
|
||||
|
||||
func cmdInstall() {
|
||||
exePath, _ := os.Executable()
|
||||
exeDir := filepath.Dir(exePath)
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
installWindows(exePath, exeDir)
|
||||
} else {
|
||||
installLinux(exePath, exeDir)
|
||||
}
|
||||
}
|
||||
|
||||
func cmdUninstall() {
|
||||
if runtime.GOOS == "windows" {
|
||||
uninstallWindows()
|
||||
} else {
|
||||
uninstallLinux()
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Linux systemd ==========
|
||||
|
||||
func installLinux(exePath, exeDir string) {
|
||||
serviceContent := fmt.Sprintf(`[Unit]
|
||||
Description=%s
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
WorkingDirectory=%s
|
||||
ExecStart=%s run
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
`, ServiceDesc, exeDir, exePath)
|
||||
|
||||
servicePath := fmt.Sprintf("/etc/systemd/system/%s.service", ServiceName)
|
||||
if err := os.WriteFile(servicePath, []byte(serviceContent), 0644); err != nil {
|
||||
fmt.Printf("创建服务文件失败: %v\n", err)
|
||||
fmt.Println("请使用 sudo 运行")
|
||||
return
|
||||
}
|
||||
|
||||
// 重载 systemd
|
||||
exec.Command("systemctl", "daemon-reload").Run()
|
||||
exec.Command("systemctl", "enable", ServiceName).Run()
|
||||
|
||||
fmt.Printf("服务已安装: %s\n", servicePath)
|
||||
fmt.Println("使用以下命令管理服务:")
|
||||
fmt.Printf(" 启动: sudo systemctl start %s\n", ServiceName)
|
||||
fmt.Printf(" 停止: sudo systemctl stop %s\n", ServiceName)
|
||||
fmt.Printf(" 状态: sudo systemctl status %s\n", ServiceName)
|
||||
}
|
||||
|
||||
func uninstallLinux() {
|
||||
// 停止服务
|
||||
exec.Command("systemctl", "stop", ServiceName).Run()
|
||||
exec.Command("systemctl", "disable", ServiceName).Run()
|
||||
|
||||
servicePath := fmt.Sprintf("/etc/systemd/system/%s.service", ServiceName)
|
||||
if err := os.Remove(servicePath); err != nil {
|
||||
fmt.Printf("删除服务文件失败: %v\n", err)
|
||||
fmt.Println("请使用 sudo 运行")
|
||||
return
|
||||
}
|
||||
|
||||
exec.Command("systemctl", "daemon-reload").Run()
|
||||
fmt.Println("服务已卸载")
|
||||
}
|
||||
|
||||
// ========== Windows 服务 ==========
|
||||
|
||||
func installWindows(exePath, exeDir string) {
|
||||
// 使用 sc.exe 创建服务
|
||||
cmd := exec.Command("sc", "create", ServiceName,
|
||||
"binPath=", fmt.Sprintf(`"%s" run`, exePath),
|
||||
"start=", "auto",
|
||||
"DisplayName=", ServiceDesc)
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Printf("创建服务失败: %v\n", err)
|
||||
fmt.Println("请以管理员身份运行")
|
||||
return
|
||||
}
|
||||
|
||||
// 设置服务描述
|
||||
exec.Command("sc", "description", ServiceName, ServiceDesc).Run()
|
||||
|
||||
fmt.Println("服务已安装")
|
||||
fmt.Println("使用以下命令管理服务:")
|
||||
fmt.Printf(" 启动: sc start %s\n", ServiceName)
|
||||
fmt.Printf(" 停止: sc stop %s\n", ServiceName)
|
||||
fmt.Printf(" 状态: sc query %s\n", ServiceName)
|
||||
}
|
||||
|
||||
func uninstallWindows() {
|
||||
// 停止服务
|
||||
exec.Command("sc", "stop", ServiceName).Run()
|
||||
|
||||
// 删除服务
|
||||
cmd := exec.Command("sc", "delete", ServiceName)
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Printf("删除服务失败: %v\n", err)
|
||||
fmt.Println("请以管理员身份运行")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("服务已卸载")
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// selfUpdate 自动更新
|
||||
func (a *Agent) selfUpdate() {
|
||||
// 获取当前可执行文件路径
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Errorf("获取可执行文件路径失败: %v", err)
|
||||
return
|
||||
}
|
||||
exePath, _ = filepath.Abs(exePath)
|
||||
|
||||
// 下载新版本 tar.gz
|
||||
downloadURL := a.config.ServerURL + "/api/agent/download?os=" + runtime.GOOS + "&arch=" + runtime.GOARCH
|
||||
req, err := http.NewRequest("GET", downloadURL, nil)
|
||||
if err != nil {
|
||||
log.Errorf("创建下载请求失败: %v", err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+a.config.Token)
|
||||
|
||||
client := &http.Client{Timeout: 5 * time.Minute}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Errorf("下载新版本失败: %v", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Errorf("下载新版本失败: HTTP %d", resp.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
// 读取 tar.gz 内容
|
||||
gzReader, err := gzip.NewReader(resp.Body)
|
||||
if err != nil {
|
||||
log.Errorf("解压 gzip 失败: %v", err)
|
||||
return
|
||||
}
|
||||
defer gzReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(gzReader)
|
||||
|
||||
// 解压并找到二进制文件
|
||||
var newBinary []byte
|
||||
binaryName := "taskpool-agent"
|
||||
if runtime.GOOS == "windows" {
|
||||
binaryName = "taskpool-agent.exe"
|
||||
}
|
||||
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorf("读取 tar 失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if header.Typeflag == tar.TypeReg && header.Name == binaryName {
|
||||
newBinary, err = io.ReadAll(tarReader)
|
||||
if err != nil {
|
||||
log.Errorf("读取二进制文件失败: %v", err)
|
||||
return
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if newBinary == nil {
|
||||
log.Errorf("tar.gz 中未找到 %s", binaryName)
|
||||
return
|
||||
}
|
||||
|
||||
// 保存到临时文件(放到 data 目录)
|
||||
os.MkdirAll(dataDir, 0755)
|
||||
tmpFile := filepath.Join(dataDir, binaryName+".new")
|
||||
if err := os.WriteFile(tmpFile, newBinary, 0755); err != nil {
|
||||
log.Errorf("保存新版本失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 计算基础路径(去掉所有 .bak 后缀)
|
||||
basePath := exePath
|
||||
for strings.HasSuffix(basePath, ".bak") {
|
||||
basePath = strings.TrimSuffix(basePath, ".bak")
|
||||
}
|
||||
backupFile := basePath + ".bak"
|
||||
|
||||
// 如果当前运行的就是 .bak 文件,直接删除它(更新后会用新版本)
|
||||
// 否则需要备份当前文件
|
||||
if exePath != backupFile {
|
||||
os.Remove(backupFile)
|
||||
if err := os.Rename(exePath, backupFile); err != nil {
|
||||
log.Errorf("备份旧版本失败: %v", err)
|
||||
os.Remove(tmpFile)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 替换为新版本(放到 basePath,即不带 .bak 的路径)
|
||||
if err := os.Rename(tmpFile, basePath); err != nil {
|
||||
log.Errorf("替换新版本失败: %v", err)
|
||||
if exePath != backupFile {
|
||||
os.Rename(backupFile, exePath) // 恢复旧版本
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 如果之前运行的是 .bak 文件,现在可以删除它了
|
||||
if exePath == backupFile {
|
||||
os.Remove(exePath)
|
||||
}
|
||||
|
||||
log.Info("更新完成,正在重启...")
|
||||
|
||||
// 重启服务
|
||||
a.restart()
|
||||
}
|
||||
|
||||
// restart 重启服务
|
||||
func (a *Agent) restart() {
|
||||
exePath, _ := os.Executable()
|
||||
|
||||
// 计算基础路径(去掉所有 .bak 后缀),确保启动的是正确的可执行文件
|
||||
basePath := exePath
|
||||
for strings.HasSuffix(basePath, ".bak") {
|
||||
basePath = strings.TrimSuffix(basePath, ".bak")
|
||||
}
|
||||
|
||||
// 删除 PID 文件,避免新进程检测到旧 PID 而拒绝启动
|
||||
removePidFile()
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
// Windows: 启动新进程后退出
|
||||
cmd := exec.Command(basePath, "start")
|
||||
cmd.Start()
|
||||
os.Exit(0)
|
||||
} else {
|
||||
// Linux/macOS: 使用 exec 替换当前进程,直接运行(不需要 daemon)
|
||||
// 因为 syscall.Exec 会替换当前进程,当前进程本身就是 daemon
|
||||
// --restart 标记告诉新进程这是重启,只输出到文件
|
||||
syscall.Exec(basePath, []string{basePath, "run", "--restart"}, os.Environ())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user