From 1aae1970a382efae429bc3744b310343bf0f9891 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:21:24 +0100 Subject: [PATCH 1/5] refactor/cron/server/main): Validate event params --- [core]/cron/server/main.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/[core]/cron/server/main.lua b/[core]/cron/server/main.lua index bc8f81d0..4d2b650f 100644 --- a/[core]/cron/server/main.lua +++ b/[core]/cron/server/main.lua @@ -47,5 +47,9 @@ LastTime = GetUnixTimestamp() Tick() AddEventHandler("cron:runAt", function(h, m, cb) + assert(type(h) == "number", ("Expected number for h, got %s"):format(type(h))) + assert(type(m) == "number", ("Expected number for m, got %s"):format(type(m))) + assert(type(cb) == "function", ("Expected function for cb, got %s"):format(type(cb))) + RunAt(h, m, cb) end) From 4bb69c15e8ef3e4df75a488d88dd0bf699180eb2 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:26:39 +0100 Subject: [PATCH 2/5] refactor(cron/server/main): Add type annotations --- [core]/cron/server/main.lua | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/[core]/cron/server/main.lua b/[core]/cron/server/main.lua index 4d2b650f..76b8915b 100644 --- a/[core]/cron/server/main.lua +++ b/[core]/cron/server/main.lua @@ -1,6 +1,16 @@ -local Jobs = {} -local LastTime = nil +---@class CronJob +---@field h number +---@field m number +---@field cb function +---@type CronJob[] +local Jobs = {} +---@type number|false +local LastTime = false + +---@param h number +---@param m number +---@param cb function function RunAt(h, m, cb) Jobs[#Jobs + 1] = { h = h, @@ -9,10 +19,12 @@ function RunAt(h, m, cb) } end +---@return number function GetUnixTimestamp() return os.time() end +---@param time number function OnTime(time) for i = 1, #Jobs, 1 do local scheduledTimestamp = os.time({ @@ -31,6 +43,7 @@ function OnTime(time) end end +---@return nil function Tick() local time = GetUnixTimestamp() @@ -43,7 +56,6 @@ function Tick() end LastTime = GetUnixTimestamp() - Tick() AddEventHandler("cron:runAt", function(h, m, cb) From b69385fff694c24c696df63a9180d03016891155 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:29:48 +0100 Subject: [PATCH 3/5] refactor(cron/server/main): More appropriate variable names --- [core]/cron/server/main.lua | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/[core]/cron/server/main.lua b/[core]/cron/server/main.lua index 76b8915b..2ad258b6 100644 --- a/[core]/cron/server/main.lua +++ b/[core]/cron/server/main.lua @@ -4,15 +4,15 @@ ---@field cb function ---@type CronJob[] -local Jobs = {} +local cronJobs = {} ---@type number|false -local LastTime = false +local lastTimestamp = false ---@param h number ---@param m number ---@param cb function function RunAt(h, m, cb) - Jobs[#Jobs + 1] = { + cronJobs[#cronJobs + 1] = { h = h, m = m, cb = cb, @@ -24,38 +24,38 @@ function GetUnixTimestamp() return os.time() end ----@param time number -function OnTime(time) - for i = 1, #Jobs, 1 do +---@param timestamp number +function OnTime(timestamp) + for i = 1, #cronJobs, 1 do local scheduledTimestamp = os.time({ - hour = Jobs[i].h, - min = Jobs[i].m, + hour = cronJobs[i].h, + min = cronJobs[i].m, sec = 0, -- Assuming tasks run at the start of the minute - day = os.date("%d", time), - month = os.date("%m", time), - year = os.date("%Y", time), + day = os.date("%d", timestamp), + month = os.date("%m", timestamp), + year = os.date("%Y", timestamp), }) - if time >= scheduledTimestamp and (not LastTime or LastTime < scheduledTimestamp) then + if timestamp >= scheduledTimestamp and (not lastTimestamp or lastTimestamp < scheduledTimestamp) then local d = os.date('*t', scheduledTimestamp).wday - Jobs[i].cb(d, Jobs[i].h, Jobs[i].m) + cronJobs[i].cb(d, cronJobs[i].h, cronJobs[i].m) end end end ---@return nil function Tick() - local time = GetUnixTimestamp() + local timestamp = GetUnixTimestamp() - if not LastTime or os.date("%M", time) ~= os.date("%M", LastTime) then - OnTime(time) - LastTime = time + if not lastTimestamp or os.date("%M", timestamp) ~= os.date("%M", lastTimestamp) then + OnTime(timestamp) + lastTimestamp = timestamp end SetTimeout(60000, Tick) end -LastTime = GetUnixTimestamp() +lastTimestamp = GetUnixTimestamp() Tick() AddEventHandler("cron:runAt", function(h, m, cb) From 6b91207cc94a3cc76087f0ac5bc1fed50d66b3f0 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:33:35 +0100 Subject: [PATCH 4/5] refactor(cron/server/main): Add more type annotations --- [core]/cron/server/main.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/[core]/cron/server/main.lua b/[core]/cron/server/main.lua index 2ad258b6..fb6cec25 100644 --- a/[core]/cron/server/main.lua +++ b/[core]/cron/server/main.lua @@ -58,6 +58,9 @@ end lastTimestamp = GetUnixTimestamp() Tick() +---@param h number +---@param m number +---@param cb function AddEventHandler("cron:runAt", function(h, m, cb) assert(type(h) == "number", ("Expected number for h, got %s"):format(type(h))) assert(type(m) == "number", ("Expected number for m, got %s"):format(type(m))) From 0576b209670780b34d5b0483bedda630b69515ea Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:28:45 +0100 Subject: [PATCH 5/5] validate serialized function references --- [core]/cron/server/main.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/[core]/cron/server/main.lua b/[core]/cron/server/main.lua index fb6cec25..51da085d 100644 --- a/[core]/cron/server/main.lua +++ b/[core]/cron/server/main.lua @@ -1,7 +1,7 @@ ---@class CronJob ---@field h number ---@field m number ----@field cb function +---@field cb function|table ---@type CronJob[] local cronJobs = {} @@ -10,7 +10,7 @@ local lastTimestamp = false ---@param h number ---@param m number ----@param cb function +---@param cb function|table function RunAt(h, m, cb) cronJobs[#cronJobs + 1] = { h = h, @@ -60,11 +60,16 @@ Tick() ---@param h number ---@param m number ----@param cb function +---@param cb function|table AddEventHandler("cron:runAt", function(h, m, cb) - assert(type(h) == "number", ("Expected number for h, got %s"):format(type(h))) - assert(type(m) == "number", ("Expected number for m, got %s"):format(type(m))) - assert(type(cb) == "function", ("Expected function for cb, got %s"):format(type(cb))) + local invokingResource = GetInvokingResource() or "Unknown" + local typeH = type(h) + local typeM = type(m) + local typeCb = type(cb) + + assert(typeH == "number", ("Expected number for h, got %s. Invoking Resource: '%s'"):format(typeH, invokingResource)) + assert(typeM == "number", ("Expected number for m, got %s. Invoking Resource: '%s'"):format(typeM, invokingResource)) + assert(typeCb == "function" or (typeCb == "table" and type(getmetatable(cb)?.__call) == "function"), ("Expected function for cb, got %s. Invoking Resource: '%s'"):format(typeCb, invokingResource)) RunAt(h, m, cb) end)