移动文件位置
This commit is contained in:
53
internal/app/service/task/delay_task.go
Normal file
53
internal/app/service/task/delay_task.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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
|
||||
}
|
||||
|
||||
// GetPriority 获取任务优先级
|
||||
func (d *DelayTask) GetPriority() int {
|
||||
return d.priority
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user