Files
pig-farm-controller/internal/app/service/task/delay_task.go
2025-09-17 15:56:08 +08:00

49 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package task
import (
"fmt"
"time"
)
// DelayTask 是一个用于模拟延迟的 Task 实现
type DelayTask struct {
id string
duration time.Duration
priority int
done bool
}
// NewDelayTask 创建一个新的 DelayTask 实例
func NewDelayTask(id string, duration time.Duration, priority int) *DelayTask {
return &DelayTask{
id: id,
duration: duration,
priority: priority,
done: false,
}
}
// Execute 执行延迟任务,等待指定的时间
func (d *DelayTask) Execute() error {
fmt.Printf("任务 %s (%s): 开始延迟 %s...\n", d.id, d.GetDescription(), d.duration)
time.Sleep(d.duration)
fmt.Printf("任务 %s (%s): 延迟结束。\n", d.id, d.GetDescription())
d.done = true
return nil
}
// GetID 获取任务ID
func (d *DelayTask) GetID() string {
return d.id
}
// IsDone 检查任务是否已完成
func (d *DelayTask) IsDone() bool {
return d.done
}
// GetDescription 获取任务说明根据任务ID和延迟时间生成
func (d *DelayTask) GetDescription() string {
return fmt.Sprintf("延迟任务ID: %s延迟时间: %s", d.id, d.duration)
}