-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCron.php
More file actions
119 lines (98 loc) · 2.2 KB
/
Copy pathCron.php
File metadata and controls
119 lines (98 loc) · 2.2 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Dreamcoil;
class Cron
{
/**
* @var array
*/
private $jobs = [];
/**
* Checks if a job should be run
*
* @param $job
* @param $timefields
* @return bool
*/
private function runable($job, $timefields)
{
$time = explode(" ", $job['time']);
foreach($time as $key => $value) {
$return[$key] = false;
// Allow rules like "*/5" every 5 minutes
$modulo = false;
if(strpos($value,'*/') !== false) {
$devision_value = explode('/', $value)[1];
if($timefields[$key]%intval($devision_value)==0) {
$modulo = true;
}
}
if($timefields[$key] == $value || $value == '*' || $modulo) {
$return[$key] = true;
}
}
if(in_array(false, $return)) {
return false;
}
return true;
}
/**
* Returns the timefield
*
* @param $time
* @return array
*/
private function getTimeFields($time)
{
$return[0] = date('i', $time);
if($return[0]{0} == 0) {
$return[0]{0} = $return[0]{1};
$return[0] = substr($return[0], 0, -1);
}
$return[1] = date('G', $time);
$return[] = date('j', $time);
$return[] = date('n', $time);
$return[] = date('N', $time);
$return[] = date('Y', $time);
return $return;
}
/**
* Run the cronjobs
*/
public function run()
{
$timefields = $this->getTimeFields(time());
foreach($this->getJobs() as $job)
{
if($this->runable($job, $timefields)) {
echo exec($job['command']);
}
}
}
/**
* Get all the jobs
*
* @return array
*/
public function getJobs()
{
return $this->jobs;
}
/**
* Set all the jobs
*
* @param $jobs
*/
public function setJobs($jobs)
{
$this->jobs = $jobs;
}
/**
* Add a job to the list
*
* @param $job
*/
public function addJob($job)
{
$this->jobs[] = $job;
}
}