Changeset

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
parents 11986:3d5135e8a2a7
children 11988:18c0ca5fcbb8
files .luacheckrc core/moduleapi.lua
diffstat 2 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.luacheckrc	Sun Nov 21 15:50:36 2021 +0100
+++ b/.luacheckrc	Sun Nov 21 18:13:21 2021 +0100
@@ -52,6 +52,8 @@
 		"module.add_identity",
 		"module.add_item",
 		"module.add_timer",
+		"module.daily",
+		"module.hourly",
 		"module.broadcast",
 		"module.context",
 		"module.depends",
--- a/core/moduleapi.lua	Sun Nov 21 15:50:36 2021 +0100
+++ b/core/moduleapi.lua	Sun Nov 21 18:13:21 2021 +0100
@@ -506,6 +506,21 @@
 	return setmetatable(t, timer_mt);
 end
 
+function api:cron(task_spec)
+	self:depends("cron");
+	self:add_item("task", task_spec);
+end
+
+function api:hourly(name, fun)
+	if type(name) == "function" then fun, name = name, nil; end
+	self:cron({ name = name; when = "hourly"; run = fun });
+end
+
+function api:daily(name, fun)
+	if type(name) == "function" then fun, name = name, nil; end
+	self:cron({ name = name; when = "daily"; run = fun });
+end
+
 local path_sep = package.config:sub(1,1);
 function api:get_directory()
 	return self.resource_path or self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil;