|
81 | 81 | setRemoteCrontab($cronJobs);
|
82 | 82 | });
|
83 | 83 |
|
| 84 | +desc('Remove crontab jobs'); |
| 85 | +task('crontab:remove', function () { |
| 86 | + $cronJobsLocal = array_map( |
| 87 | + fn($job) => parse($job), |
| 88 | + get('crontab:jobs', []), |
| 89 | + ); |
| 90 | + |
| 91 | + $cronJobs = getRemoteCrontab(); |
| 92 | + $identifier = get('crontab:identifier'); |
| 93 | + $sectionStart = "###< $identifier"; |
| 94 | + $sectionEnd = "###> $identifier"; |
| 95 | + |
| 96 | + // Find our cronjob section |
| 97 | + $start = array_search($sectionStart, $cronJobs); |
| 98 | + $end = array_search($sectionEnd, $cronJobs); |
| 99 | + |
| 100 | + if ($start && $end) { |
| 101 | + // Remove the existing section |
| 102 | + array_splice($cronJobs, $start + 1, $end - $start - 1); |
| 103 | + writeln("Crontab: Found existing section, removed jobs"); |
| 104 | + } elseif (count($cronJobsLocal) > 0) { |
| 105 | + $foundJobs = false; |
| 106 | + // Remove individual jobs if no section is present |
| 107 | + foreach ($cronJobs as $index => $cronJob) { |
| 108 | + if (in_array($cronJob, $cronJobsLocal)) { |
| 109 | + unset($cronJobs[$index]); |
| 110 | + $foundJobs = true; |
| 111 | + } |
| 112 | + } |
| 113 | + if ($foundJobs) { |
| 114 | + writeln("Crontab: Found existing jobs in crontab, removed jobs"); |
| 115 | + } else { |
| 116 | + writeln("Crontab: No existing jobs in crontab, skipping"); |
| 117 | + return; |
| 118 | + } |
| 119 | + } else { |
| 120 | + writeln("Crontab: Found no section and crontab:jobs is not configured, skipping"); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + setRemoteCrontab($cronJobs); |
| 125 | +}); |
| 126 | + |
84 | 127 | function setRemoteCrontab(array $lines): void
|
85 | 128 | {
|
86 | 129 | $sudo = get('crontab:use_sudo') ? 'sudo' : '';
|
|
0 commit comments