# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1638486691 -3600
# Node ID bbd3ac65640d2c973f7f6b8493f629092bbf3933
# Parent  f9b2325f6b50bafb1c5d8cd5057ada6a953541df
mod_cron: Initialize daily tasks so they run around midnight UTC

Eventually the goal is to have daily tasks run while there is little
activity, but that will vary with the server and the usage patterns of
its users. This is a start anyway.

diff -r f9b2325f6b50 -r bbd3ac65640d plugins/mod_cron.lua
--- a/plugins/mod_cron.lua	Tue Nov 30 00:55:54 2021 +0100
+++ b/plugins/mod_cron.lua	Fri Dec 03 00:11:31 2021 +0100
@@ -21,6 +21,10 @@
 		task.save = save_task;
 		module:log("debug", "%s task %s added, last run %s", task.when, task.id,
 			task.last and require("util.datetime").datetime(task.last) or "never");
+		if task.last == nil and task.when == "daily" then
+			local now = os.time();
+			task.last = now - now % 86400;
+		end
 		return true
 	end
 
diff -r f9b2325f6b50 -r bbd3ac65640d teal-src/plugins/mod_cron.tl
--- a/teal-src/plugins/mod_cron.tl	Tue Nov 30 00:55:54 2021 +0100
+++ b/teal-src/plugins/mod_cron.tl	Fri Dec 03 00:11:31 2021 +0100
@@ -54,6 +54,11 @@
 		task.save = save_task;
 		module:log("debug", "%s task %s added, last run %s", task.when, task.id,
 			task.last and require"util.datetime".datetime(task.last) or "never");
+		if task.last == nil and task.when == "daily" then
+			-- initialize daily tasks to run at ~midnight UTC for now
+			local now = os.time();
+			task.last = now - now % 86400;
+		end
 		return true;
 	end