1
|
|
|
<?php |
2
|
|
|
namespace FOA\CronBundle\Manager; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Process\Process; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* CronManager provides easy access to the crontable |
8
|
|
|
* |
9
|
|
|
* @author Novikov Viktor |
10
|
|
|
*/ |
11
|
|
|
class CronManager |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The lines of the cron table, can be cron command or comment |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $lines = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The error when using the comment 'crontab' |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $error; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The output when using the command 'crontab' |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $output; |
33
|
|
|
|
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
// parsing cron file |
37
|
|
|
$process = new Process('crontab -l'); |
38
|
|
|
$process->run(); |
39
|
|
|
$lines = array_filter(explode(PHP_EOL, $process->getOutput()), function ($line) { |
40
|
|
|
return '' != trim($line); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
foreach ($lines as $lineNumber => $line) { |
44
|
|
|
// if line is nt a comment, convert it to a cron |
45
|
|
|
if (strpos($line, '#suspended: ', 0) === 0 || 0 !== strpos($line, '#', 0)) { |
46
|
|
|
try { |
47
|
|
|
$line = Cron::parse($line); |
48
|
|
|
} catch (\Exception $e) { |
49
|
|
|
$process->addErrorOutput('CronManager was unable to parse crontab at line ' . $lineNumber); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
$this->lines['l' . $lineNumber] = $line; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->error = $process->getErrorOutput(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets the array of crons indexed by line number |
60
|
|
|
* |
61
|
|
|
* @return Cron[] |
62
|
|
|
*/ |
63
|
|
|
public function getCrons() |
64
|
|
|
{ |
65
|
|
|
return array_filter($this->lines, function ($line) { |
66
|
|
|
return $line instanceof Cron; |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get a specific cron by its id |
72
|
|
|
* |
73
|
|
|
* @param int $id |
74
|
|
|
* |
75
|
|
|
* @return Cron |
76
|
|
|
*/ |
77
|
|
|
public function getById($id) |
78
|
|
|
{ |
79
|
|
|
$cronList = $this->getCrons(); |
80
|
|
|
if (!isset($cronList[$id])) { |
81
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown cron ID %d', $id)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $cronList[$id]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add a cron to the cron table |
89
|
|
|
* |
90
|
|
|
* @param Cron $cron |
91
|
|
|
*/ |
92
|
|
|
public function add(Cron $cron) |
93
|
|
|
{ |
94
|
|
|
$this->lines[] = $cron; |
95
|
|
|
|
96
|
|
|
$this->write(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Remove a cron from the cron table |
101
|
|
|
* |
102
|
|
|
* @param int $index - the line number |
103
|
|
|
*/ |
104
|
|
|
public function remove($index) |
105
|
|
|
{ |
106
|
|
|
$this->lines = array_diff_key($this->lines, [$index => '']); |
107
|
|
|
|
108
|
|
|
$this->write(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Write the current crons in the cron table |
113
|
|
|
*/ |
114
|
|
|
public function write() |
115
|
|
|
{ |
116
|
|
|
$file = tempnam(sys_get_temp_dir(), 'cron'); |
117
|
|
|
|
118
|
|
|
file_put_contents($file, $this->getRaw() . PHP_EOL); |
119
|
|
|
|
120
|
|
|
$process = new Process('crontab ' . $file); |
121
|
|
|
$process->run(); |
122
|
|
|
|
123
|
|
|
$this->error = $process->getErrorOutput(); |
124
|
|
|
$this->output = $process->getOutput(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets the error output when using the 'crontab' command |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getError() |
133
|
|
|
{ |
134
|
|
|
return $this->error; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets the output when using the 'crontab' command |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function getOutput() |
143
|
|
|
{ |
144
|
|
|
return $this->output; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Gets a representation of the cron table file |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getRaw() |
153
|
|
|
{ |
154
|
|
|
return implode(PHP_EOL, $this->lines); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|