Skip to content

Commit

Permalink
Crontab: Move existing cronjobs to the section when first generating …
Browse files Browse the repository at this point in the history
…to prevent breaking backwards compatibility

Generate new docs
  • Loading branch information
ardentsword committed Oct 12, 2023
1 parent 6ee657f commit 8516493
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
19 changes: 14 additions & 5 deletions contrib/crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@

desc('Sync crontab jobs');
task('crontab:sync', function () {
$cronJobsLocal = get('crontab:jobs', []);
$cronJobsLocal = array_map(
fn($job) => parse($job),
get('crontab:jobs', [])
);

if (count($cronJobsLocal) == 0) {
writeln("Nothing to sync - configure crontab:jobs");
Expand All @@ -52,11 +55,17 @@
$end = array_search($sectionEnd, $cronJobs);

if ($start === false || $end === false) {
// Section is not found, create the section
$cronJobs[] = $sectionStart;
foreach ($cronJobsLocal as $cronJobLocal) {
$cronJobs[] = parse($cronJobLocal);
// 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");
}
}

// Create the section
$cronJobs[] = $sectionStart;
$cronJobs += $cronJobsLocal;
$cronJobs[] = $sectionEnd;
writeln("Crontab: Found no section, created the section with configured jobs");
} else {
Expand Down
36 changes: 8 additions & 28 deletions docs/contrib/crontab.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,9 @@ require 'contrib/crontab.php';
[Source](/contrib/crontab.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',
]);
```


## Configuration
### bin/crontab
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L26)
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L28)

Get path to bin

Expand All @@ -44,19 +22,21 @@ return which('crontab');
```


### crontab:identifier
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L32)

## Tasks

### crontab:load
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L31)

Loads crontab.
```php title="Default value"
return get('application', 'application');
```



## Tasks

### crontab:sync
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L56)
[Source](https://github.com/deployphp/deployer/blob/master/contrib/crontab.php#L37)

Sync crontab jobs.

Expand Down

0 comments on commit 8516493

Please sign in to comment.