跳到主要内容

schedule

简介

计划任务,定时/循环触发某个任务

提示
  1. 热更新期间,任务列表保留,协程启动后遍历检查,从而天然支持热更新
  2. 算法“exec_time = exec_time + interval”保证了time.shift的支持

组件接口

schedule.gs

函数原型函数作用
void dump()调试接口 - 打印计划任务表
void debug_set_time(mixed t, int off = 0)设置时间(仅用于调试)
void list()调试接口 - 打印计划任务表
bool create_task(string task_name, function f, int first_exec_time, mixed periode_type = nil, bool overwrite = false)创建计划任务
SchedulePeriodTime new_period_time(int t, mixed periode_type)创建周期性时间
bool create_task_with_period_time(string task_name, function func, SchedulePeriodTime period_time, bool overwrite = false)创建周期性计划任务
array create_scheme_format(string scheme_name, function func, string scheme_type, map arg, bool overwrite = true)创建任务计划,支持多种格式的计划任务
void delete_task(string task_name)删除计划
int get_count()获取任务计划数量
ScheduleTask get_task(string task_name)查询任务计划

ScheduleTask

计划任务基础类

成员变量

变量名类型初始值须初始化描述
namestringnil可选任务名称
funcfunctionnil可选任务执行函数
period_timeSchedulePeriodTimenil可选任务周期

成员方法

函数原型函数作用
void exec()执行任务

ScheduleTaskList

计划任务列表

成员变量

变量名类型初始值须初始化描述
listarraynil可选
semarphoresync_objectnil可选

成员方法

函数原型函数作用
void notify()通知变更
void wait(int wait_time)等待
array try_dequeue(int now_time)尝试获取一个有效的任务
ScheduleTask dequeue()出队列
int enqueue(ScheduleTask task)进队列
ScheduleTask get_by_name(string name)根据任务名称查找任务
bool delete_by_name(string name)删除任务

SchedulePeriodTime

周期时间

成员变量

变量名类型初始值须初始化描述
nowint-1可选本周期时间

成员方法

函数原型函数作用
string desc()获取描述
bool update()更新周期时间
int time()获取周期时间
int diff(int t)计算时间差
bool is_early_than(int t)判断是否早于指定时间
bool is_late_than(int t)判断是否晚于指定时间
int calc_next_period_time()计算下一周期时间

YearSchedulePeriodTime

继承自 SchedulePeriodTime

间隔时长为1年的计划周期

成员变量

变量名类型初始值须初始化描述

成员方法

函数原型函数作用
int calc_next_period_time()计算周期的下一个时间

MonthSchedulePeriodTime

继承自 SchedulePeriodTime

间隔时长为1月的计划周期

成员变量

变量名类型初始值须初始化描述

成员方法

函数原型函数作用
int calc_next_period_time()计算周期的下一个时间

WeekSchedulePeriodTime

继承自 SchedulePeriodTime

间隔时长为1周的计划周期

成员变量

变量名类型初始值须初始化描述

成员方法

函数原型函数作用
int calc_time(int base_time, int ch_wday, int hour, int minute, int second)
int calc_next_period_time()计算周期的下一个时间

DaySchedulePeriodTime

继承自 SchedulePeriodTime

间隔时长为1天的计划周期

成员变量

变量名类型初始值须初始化描述

成员方法

函数原型函数作用
int calc_time(int base_time, int hour, int minute, int second)
int calc_next_period_time()计算周期的下一个时间

HourSchedulePeriodTime

继承自 SchedulePeriodTime

间隔时长为1小时的计划周期

成员变量

变量名类型初始值须初始化描述

成员方法

函数原型函数作用
int calc_next_period_time()计算周期的下一个时间

MinuteSchedulePeriodTime

继承自 SchedulePeriodTime

间隔时长为1分钟的计划周期

成员变量

变量名类型初始值须初始化描述

成员方法

函数原型函数作用
int calc_next_period_time()计算周期的下一个时间

SecondSchedulePeriodTime

继承自 SchedulePeriodTime

周期间隔时长为1秒的计划周期

成员变量

变量名类型初始值须初始化描述

成员方法

函数原型函数作用
int calc_next_period_time()计算周期的下一个时间

FixedSchedulePeriodTime

继承自 SchedulePeriodTime

固定时长的计划周期

成员变量

变量名类型初始值须初始化描述
period_secondsint-1可选

成员方法

函数原型函数作用
int calc_next_period_time()计算周期的下一个时间

样例

void test()
{
queue q = queue.create("test_schedule");
defer q.close();

// 定时触发
schedule.create_task("test01", (: q.send_raw, "test01" :), time.time() + 1);
schedule.create_task("test02", (: q.send_raw, "test02" :), time.time() + 2, nil, true);
schedule.create_task("test03", (: q.send_raw, "test03" :), time.time() + 3, nil, true);

gtest.test_equal(q.receive(), "test01");
gtest.test_equal(q.receive(), "test02");
gtest.test_equal(q.receive(), "test03");

// 每天5点定时触发
map daily_times = {
"times" : [
{ "hour" : 5 }
]
};
function daily_func = () {
printf(HIR "daily repeat\n" NOR);
};
array arr1 = schedule.create_scheme_format("test11", daily_func, "day", daily_times);
for (string task_name : arr1)
gtest.test_equal(schedule.get_task(task_name) != nil, true);

// 每周一8:00定时触发
map weekly_times = {
"date" : [ 1 ],
"times" : [
{ "hour" : 8 }
]
};
function weekly_func = () {
printf(HIR "weekly repeat\n" NOR);
};
array arr2 = schedule.create_scheme_format("test12", weekly_func, "week", weekly_times);
for (string task_name : arr2)
gtest.test_equal(schedule.get_task(task_name) != nil, true);

// 删除计划任务
for (string task_name : arr1 + arr2)
schedule.delete_task(task_name);

// 查看是否删除成功
gtest.test_equal(schedule.get_count(), 0);
}