实现 StopUpgrade

This commit is contained in:
2025-12-05 16:53:52 +08:00
parent b4ecee6626
commit 349a31518d
4 changed files with 77 additions and 14 deletions

View File

@@ -2,20 +2,27 @@ package device
import (
"context"
"fmt"
"path/filepath"
"time"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/file"
)
// otaServiceImpl 是 OtaService 接口的实现。
type otaServiceImpl struct {
ctx context.Context
otaRepo repository.OtaRepository
deviceRepo repository.DeviceRepository
}
// NewOtaService 创建一个新的 OtaService 实例。
func NewOtaService(otaRepo repository.OtaRepository, deviceRepo repository.DeviceRepository) OtaService {
func NewOtaService(ctx context.Context, otaRepo repository.OtaRepository, deviceRepo repository.DeviceRepository) OtaService {
return &otaServiceImpl{
ctx: ctx,
otaRepo: otaRepo,
deviceRepo: deviceRepo,
}
@@ -32,6 +39,37 @@ func (o *otaServiceImpl) GetUpgradeProgress(ctx context.Context, taskID uint32)
}
func (o *otaServiceImpl) StopUpgrade(ctx context.Context, taskID uint32) error {
//TODO implement me
panic("implement me")
serviceCtx, logger := logs.Trace(ctx, o.ctx, "StopUpgrade")
task, err := o.otaRepo.FindByID(serviceCtx, taskID)
if err != nil {
logger.Errorf("查找 OTA 任务失败: %v, 任务ID: %d", err, taskID)
return fmt.Errorf("查找 OTA 任务失败: %w", err)
}
// 幂等性检查:如果任务已处于终态,则直接返回成功
if task.IsOver() {
logger.Infof("OTA 任务 %d 已处于终态 %s无需停止", taskID, task.Status)
return nil
}
now := time.Now()
task.Status = models.OTATaskStatusStopped
task.CompletedAt = &now
task.ErrorMessage = "任务被用户手动停止"
if err := o.otaRepo.Update(serviceCtx, task); err != nil {
logger.Errorf("更新 OTA 任务状态失败: %v, 任务ID: %d", err, taskID)
return fmt.Errorf("更新 OTA 任务状态失败: %w", err)
}
// 清理相关文件目录
dirToRemove := filepath.Join(models.OTADir, fmt.Sprintf("%d", taskID))
if err := file.RemoveTempDir(dirToRemove); err != nil {
// 文件清理失败不应阻塞主流程,但需要记录日志
logger.Warnf("清理 OTA 任务 %d 的文件目录 %s 失败: %v", taskID, dirToRemove, err)
}
logger.Infof("OTA 任务 %d 已被成功标记为手动停止", taskID)
return nil
}

View File

@@ -3,6 +3,7 @@ package task
import (
"context"
"fmt"
"path/filepath"
"sync"
"time"
@@ -10,6 +11,7 @@ import (
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/file"
)
// OtaCheckTaskParams 定义了 OTA 检查任务所需的参数。
@@ -91,6 +93,13 @@ func (t *otaCheckTask) Execute(ctx context.Context) error {
if err := t.otaRepo.Update(taskCtx, task); err != nil {
// 仅记录错误,不中断整个检查任务,以确保其他超时任务能被处理
logger.Errorf("更新超时的OTA任务 #%d 状态失败: %v", task.ID, err)
} else {
// 数据库更新成功后,清理文件
logger.Infof("OTA任务 #%d 状态已更新为超时,现在开始清理文件。", task.ID)
dirToRemove := filepath.Join(models.OTADir, fmt.Sprintf("%d", task.ID))
if removeErr := file.RemoveTempDir(dirToRemove); removeErr != nil {
logger.Warnf("清理超时的OTA任务 #%d 的文件目录 %s 失败: %v", task.ID, dirToRemove, removeErr)
}
}
}