-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtask_manager.lua
More file actions
159 lines (139 loc) · 3.67 KB
/
task_manager.lua
File metadata and controls
159 lines (139 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--[[
任务管理器
用于统一管理LuatOS中的任务创建、删除和生命周期
]]
local TaskManager = {
tasks = {},
debug = false
}
--[[
创建命名任务
@param name string 任务名称
@param func function 任务函数
@param callback function 错误回调函数
@return 任务对象
]]
function TaskManager.create(name, func, callback)
if TaskManager.debug then
log.info("TaskManager", "creating task:", name)
end
-- 删除已存在的同名任务
TaskManager.delete(name)
local task = sysplus.taskInitEx(function()
local success, err = pcall(func)
if not success then
log.error("TaskManager", name, "task error:", err)
if callback then callback(false, err) end
else
if TaskManager.debug then
log.info("TaskManager", name, "task completed successfully")
end
if callback then callback(true, nil) end
end
TaskManager.tasks[name] = nil
end, name, function(err)
log.error("TaskManager", name, "init error:", err)
TaskManager.tasks[name] = nil
if callback then callback(false, err) end
end)
TaskManager.tasks[name] = task
return task
end
--[[
删除指定任务
@param name string 任务名称
@return boolean 是否删除成功
]]
function TaskManager.delete(name)
if TaskManager.tasks[name] then
if TaskManager.debug then
log.info("TaskManager", "deleting task:", name)
end
sysplus.taskDel(name)
TaskManager.tasks[name] = nil
return true
end
return false
end
--[[
检查任务是否存在
@param name string 任务名称
@return boolean 任务是否存在
]]
function TaskManager.exists(name)
return TaskManager.tasks[name] ~= nil
end
--[[
获取所有活跃任务列表
@return table 任务名称列表
]]
function TaskManager.list()
local list = {}
for name, _ in pairs(TaskManager.tasks) do
table.insert(list, name)
end
return list
end
--[[
清理所有任务
]]
function TaskManager.cleanup()
if TaskManager.debug then
log.info("TaskManager", "cleaning up all tasks")
end
for name, _ in pairs(TaskManager.tasks) do
TaskManager.delete(name)
end
end
--[[
设置调试模式
@param enable boolean 是否开启调试
]]
function TaskManager.setDebug(enable)
TaskManager.debug = enable
end
--[[
创建循环任务(带退出机制)
@param name string 任务名称
@param func function 循环执行的函数
@param interval number 循环间隔(毫秒)
@param callback function 错误回调函数
@return 任务对象
]]
function TaskManager.createLoop(name, func, interval, callback)
if not interval or interval <= 0 then
interval = 1000 -- 默认1秒
end
local should_stop = false
local task_func = function()
while not should_stop do
local success, err = pcall(func)
if not success then
log.error("TaskManager", name, "loop error:", err)
if callback then callback(false, err) end
end
-- 检查是否应该停止
if should_stop then
break
end
sys.wait(interval)
end
if TaskManager.debug then
log.info("TaskManager", name, "loop task stopped")
end
end
local task = TaskManager.create(name, task_func, callback)
-- 返回停止函数
return task, function()
should_stop = true
end
end
--[[
停止循环任务
@param name string 任务名称
]]
function TaskManager.stopLoop(name)
-- 通过删除任务来停止循环
TaskManager.delete(name)
end
return TaskManager