forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crontab.php
134 lines (102 loc) · 3.3 KB
/
crontab.php
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
Recipe for adding crontab jobs.
It checks for duplicates by the command part of the job. Changing the schedule will update the crontab. So when you change the command part you have to manually remove the old one. Use `crontab -e` on the server to remove it.
## Configuration
- *crontab:jobs* - An array of strings with crontab lines.
## Usage
```php
require 'contrib/crontab.php';
after('deploy:success', 'crontab:sync');
add('crontab:jobs', [
'* * * * * cd {{current_path}} && {{bin/php}} artisan schedule:run >> /dev/null 2>&1',
]);
```
*/
namespace Deployer;
// Get path to bin
set('bin/crontab', function () {
return locateBinaryPath('crontab');
});
desc('Load crontab');
task('crontab:load', function () {
set('crontab:all', []);
// Crontab is empty
if (!test ("{{bin/crontab}} -l >> /dev/null 2>&1")) {
return;
}
$cronData = run ("{{bin/crontab}} -l");
$cronLines = explode (PHP_EOL, $cronData);
$currentTasks = [];
foreach ($cronLines as $cronLine) {
$jobData = parseJob($cronLine);
if (is_null ($jobData)) {
continue;
}
$currentTasks[$jobData['ckey']] = $jobData;
}
set ('crontab:all', $currentTasks);
});
desc('Sync crontab jobs');
task('crontab:sync', function () {
$syncJobs = get('crontab:jobs', []);
if (count ($syncJobs) == 0) {
writeln("Nothing to sync - configure crontab:jobs");
return;
}
// Load current jobs
invoke('crontab:load');
$cronJobs = get('crontab:all');
foreach ($syncJobs as $syncJob) {
$syncJob = parse($syncJob);
$syncJobData = parseJob($syncJob);
if (is_null ($syncJobData)) {
continue;
}
$cronJobData = $cronJobs[$syncJobData['ckey']] ?? NULL;
if (!is_null ($cronJobData) && $cronJobData['skey'] == $syncJobData['skey']) {
// Job is exists and correct
writeLn($syncJobData['cmd'] . ': <fg=green;options=bold>OK</>');
}
else {
if (is_null ($cronJobData)) {
writeLn($syncJobData['cmd'] . ': <fg=yellow;options=bold>NEW</>');
}
else {
writeLn($syncJobData['cmd'] . ': <fg=red;options=bold>FIX</>');
}
$cronJobs[$syncJobData['ckey']] = $syncJobData;
}
}
if (test ("[ -f '/tmp/crontab_save' ]")) {
run ("unlink '/tmp/crontab_save'");
}
foreach ($cronJobs as $cronJob) {
$jobString = $cronJob['minute'] . ' ' . $cronJob['hour'] . ' ' . $cronJob['day'] . ' ' . $cronJob['month'] . ' ' . $cronJob['weekday'] . ' ' . $cronJob['cmd'];
run ("echo '" . $jobString . "' >> '/tmp/crontab_save'");
}
run ("{{bin/crontab}} /tmp/crontab_save");
run ('unlink /tmp/crontab_save');
});
function parseJob ($job) {
if (!is_string($job)) {
return NULL;
}
if (substr ($job, 0, 1) == '#') {
return NULL;
}
$jobData = explode (' ', $job, 6);
if (count ($jobData) != 6) {
return NULL;
}
return [
'skey' => md5 ($job),
'ckey' => md5 ($jobData['5']),
'minute' => $jobData['0'],
'hour' => $jobData['1'],
'day' => $jobData['2'],
'month' => $jobData['3'],
'weekday' => $jobData['4'],
'cmd' => $jobData['5'],
];
}