Skip to content

Commit

Permalink
Merge pull request #1130 from ItsKuf/main
Browse files Browse the repository at this point in the history
🚀 Enhanced ESX Core Cron Task Handling
  • Loading branch information
Gellipapa authored Jul 26, 2023
2 parents 804b8ac + ea14a80 commit da530d6
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions [core]/cron/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,41 @@ function RunAt(h, m, cb)
}
end

function GetTime()
local timestamp = os.time()
local d = os.date('*t', timestamp).wday
local h = tonumber(os.date('%H', timestamp))
local m = tonumber(os.date('%M', timestamp))

return {
d = d,
h = h,
m = m
}
function GetUnixTimestamp()
return os.time()
end

function OnTime(d, h, m)
function OnTime(h, m)
local currentTimestamp = GetUnixTimestamp()

for i = 1, #Jobs, 1 do
if Jobs[i].h == h and Jobs[i].m == m then
Jobs[i].cb(d, h, m)
local scheduledTimestamp = os.time({
hour = Jobs[i].h,
minute = Jobs[i].m,
second = 0, -- Assuming tasks run at the start of the minute
day = os.date('%d', currentTimestamp),
month = os.date('%m', currentTimestamp),
year = os.date('%Y', currentTimestamp)
})

if currentTimestamp >= scheduledTimestamp and (not LastTime or LastTime < scheduledTimestamp) then
Jobs[i].cb(Jobs[i].h, Jobs[i].m)
end
end
end

function Tick()
local time = GetTime()
local time = GetUnixTimestamp()

if time.h ~= LastTime.h or time.m ~= LastTime.m then
OnTime(time.d, time.h, time.m)
if not LastTime or os.date('%M', time) ~= os.date('%M', LastTime) then
OnTime(os.date('%H', time), os.date('%M', time))
LastTime = time
end

SetTimeout(60000, Tick)
end

LastTime = GetTime()
LastTime = GetUnixTimestamp()

Tick()

Expand Down

0 comments on commit da530d6

Please sign in to comment.