2020-06-13 12:58:22 +03:00
|
|
|
<?php
|
2020-10-02 00:11:13 +02:00
|
|
|
/*
|
2020-10-31 20:40:36 +01:00
|
|
|
Recipe for adding crontab jobs.
|
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
This recipe creates a new section in the crontab file with the configured jobs.
|
|
|
|
The section is identified by the *crontab:identifier* variable, by default the application name.
|
2020-10-31 20:40:36 +01:00
|
|
|
|
|
|
|
## 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',
|
|
|
|
]);
|
|
|
|
```
|
2020-06-13 12:58:22 +03:00
|
|
|
*/
|
2023-10-12 15:54:44 +02:00
|
|
|
|
2020-06-13 12:58:22 +03:00
|
|
|
namespace Deployer;
|
|
|
|
|
|
|
|
// Get path to bin
|
|
|
|
set('bin/crontab', function () {
|
2021-11-05 15:23:34 +01:00
|
|
|
return which('crontab');
|
2020-06-13 12:58:22 +03:00
|
|
|
});
|
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
// Set the identifier used in the crontab, application name by default
|
|
|
|
set('crontab:identifier', function () {
|
|
|
|
return get('application', 'application');
|
2020-06-13 12:58:22 +03:00
|
|
|
});
|
|
|
|
|
2023-12-22 10:13:44 +01:00
|
|
|
// Use sudo to run crontab. When running crontab with sudo, you can use the `-u` parameter to change a crontab for a different user.
|
|
|
|
set('crontab:use_sudo', false);
|
|
|
|
|
2020-06-13 12:58:22 +03:00
|
|
|
desc('Sync crontab jobs');
|
|
|
|
task('crontab:sync', function () {
|
2023-10-12 15:54:44 +02:00
|
|
|
$cronJobsLocal = array_map(
|
|
|
|
fn($job) => parse($job),
|
|
|
|
get('crontab:jobs', [])
|
|
|
|
);
|
2020-06-13 12:58:22 +03:00
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
if (count($cronJobsLocal) == 0) {
|
2020-06-13 12:58:22 +03:00
|
|
|
writeln("Nothing to sync - configure crontab:jobs");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
$cronJobs = getRemoteCrontab();
|
|
|
|
$identifier = get('crontab:identifier');
|
|
|
|
$sectionStart = "###< $identifier";
|
|
|
|
$sectionEnd = "###> $identifier";
|
|
|
|
|
|
|
|
// find our cronjob section
|
|
|
|
$start = array_search($sectionStart, $cronJobs);
|
|
|
|
$end = array_search($sectionEnd, $cronJobs);
|
|
|
|
|
|
|
|
if ($start === false || $end === false) {
|
|
|
|
// Move the duplicates over when first generating the section
|
|
|
|
foreach ($cronJobs as $index => $cronJob) {
|
|
|
|
if (in_array($cronJob, $cronJobsLocal)) {
|
|
|
|
unset($cronJobs[$index]);
|
|
|
|
writeln("Crontab: Found existing job in crontab, moving it to the section");
|
2020-06-13 12:58:22 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 15:54:44 +02:00
|
|
|
|
|
|
|
// Create the section
|
|
|
|
$cronJobs[] = $sectionStart;
|
2023-11-21 17:31:14 +02:00
|
|
|
$cronJobs = [...$cronJobs, ...$cronJobsLocal];
|
2023-10-12 15:54:44 +02:00
|
|
|
$cronJobs[] = $sectionEnd;
|
|
|
|
writeln("Crontab: Found no section, created the section with configured jobs");
|
|
|
|
} else {
|
|
|
|
// Replace the existing section
|
|
|
|
array_splice($cronJobs, $start + 1, $end - $start - 1, $cronJobsLocal);
|
|
|
|
writeln("Crontab: Found existing section, replaced with configured jobs");
|
2020-06-13 12:58:22 +03:00
|
|
|
}
|
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
setRemoteCrontab($cronJobs);
|
|
|
|
});
|
|
|
|
|
|
|
|
function setRemoteCrontab(array $lines): void
|
|
|
|
{
|
2023-12-22 10:13:44 +01:00
|
|
|
$sudo = get('crontab:use_sudo') ? 'sudo' : '';
|
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
$tmpCrontabPath = sprintf('/tmp/%s', uniqid('crontab_save_'));
|
2022-08-28 18:50:55 +02:00
|
|
|
|
|
|
|
if (test("[ -f '$tmpCrontabPath' ]")) {
|
|
|
|
run("unlink '$tmpCrontabPath'");
|
2020-06-13 12:58:22 +03:00
|
|
|
}
|
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
foreach ($lines as $line) {
|
|
|
|
run("echo '" . $line . "' >> $tmpCrontabPath");
|
2020-06-13 12:58:22 +03:00
|
|
|
}
|
|
|
|
|
2023-12-22 10:13:44 +01:00
|
|
|
run("$sudo {{bin/crontab}} " . $tmpCrontabPath);
|
2022-08-28 18:50:55 +02:00
|
|
|
run('unlink ' . $tmpCrontabPath);
|
2023-10-12 15:54:44 +02:00
|
|
|
}
|
2020-06-13 12:58:22 +03:00
|
|
|
|
2023-10-12 15:54:44 +02:00
|
|
|
function getRemoteCrontab(): array
|
|
|
|
{
|
2023-12-22 10:13:44 +01:00
|
|
|
$sudo = get('crontab:use_sudo') ? 'sudo' : '';
|
|
|
|
|
|
|
|
if (!test("$sudo {{bin/crontab}} -l >> /dev/null 2>&1")) {
|
2023-10-12 15:54:44 +02:00
|
|
|
return [];
|
2020-06-13 12:58:22 +03:00
|
|
|
}
|
|
|
|
|
2023-12-22 10:13:44 +01:00
|
|
|
return explode(PHP_EOL, run("$sudo {{bin/crontab}} -l"));
|
2020-06-13 12:58:22 +03:00
|
|
|
}
|
|
|
|
|