Added multi:scheduleJob(time,func) Removed multi:newTimeStamper

This commit is contained in:
Ryan Ward 2020-01-28 14:48:23 -05:00
parent 504186a852
commit 8484065479
2 changed files with 30 additions and 154 deletions

View File

@ -1305,144 +1305,34 @@ function multi:newTStep(start,reset,count,set)
self:create(c) self:create(c)
return c return c
end end
function multi:newTimeStamper() local scheduledjobs = {}
local c=self:newUpdater(self.Priority_Idle) local sthread
c:OnUpdate(function() function multi:scheduleJob(time,func)
c:Run() if not sthread then
end) sthread = multi:newThread("JobScheduler",function()
local feb = 28 local time = os.date("*t", os.time())
local leap = tonumber(os.date("%Y"))%4==0 and (tonumber(os.date("%Y"))%100~=0 or tonumber(os.date("%Y"))%400==0) local ready = false
if leap then while true do
feb = 29 thread.sleep(1) -- Every second we do some tests
end time = os.date("*t", os.time())
local dInM = { for j,job in pairs(scheduledjobs) do
["01"] = 31, ready = true
["02"] = feb, for k,v in pairs(job[1]) do
["03"] = 31, if not (v == time[k]) then
["04"] = 30, ready = false
["05"] = 31, end
["06"] = 30, end
["07"] = 31, -- This is dumb, why do we still follow this double 31 days!? if ready and not job[3] then
["08"] = 31, job[2]()
["09"] = 30, job[3] = true
["10"] = 31, elseif not ready and job[3] then
["11"] = 30, job[3] = false
["12"] = 31, end
}
c.Type='timestamper'
c:setPriority("Idle")
c.hour = {}
c.minute = {}
c.second = {}
c.time = {}
c.day = {}
c.month = {}
c.year = {}
function c:Run()
for i=1,#self.hour do
if self.hour[i][1]==os.date("%H") and self.hour[i][3] then
self.hour[i][2](self)
self.hour[i][3]=false
elseif self.hour[i][1]~=os.date("%H") and not self.hour[i][3] then
self.hour[i][3]=true
end
end
for i=1,#self.minute do
if self.minute[i][1]==os.date("%M") and self.minute[i][3] then
self.minute[i][2](self)
self.minute[i][3]=false
elseif self.minute[i][1]~=os.date("%M") and not self.minute[i][3] then
self.minute[i][3]=true
end
end
for i=1,#self.second do
if self.second[i][1]==os.date("%S") and self.second[i][3] then
self.second[i][2](self)
self.second[i][3]=false
elseif self.second[i][1]~=os.date("%S") and not self.second[i][3] then
self.second[i][3]=true
end
end
for i=1,#self.day do
if type(self.day[i][1])=="string" then
if self.day[i][1]==os.date("%a") and self.day[i][3] then
self.day[i][2](self)
self.day[i][3]=false
elseif self.day[i][1]~=os.date("%a") and not self.day[i][3] then
self.day[i][3]=true
end
else
local dday = self.day[i][1]
if dday < 0 then
dday = dInM[os.date("%m")]+(dday+1)
end
if string.format("%02d",dday)==os.date("%d") and self.day[i][3] then
self.day[i][2](self)
self.day[i][3]=false
elseif string.format("%02d",dday)~=os.date("%d") and not self.day[i][3] then
self.day[i][3]=true
end end
end end
end end)
for i=1,#self.month do
if self.month[i][1]==os.date("%m") and self.month[i][3] then
self.month[i][2](self)
self.month[i][3]=false
elseif self.month[i][1]~=os.date("%m") and not self.month[i][3] then
self.month[i][3]=true
end
end
for i=1,#self.time do
if self.time[i][1]==os.date("%X") and self.time[i][3] then
self.time[i][2](self)
self.time[i][3]=false
elseif self.time[i][1]~=os.date("%X") and not self.time[i][3] then
self.time[i][3]=true
end
end
for i=1,#self.year do
if self.year[i][1]==os.date("%y") and self.year[i][3] then
self.year[i][2](self)
self.year[i][3]=false
elseif self.year[i][1]~=os.date("%y") and not self.year[i][3] then
self.year[i][3]=true
end
end
end end
function c:OnTime(hour,minute,second,func) table.insert(scheduledjobs,{time, func,false})
if type(hour)=="number" then
self.time[#self.time+1]={string.format("%02d:%02d:%02d",hour,minute,second),func,true}
else
self.time[#self.time+1]={hour,minute,true}
end
return self
end
function c:OnHour(hour,func)
self.hour[#self.hour+1]={string.format("%02d",hour),func,true}
return self
end
function c:OnMinute(minute,func)
self.minute[#self.minute+1]={string.format("%02d",minute),func,true}
return self
end
function c:OnSecond(second,func)
self.second[#self.second+1]={string.format("%02d",second),func,true}
return self
end
function c:OnDay(day,func)
self.day[#self.day+1]={day,func,true}
return self
end
function c:OnMonth(month,func)
self.month[#self.month+1]={string.format("%02d",month),func,true}
return self
end
function c:OnYear(year,func)
self.year[#self.year+1]={string.format("%02d",year),func,true}
return self
end
self:create(c)
return c
end end
-- Threading stuff -- Threading stuff
multi.GlobalVariables={} multi.GlobalVariables={}

View File

@ -1,21 +1,7 @@
package.path="?/init.lua;?.lua;"..package.path package.path="?/init.lua;?.lua;"..package.path
multi = require("multi") multi,thread = require("multi"):init()
local a=0 multi:scheduleJob({min = 15, hour = 14},function()
multi:newThread("Test",function() print("hi!")
while true do
thread.hold(function()
return a%4000000==0 and a~=0
end)
print(thread.getCores(),a)
end
end) end)
multi:newThread("Test2",function() --min,hour,day,wday,month
while true do multi:mainloop()
thread.yield()
a=a+1
end
end)
multi.OnError(function(...)
print(...)
end)
multi:threadloop()