uint/uint64全部改为uint32
This commit is contained in:
@@ -18,10 +18,10 @@ type AnalysisPlanTaskManager interface {
|
||||
Refresh(ctx context.Context) error
|
||||
// CreateOrUpdateTrigger 为给定的 planID 创建其关联的触发任务。
|
||||
// 如果触发器已存在,会根据计划类型更新其执行时间。
|
||||
CreateOrUpdateTrigger(ctx context.Context, planID uint) error
|
||||
CreateOrUpdateTrigger(ctx context.Context, planID uint32) error
|
||||
// EnsureAnalysisTaskDefinition 确保计划的分析任务定义存在于 tasks 表中。
|
||||
// 如果不存在,则会自动创建。此方法不涉及待执行队列。
|
||||
EnsureAnalysisTaskDefinition(ctx context.Context, planID uint) error
|
||||
EnsureAnalysisTaskDefinition(ctx context.Context, planID uint32) error
|
||||
}
|
||||
|
||||
// analysisPlanTaskManagerImpl 负责管理分析计划的触发器任务。
|
||||
@@ -82,7 +82,7 @@ func (m *analysisPlanTaskManagerImpl) Refresh(ctx context.Context) error {
|
||||
|
||||
// CreateOrUpdateTrigger 为给定的 planID 创建其关联的触发任务。
|
||||
// 如果触发器已存在,会根据计划类型更新其执行时间。
|
||||
func (m *analysisPlanTaskManagerImpl) CreateOrUpdateTrigger(ctx context.Context, planID uint) error {
|
||||
func (m *analysisPlanTaskManagerImpl) CreateOrUpdateTrigger(ctx context.Context, planID uint32) error {
|
||||
managerCtx, logger := logs.Trace(ctx, m.ctx, "CreateOrUpdateTrigger")
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@@ -138,7 +138,7 @@ func (m *analysisPlanTaskManagerImpl) CreateOrUpdateTrigger(ctx context.Context,
|
||||
|
||||
// EnsureAnalysisTaskDefinition 确保计划的分析任务定义存在于 tasks 表中。
|
||||
// 如果不存在,则会自动创建。此方法不涉及待执行队列。
|
||||
func (m *analysisPlanTaskManagerImpl) EnsureAnalysisTaskDefinition(ctx context.Context, planID uint) error {
|
||||
func (m *analysisPlanTaskManagerImpl) EnsureAnalysisTaskDefinition(ctx context.Context, planID uint32) error {
|
||||
managerCtx, logger := logs.Trace(ctx, m.ctx, "EnsureAnalysisTaskDefinition")
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@@ -170,7 +170,7 @@ func (m *analysisPlanTaskManagerImpl) EnsureAnalysisTaskDefinition(ctx context.C
|
||||
// --- 内部私有方法 ---
|
||||
|
||||
// getRefreshData 从数据库获取刷新所需的所有数据。
|
||||
func (m *analysisPlanTaskManagerImpl) getRefreshData(ctx context.Context) (runnablePlans []*models.Plan, invalidPlanIDs []uint, pendingTasks []models.PendingTask, err error) {
|
||||
func (m *analysisPlanTaskManagerImpl) getRefreshData(ctx context.Context) (runnablePlans []*models.Plan, invalidPlanIDs []uint32, pendingTasks []models.PendingTask, err error) {
|
||||
managerCtx, logger := logs.Trace(ctx, m.ctx, "getRefreshData")
|
||||
runnablePlans, err = m.planRepo.FindRunnablePlans(managerCtx)
|
||||
if err != nil {
|
||||
@@ -183,7 +183,7 @@ func (m *analysisPlanTaskManagerImpl) getRefreshData(ctx context.Context) (runna
|
||||
logger.Errorf("获取失效计划列表失败: %v", err)
|
||||
return
|
||||
}
|
||||
invalidPlanIDs = make([]uint, len(invalidPlans))
|
||||
invalidPlanIDs = make([]uint32, len(invalidPlans))
|
||||
for i, p := range invalidPlans {
|
||||
invalidPlanIDs[i] = p.ID
|
||||
}
|
||||
@@ -197,19 +197,19 @@ func (m *analysisPlanTaskManagerImpl) getRefreshData(ctx context.Context) (runna
|
||||
}
|
||||
|
||||
// cleanupInvalidTasks 清理所有与失效计划相关的待执行任务。
|
||||
func (m *analysisPlanTaskManagerImpl) cleanupInvalidTasks(ctx context.Context, invalidPlanIDs []uint, allPendingTasks []models.PendingTask) error {
|
||||
func (m *analysisPlanTaskManagerImpl) cleanupInvalidTasks(ctx context.Context, invalidPlanIDs []uint32, allPendingTasks []models.PendingTask) error {
|
||||
managerCtx, logger := logs.Trace(ctx, m.ctx, "cleanupInvalidTasks")
|
||||
if len(invalidPlanIDs) == 0 {
|
||||
return nil // 没有需要清理的计划
|
||||
}
|
||||
|
||||
invalidPlanIDSet := make(map[uint]struct{}, len(invalidPlanIDs))
|
||||
invalidPlanIDSet := make(map[uint32]struct{}, len(invalidPlanIDs))
|
||||
for _, id := range invalidPlanIDs {
|
||||
invalidPlanIDSet[id] = struct{}{}
|
||||
}
|
||||
|
||||
var tasksToDeleteIDs []uint
|
||||
var logsToCancelIDs []uint
|
||||
var tasksToDeleteIDs []uint32
|
||||
var logsToCancelIDs []uint32
|
||||
|
||||
for _, pt := range allPendingTasks {
|
||||
if pt.Task == nil { // 防御性编程,确保 Task 被预加载
|
||||
@@ -245,7 +245,7 @@ func (m *analysisPlanTaskManagerImpl) cleanupInvalidTasks(ctx context.Context, i
|
||||
func (m *analysisPlanTaskManagerImpl) addOrUpdateTriggers(ctx context.Context, runnablePlans []*models.Plan, allPendingTasks []models.PendingTask) error {
|
||||
managerCtx, logger := logs.Trace(ctx, m.ctx, "addOrUpdateTriggers")
|
||||
// 创建一个映射,存放所有已在队列中的计划触发器
|
||||
pendingTriggersMap := make(map[uint]models.PendingTask)
|
||||
pendingTriggersMap := make(map[uint32]models.PendingTask)
|
||||
for _, pt := range allPendingTasks {
|
||||
if pt.Task != nil && pt.Task.Type == models.TaskPlanAnalysis {
|
||||
pendingTriggersMap[pt.Task.PlanID] = pt
|
||||
|
||||
@@ -25,21 +25,21 @@ type ExecutionManager interface {
|
||||
// ProgressTracker 仅用于在内存中提供计划执行的并发锁
|
||||
type ProgressTracker struct {
|
||||
mu sync.Mutex
|
||||
cond *sync.Cond // 用于实现阻塞锁
|
||||
runningPlans map[uint]bool // key: planExecutionLogID, value: true (用作内存锁)
|
||||
cond *sync.Cond // 用于实现阻塞锁
|
||||
runningPlans map[uint32]bool // key: planExecutionLogID, value: true (用作内存锁)
|
||||
}
|
||||
|
||||
// NewProgressTracker 创建一个新的进度跟踪器
|
||||
func NewProgressTracker() *ProgressTracker {
|
||||
t := &ProgressTracker{
|
||||
runningPlans: make(map[uint]bool),
|
||||
runningPlans: make(map[uint32]bool),
|
||||
}
|
||||
t.cond = sync.NewCond(&t.mu)
|
||||
return t
|
||||
}
|
||||
|
||||
// TryLock (非阻塞) 尝试锁定一个计划。如果计划未被锁定,则锁定并返回 true。
|
||||
func (t *ProgressTracker) TryLock(planLogID uint) bool {
|
||||
func (t *ProgressTracker) TryLock(planLogID uint32) bool {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if t.runningPlans[planLogID] {
|
||||
@@ -50,7 +50,7 @@ func (t *ProgressTracker) TryLock(planLogID uint) bool {
|
||||
}
|
||||
|
||||
// Lock (阻塞) 获取一个计划的执行锁。如果锁已被占用,则会一直等待直到锁被释放。
|
||||
func (t *ProgressTracker) Lock(planLogID uint) {
|
||||
func (t *ProgressTracker) Lock(planLogID uint32) {
|
||||
t.mu.Lock()
|
||||
// 当计划正在运行时,调用 t.cond.Wait() 会原子地解锁 mu 并挂起当前协程。
|
||||
// 当被唤醒时,它会重新锁定 mu 并再次检查循环条件。
|
||||
@@ -63,7 +63,7 @@ func (t *ProgressTracker) Lock(planLogID uint) {
|
||||
}
|
||||
|
||||
// Unlock 解锁一个计划,并唤醒所有正在等待此锁的协程。
|
||||
func (t *ProgressTracker) Unlock(planLogID uint) {
|
||||
func (t *ProgressTracker) Unlock(planLogID uint32) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
delete(t.runningPlans, planLogID)
|
||||
@@ -72,10 +72,10 @@ func (t *ProgressTracker) Unlock(planLogID uint) {
|
||||
}
|
||||
|
||||
// GetRunningPlanIDs 获取当前所有正在执行的计划ID列表
|
||||
func (t *ProgressTracker) GetRunningPlanIDs() []uint {
|
||||
func (t *ProgressTracker) GetRunningPlanIDs() []uint32 {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
ids := make([]uint, 0, len(t.runningPlans))
|
||||
ids := make([]uint32, 0, len(t.runningPlans))
|
||||
for id := range t.runningPlans {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
@@ -214,7 +214,7 @@ func (s *planExecutionManagerImpl) claimAndSubmit(ctx context.Context) {
|
||||
}
|
||||
|
||||
// handleRequeue 同步地、安全地将一个无法立即执行的任务放回队列。
|
||||
func (s *planExecutionManagerImpl) handleRequeue(ctx context.Context, planExecutionLogID uint, taskToRequeue *models.PendingTask) {
|
||||
func (s *planExecutionManagerImpl) handleRequeue(ctx context.Context, planExecutionLogID uint32, taskToRequeue *models.PendingTask) {
|
||||
managerCtx, logger := logs.Trace(ctx, s.ctx, "handleRequeue")
|
||||
logger.Warnf("计划 %d 正在执行,任务 %d (TaskID: %d) 将等待并重新入队...", planExecutionLogID, taskToRequeue.ID, taskToRequeue.TaskID)
|
||||
|
||||
@@ -308,7 +308,7 @@ func (s *planExecutionManagerImpl) analysisPlan(ctx context.Context, claimedLog
|
||||
// 创建Plan执行记录
|
||||
// 从任务的 Parameters 中解析出真实的 PlanID
|
||||
var params struct {
|
||||
PlanID uint `json:"plan_id"`
|
||||
PlanID uint32 `json:"plan_id"`
|
||||
}
|
||||
if err := claimedLog.Task.ParseParameters(¶ms); err != nil {
|
||||
logger.Errorf("解析任务参数中的计划ID失败,日志ID: %d, 错误: %v", claimedLog.ID, err)
|
||||
@@ -390,7 +390,7 @@ func (s *planExecutionManagerImpl) updateTaskExecutionLogStatus(ctx context.Cont
|
||||
}
|
||||
|
||||
// handlePlanTermination 集中处理计划的终止逻辑(失败或取消)
|
||||
func (s *planExecutionManagerImpl) handlePlanTermination(ctx context.Context, planLogID uint, reason string) {
|
||||
func (s *planExecutionManagerImpl) handlePlanTermination(ctx context.Context, planLogID uint32, reason string) {
|
||||
managerCtx, logger := logs.Trace(ctx, s.ctx, "handlePlanTermination")
|
||||
// 1. 从待执行队列中删除所有相关的子任务
|
||||
if err := s.pendingTaskRepo.DeletePendingTasksByPlanLogID(managerCtx, planLogID); err != nil {
|
||||
@@ -434,7 +434,7 @@ func (s *planExecutionManagerImpl) handlePlanTermination(ctx context.Context, pl
|
||||
}
|
||||
|
||||
// handlePlanCompletion 集中处理计划成功完成后的所有逻辑
|
||||
func (s *planExecutionManagerImpl) handlePlanCompletion(ctx context.Context, planLogID uint) {
|
||||
func (s *planExecutionManagerImpl) handlePlanCompletion(ctx context.Context, planLogID uint32) {
|
||||
managerCtx, logger := logs.Trace(ctx, s.ctx, "handlePlanCompletion")
|
||||
logger.Infof("计划执行 %d 的所有任务已完成,开始处理计划完成逻辑...", planLogID)
|
||||
|
||||
|
||||
@@ -41,17 +41,17 @@ type Service interface {
|
||||
// CreatePlan 创建一个新的计划
|
||||
CreatePlan(ctx context.Context, plan *models.Plan) (*models.Plan, error)
|
||||
// GetPlanByID 根据ID获取计划详情
|
||||
GetPlanByID(ctx context.Context, id uint) (*models.Plan, error)
|
||||
GetPlanByID(ctx context.Context, id uint32) (*models.Plan, error)
|
||||
// ListPlans 获取计划列表,支持过滤和分页
|
||||
ListPlans(ctx context.Context, opts repository.ListPlansOptions, page, pageSize int) ([]models.Plan, int64, error)
|
||||
// UpdatePlan 更新计划, wantPlanType 表示期望被修改的计划是什么类型
|
||||
UpdatePlan(ctx context.Context, plan *models.Plan, wantPlanType models.PlanType) (*models.Plan, error)
|
||||
// DeletePlan 删除计划(软删除)
|
||||
DeletePlan(ctx context.Context, id uint) error
|
||||
DeletePlan(ctx context.Context, id uint32) error
|
||||
// StartPlan 启动计划
|
||||
StartPlan(ctx context.Context, id uint) error
|
||||
StartPlan(ctx context.Context, id uint32) error
|
||||
// StopPlan 停止计划
|
||||
StopPlan(ctx context.Context, id uint) error
|
||||
StopPlan(ctx context.Context, id uint32) error
|
||||
}
|
||||
|
||||
// planServiceImpl 是 Service 接口的具体实现。
|
||||
@@ -150,7 +150,7 @@ func (s *planServiceImpl) CreatePlan(ctx context.Context, planToCreate *models.P
|
||||
// 优化:无需查询完整的设备对象,只需构建包含ID的结构体即可建立关联
|
||||
devices := make([]models.Device, len(deviceIDs))
|
||||
for i, id := range deviceIDs {
|
||||
devices[i] = models.Device{Model: gorm.Model{ID: id}}
|
||||
devices[i] = models.Device{Model: models.Model{ID: id}}
|
||||
}
|
||||
taskModel.Devices = devices
|
||||
}
|
||||
@@ -174,7 +174,7 @@ func (s *planServiceImpl) CreatePlan(ctx context.Context, planToCreate *models.P
|
||||
}
|
||||
|
||||
// GetPlanByID 根据ID获取计划详情
|
||||
func (s *planServiceImpl) GetPlanByID(ctx context.Context, id uint) (*models.Plan, error) {
|
||||
func (s *planServiceImpl) GetPlanByID(ctx context.Context, id uint32) (*models.Plan, error) {
|
||||
planCtx, logger := logs.Trace(ctx, s.ctx, "GetPlanByID")
|
||||
const actionType = "领域层:获取计划详情"
|
||||
|
||||
@@ -262,7 +262,7 @@ func (s *planServiceImpl) UpdatePlan(ctx context.Context, planToUpdate *models.P
|
||||
// 优化:无需查询完整的设备对象,只需构建包含ID的结构体即可建立关联
|
||||
devices := make([]models.Device, len(deviceIDs))
|
||||
for i, id := range deviceIDs {
|
||||
devices[i] = models.Device{Model: gorm.Model{ID: id}}
|
||||
devices[i] = models.Device{Model: models.Model{ID: id}}
|
||||
}
|
||||
taskModel.Devices = devices
|
||||
}
|
||||
@@ -290,7 +290,7 @@ func (s *planServiceImpl) UpdatePlan(ctx context.Context, planToUpdate *models.P
|
||||
}
|
||||
|
||||
// DeletePlan 删除计划(软删除)
|
||||
func (s *planServiceImpl) DeletePlan(ctx context.Context, id uint) error {
|
||||
func (s *planServiceImpl) DeletePlan(ctx context.Context, id uint32) error {
|
||||
planCtx, logger := logs.Trace(ctx, s.ctx, "DeletePlan")
|
||||
const actionType = "领域层:删除计划"
|
||||
|
||||
@@ -328,7 +328,7 @@ func (s *planServiceImpl) DeletePlan(ctx context.Context, id uint) error {
|
||||
}
|
||||
|
||||
// StartPlan 启动计划
|
||||
func (s *planServiceImpl) StartPlan(ctx context.Context, id uint) error {
|
||||
func (s *planServiceImpl) StartPlan(ctx context.Context, id uint32) error {
|
||||
planCtx, logger := logs.Trace(ctx, s.ctx, "StartPlan")
|
||||
const actionType = "领域层:启动计划"
|
||||
|
||||
@@ -383,7 +383,7 @@ func (s *planServiceImpl) StartPlan(ctx context.Context, id uint) error {
|
||||
}
|
||||
|
||||
// StopPlan 停止计划
|
||||
func (s *planServiceImpl) StopPlan(ctx context.Context, id uint) error {
|
||||
func (s *planServiceImpl) StopPlan(ctx context.Context, id uint32) error {
|
||||
planCtx, logger := logs.Trace(ctx, s.ctx, "StopPlan")
|
||||
const actionType = "领域层:停止计划"
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ type Task interface {
|
||||
// TaskDeviceIDResolver 定义了从任务配置中解析设备ID的方法
|
||||
type TaskDeviceIDResolver interface {
|
||||
// ResolveDeviceIDs 从任务配置中解析并返回所有关联的设备ID列表
|
||||
// 返回值: uint数组,每个字符串代表一个设备ID
|
||||
ResolveDeviceIDs(ctx context.Context) ([]uint, error)
|
||||
// 返回值: uint32数组,每个字符串代表一个设备ID
|
||||
ResolveDeviceIDs(ctx context.Context) ([]uint32, error)
|
||||
}
|
||||
|
||||
// TaskFactory 是一个工厂接口,用于根据任务执行日志创建任务实例。
|
||||
|
||||
Reference in New Issue
Block a user