1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| module ..., package.seeall
export class Cron initial: false basket: {} cbFunc: nil enable: true
check = (set,time)-> if set < 0 return time%(-set) == 0 else return set == time
run: => sys.taskInit -> next = (os.date "*t").sec sys.wait (60-next)*1000 while true return unless @enable t = os.date "*t" for i, v in pairs @basket if (check v.min,t.min) and (check v.day,t.day) and (check v.mon,t.month) and (check v.year,t.year) and (check v.week,t.wday) @cbFunc i if @cbFunc next = (os.date "*t").sec sys.wait (61-next)*1000
Initial: => return true if @initial @initial = (os.date "*t").year >= 2020 @run! if @initial @initial
new: => @Initial!
SetCallBack: (func) => @cbFunc = func
Add: (name,min=-1,hour=-1,day=-1,mon=-1,year=-1,week=-1) => @basket[name] = {:min,:hour,:day,:mon,:year,:week}
Del: (name) => @basket[name] = nil
Get: (name) => return unless @basket[name] @basket[name].min,@basket[name].day,@basket[name].mon,@basket[name].year,@basket[name].week
List: => k = {} for i in pairs @basket table.insert k,i k
Distory: => @enable = nil
|