mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
refactor(server/cron): types and formatting, simplify getMaxDaysInMonth
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
lib.cron = {}
|
lib.cron = {}
|
||||||
local currentDate = os.date('*t') --[[@as osdate]]
|
|
||||||
|
local currentDate = os.date('*t') --[[@as { year: number, month: number, day: number, hour: number, min: number, sec: number, wday: number, yday: number, isdst: boolean }]]
|
||||||
currentDate.sec = 0
|
currentDate.sec = 0
|
||||||
|
|
||||||
---@class OxTaskProperties
|
---@class OxTaskProperties
|
||||||
@@ -7,6 +8,7 @@ currentDate.sec = 0
|
|||||||
---@field hour? number | string
|
---@field hour? number | string
|
||||||
---@field day? number | string
|
---@field day? number | string
|
||||||
---@field month? number | string
|
---@field month? number | string
|
||||||
|
---@field year? number | string
|
||||||
---@field weekday? number | string
|
---@field weekday? number | string
|
||||||
---@field job fun(task: OxTask, date: osdate)
|
---@field job fun(task: OxTask, date: osdate)
|
||||||
---@field isActive boolean
|
---@field isActive boolean
|
||||||
@@ -31,11 +33,7 @@ local maxUnits = {
|
|||||||
---@param year? number
|
---@param year? number
|
||||||
---@return number
|
---@return number
|
||||||
local function getMaxDaysInMonth(month, year)
|
local function getMaxDaysInMonth(month, year)
|
||||||
year = year or currentDate.year
|
return os.date('*t', os.time({ year = year or currentDate.year, month = month + 1, day = -1 })).day --[[@as number]]
|
||||||
local nextMonth = month + 1
|
|
||||||
local nextMonthFirstDay = os.time{year=year, month=nextMonth, day=1}
|
|
||||||
local lastDayOfCurrentMonth = os.date("%d", nextMonthFirstDay - 86400)
|
|
||||||
return tonumber(lastDayOfCurrentMonth)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param value string | number | nil
|
---@param value string | number | nil
|
||||||
@@ -43,9 +41,9 @@ end
|
|||||||
---@return string | number | false | nil
|
---@return string | number | false | nil
|
||||||
local function getTimeUnit(value, unit)
|
local function getTimeUnit(value, unit)
|
||||||
local currentTime = currentDate[unit]
|
local currentTime = currentDate[unit]
|
||||||
|
local unitMax = maxUnits[unit]
|
||||||
|
|
||||||
if type(value) == 'string' then
|
if type(value) == 'string' then
|
||||||
local unitMax = maxUnits[unit]
|
|
||||||
local stepValue = string.match(value, '*/(%d+)')
|
local stepValue = string.match(value, '*/(%d+)')
|
||||||
|
|
||||||
if stepValue then
|
if stepValue then
|
||||||
@@ -138,13 +136,9 @@ end
|
|||||||
---@return number
|
---@return number
|
||||||
function OxTask:getAbsoluteNextTime()
|
function OxTask:getAbsoluteNextTime()
|
||||||
local minute = getTimeUnit(self.minute, 'min')
|
local minute = getTimeUnit(self.minute, 'min')
|
||||||
|
|
||||||
local hour = getTimeUnit(self.hour, 'hour')
|
local hour = getTimeUnit(self.hour, 'hour')
|
||||||
|
|
||||||
local day = getTimeUnit(self.day, 'day')
|
local day = getTimeUnit(self.day, 'day')
|
||||||
|
|
||||||
local month = getTimeUnit(self.month, 'month')
|
local month = getTimeUnit(self.month, 'month')
|
||||||
|
|
||||||
local year = getTimeUnit(self.year, 'year')
|
local year = getTimeUnit(self.year, 'year')
|
||||||
|
|
||||||
-- To avoid modifying getTimeUnit function, the day is adjusted here if needed.
|
-- To avoid modifying getTimeUnit function, the day is adjusted here if needed.
|
||||||
@@ -155,6 +149,7 @@ function OxTask:getAbsoluteNextTime()
|
|||||||
day = getMaxDaysInMonth(currentDate.month)
|
day = getMaxDaysInMonth(currentDate.month)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if currentDate.hour > hour or (currentDate.hour == hour and currentDate.min >= minute) then
|
if currentDate.hour > hour or (currentDate.hour == hour and currentDate.min >= minute) then
|
||||||
day = day + 1
|
day = day + 1
|
||||||
if day > getMaxDaysInMonth(currentDate.month) or day == 1 then
|
if day > getMaxDaysInMonth(currentDate.month) or day == 1 then
|
||||||
@@ -165,7 +160,8 @@ function OxTask:getAbsoluteNextTime()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Check if time will be in next year.
|
-- Check if time will be in next year.
|
||||||
if os.time({year=year, month=month, day=day, hour=hour, min=minute}) < os.time() then
|
---@diagnostic disable-next-line: assign-type-mismatch
|
||||||
|
if os.time({ year = year, month = month, day = day, hour = hour, min = minute }) < os.time() then
|
||||||
year = year and year + 1 or currentDate.year + 1
|
year = year and year + 1 or currentDate.year + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -217,6 +213,10 @@ function OxTask:scheduleTask()
|
|||||||
if self.isActive then
|
if self.isActive then
|
||||||
self:job(currentDate)
|
self:job(currentDate)
|
||||||
|
|
||||||
|
if self.debug then
|
||||||
|
print(('(%s/%s/%s %s:%s) ran task %s'):format(currentDate.year, currentDate.month, currentDate.day, currentDate.hour, currentDate.min, self.id))
|
||||||
|
end
|
||||||
|
|
||||||
Wait(30000)
|
Wait(30000)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|||||||
Reference in New Issue
Block a user