This commit is contained in:
lenheart
2024-10-02 21:00:21 +08:00
parent 7f547f5fd4
commit 2d6e633410
16 changed files with 736 additions and 36 deletions

View File

@@ -7,14 +7,18 @@
class Timer {
//执行任务队列树
Wait_Exec_Tree = null;
//定时执行任务队列树
Date_Exec_Tree = null;
constructor() {
Wait_Exec_Tree = RedBlackTree();
Date_Exec_Tree = RedBlackTree();
Cb_timer_dispatch_Func.rawset("__System__Timer__Event", Update.bindenv(this));
}
function Update() {
//检测延时任务
function CheckTimeOut() {
local Node = Wait_Exec_Tree.pop();
if (!Node) {
return;
@@ -24,7 +28,7 @@ class Timer {
//执行时间
local exec_time = Node.time;
//如果没到执行时间,放回去,等待下次扫描
if (time() <= exec_time) {
if (clock() <= exec_time) {
Wait_Exec_Tree.insert(exec_time, Node.Info);
return;
}
@@ -42,10 +46,10 @@ class Timer {
for (local i = 0; i< vargv.len(); i++) {
target_arg_list.push(vargv[i]);
}
//当前时间戳,单位:s
local time_sec = time();
//当前时间戳,单位:
local time_sec = clock();
//计算下一次执行的时间
local exec_time_sec = time_sec + (delay_time - 1);
local exec_time_sec = time_sec + (delay_time / 1000.0).tofloat();
//设置下一次执行
local func_info = [];
@@ -55,4 +59,74 @@ class Timer {
_Timer_Object.Wait_Exec_Tree.insert(exec_time_sec, func_info);
}
//检测定时任务
function CheckCronTask() {
local Node = Date_Exec_Tree.pop();
if (!Node) {
return;
}
//取得函数体
local Info = Node.Info;
//执行时间
local exec_time = Node.time;
//如果没到执行时间,放回去,等待下次扫描
if (time() <= exec_time) {
Date_Exec_Tree.insert(exec_time, Node.Info);
return;
}
//函数
local func = Info[0];
//参数
local func_args = Info[1];
//下次任务叠加时间
local NextTimestep = Info[2];
//执行函数
func.acall(func_args);
//继续构建下一次任务
Date_Exec_Tree.insert(time() + NextTimestep, Info);
}
function SetCronTask(target_func, CronString, ...) {
local parts = split(CronString, "/");
local minute = parts[0].tointeger();
local hour = parts[1].tointeger();
local day = parts[2].tointeger();
local weekday = parts[3].tointeger();
local S_minute = minute * 60;
local S_hour = hour * 60 * 60;
local S_day = day * 24 * 60 * 60;
local S_weekday = weekday * 7 * 24 * 60 * 60;
local AddTimestep = S_minute + S_hour + S_day + S_weekday;
local NowTimestep = time();
//下一次执行的时间
local NextTimestep = AddTimestep + NowTimestep;
local target_arg_list = [];
target_arg_list.push(getroottable());
for (local i = 0; i< vargv.len(); i++) {
target_arg_list.push(vargv[i]);
}
//设置下一次执行
local func_info = [];
//函数体
func_info.push(target_func);
//参数列表
func_info.push(target_arg_list);
//间隔时间戳时间
func_info.push(AddTimestep);
_Timer_Object.Date_Exec_Tree.insert(NextTimestep, func_info);
}
function Update() {
CheckTimeOut();
CheckCronTask();
}
}