Comparison

core/moduleapi.lua @ 11987:4b519c575ad0

core.moduleapi: Add API for adding daily or hourly tasks via mod_cron
author Kim Alvefur <zash@zash.se>
date Sun, 21 Nov 2021 18:13:21 +0100
parent 11931:c65d5da8e99a
child 12002:cbed7d8d8f35
comparison
equal deleted inserted replaced
11986:3d5135e8a2a7 11987:4b519c575ad0
504 t.callback = callback; 504 t.callback = callback;
505 t.id = timer.add_task(delay, timer_callback, t); 505 t.id = timer.add_task(delay, timer_callback, t);
506 return setmetatable(t, timer_mt); 506 return setmetatable(t, timer_mt);
507 end 507 end
508 508
509 function api:cron(task_spec)
510 self:depends("cron");
511 self:add_item("task", task_spec);
512 end
513
514 function api:hourly(name, fun)
515 if type(name) == "function" then fun, name = name, nil; end
516 self:cron({ name = name; when = "hourly"; run = fun });
517 end
518
519 function api:daily(name, fun)
520 if type(name) == "function" then fun, name = name, nil; end
521 self:cron({ name = name; when = "daily"; run = fun });
522 end
523
509 local path_sep = package.config:sub(1,1); 524 local path_sep = package.config:sub(1,1);
510 function api:get_directory() 525 function api:get_directory()
511 return self.resource_path or self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; 526 return self.resource_path or self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil;
512 end 527 end
513 528