1
+ local CronJobs , CronJobsAt = {}, {}
2
+
3
+ local getTime = function (time , date )
4
+ if date .m then
5
+ time = time + (60 * date .m )
6
+ end
7
+
8
+ if date .h then
9
+ time = time + (60 * 60 * date .h )
10
+ end
11
+
12
+ if date .d then
13
+ time = time + (60 * 60 * 24 * date .d )
14
+ end
15
+
16
+ if date .w then
17
+ time = time + (60 * 60 * 24 * 7 * date .w )
18
+ end
19
+
20
+ return time
21
+ end
22
+
23
+ tickCronJob = function ()
24
+ local currTime = os.time ()
25
+ local currD = tonumber (os.date (' %d' , currTime ))
26
+ local currH = tonumber (os.date (' %H' , currTime ))
27
+ local currM = tonumber (os.date (' %M' , currTime ))
28
+
29
+ for i = 1 , # CronJobs , 1 do
30
+ local timestamp = CronJobs [i ].timestamp
31
+ local d = tonumber (os.date (' %d' , timestamp ))
32
+ local h = tonumber (os.date (' %H' , timestamp ))
33
+ local m = tonumber (os.date (' %M' , timestamp ))
34
+
35
+ if currD == d and currH == h and currM == m then
36
+ logging (' debug' , ' tickCronJob' , os.date (' %d.%m.%Y %H:%M:%S' , currTime ))
37
+ CronJobs [i ].timestamp = getTime (timestamp , CronJobs [i ].date )
38
+ CronJobs [i ].cb (CronJobs [i ].data , {timestamp = currTime , d = currD , h = currH , m = currM })
39
+ end
40
+ end
41
+
42
+ SetTimeout (60000 , tickCronJob )
43
+ end
44
+ tickCronJob ()
45
+
46
+ tickCronJobAt = function ()
47
+ local currTime = os.time ()
48
+ local currD = os.date (' *t' , currTime ).wday
49
+ local currH = tonumber (os.date (' %H' , currTime ))
50
+ local currM = tonumber (os.date (' %M' , currTime ))
51
+
52
+ for i = 1 , # CronJobsAt , 1 do
53
+ if (not CronJobsAt [i ].date .atD or CronJobsAt [i ].date .atD and currD == CronJobsAt [i ].date .atD ) and currH == CronJobsAt [i ].date .atH and currM == CronJobsAt [i ].date .atM then
54
+ CronJobsAt [i ].cb (CronJobsAt [i ].data , {timestamp = currTime , d = currD , h = currH , m = currM })
55
+ end
56
+ end
57
+
58
+ SetTimeout (60000 , tickCronJobAt )
59
+ end
60
+ tickCronJobAt ()
61
+
62
+ MSK .CreateCron = function (date , data , cb )
63
+ local currTime = os.time ()
64
+ local timestamp = date
65
+
66
+ if type (date ) == " table" then
67
+ timestamp = getTime (currTime , date )
68
+ end
69
+
70
+ if currTime == timestamp then
71
+ if date .atH and date .atH > 23 then
72
+ return print (' [^1ERROR^0]' , ' Value "atH" can\' t be greater than 23 on MSK.CreateCron' )
73
+ end
74
+
75
+ if date .atM and date .atM > 59 then
76
+ return print (' [^1ERROR^0]' , ' Value "atM" can\' t be greater than 59 on MSK.CreateCron' )
77
+ end
78
+
79
+ CronJobsAt [# CronJobsAt + 1 ] = {
80
+ date = date ,
81
+ data = data ,
82
+ cb = cb
83
+ }
84
+
85
+ logging (' debug' , ' Created CronJobAT at: ' .. os.date (' %d.%m.%Y %H:%M:%S' , os.time ()), ' Will be executed at: ' .. (' %s:%s' ):format (date .atH , date .atM ) .. ' ' .. (' Day %s (1-7 = Mo - Su)' ):format (date .atD or ' everyday' ))
86
+ else
87
+ CronJobs [# CronJobs + 1 ] = {
88
+ timestamp = timestamp ,
89
+ date = date ,
90
+ data = data ,
91
+ cb = cb
92
+ }
93
+
94
+ logging (' debug' , ' Created CronJob at: ' .. os.date (' %d.%m.%Y %H:%M:%S' , os.time ()), ' Will be executed at: ' .. os.date (' %d.%m.%Y %H:%M:%S' , CronJobs [# CronJobs ].timestamp ))
95
+ end
96
+ end
97
+ exports (' CreateCron' , MSK .CreateCron )
98
+ RegisterNetEvent (' msk_core:createCron' , MSK .CreateCron )
0 commit comments