From 16e55d89ecce8ea36eb53d78ee70319c37a9f324 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 19 May 2022 20:39:02 +1000 Subject: [PATCH] Add a System maintenance teask --- .env.example | 3 + app/Console/Kernel.php | 3 + app/Jobs/Ninja/SystemMaintenance.php | 132 +++++++++++++++++++++++++++ config/ninja.php | 4 + 4 files changed, 142 insertions(+) create mode 100644 app/Jobs/Ninja/SystemMaintenance.php diff --git a/.env.example b/.env.example index de742ede3b..16c30973aa 100644 --- a/.env.example +++ b/.env.example @@ -53,5 +53,8 @@ PHANTOMJS_SECRET=secret UPDATE_SECRET=secret +DELETE_PDF_DAYS=60 +DELETE_BACKUP_DAYS=60 + COMPOSER_AUTH='{"github-oauth": {"github.com": "${{ secrets.GITHUB_TOKEN }}"}}' SENTRY_LARAVEL_DSN=https://39389664f3f14969b4c43dadda00a40b@sentry2.invoicing.co/5 \ No newline at end of file diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index b472bf3a74..6964b018b2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -19,6 +19,7 @@ use App\Jobs\Ledger\LedgerBalanceUpdate; use App\Jobs\Ninja\AdjustEmailQuota; use App\Jobs\Ninja\CompanySizeCheck; use App\Jobs\Ninja\QueueSize; +use App\Jobs\Ninja\SystemMaintenance; use App\Jobs\Util\DiskCleanup; use App\Jobs\Util\ReminderJob; use App\Jobs\Util\SchedulerCheck; @@ -73,6 +74,8 @@ class Kernel extends ConsoleKernel $schedule->job(new SchedulerCheck)->daily()->withoutOverlapping(); + $schedule->job(new SystemMaintenance)->weekly()->withoutOverlapping(); + if(Ninja::isSelfHost()) { diff --git a/app/Jobs/Ninja/SystemMaintenance.php b/app/Jobs/Ninja/SystemMaintenance.php new file mode 100644 index 0000000000..d86acd3960 --- /dev/null +++ b/app/Jobs/Ninja/SystemMaintenance.php @@ -0,0 +1,132 @@ +maintainPdfs($delete_pdf_days); + + $this->maintainBackups($delete_backup_days); + + } + + private function maintainPdfs(int $delete_pdf_days) + { + if($delete_pdf_days == 0) + return; + + Invoice::with('invitations') + ->whereBetween('created_at', [now()->subYear(), now()->subDays($delete_pdf_days)]) + ->withTrashed() + ->cursor() + ->each(function ($invoice){ + + nlog("deleting invoice {$invoice->number}"); + + $invoice->service()->deletePdf(); + + }); + + Quote::with('invitations') + ->whereBetween('created_at', [now()->subYear(), now()->subDays($delete_pdf_days)]) + ->withTrashed() + ->cursor() + ->each(function ($quote){ + + nlog("deleting quote {$quote->number}"); + + $quote->service()->deletePdf(); + + }); + + Credit::with('invitations') + ->whereBetween('created_at', [now()->subYear(), now()->subDays($delete_pdf_days)]) + ->withTrashed() + ->cursor() + ->each(function ($credit){ + + nlog("deleting credit {$credit->number}"); + + $credit->service()->deletePdf(); + + }); + + + } + + private function maintainBackups(int $delete_backup_days) + { + if($delete_backup_days == 0) + return; + + Backup::where('created_at', '<', now()->subDays($delete_backup_days)) + ->cursor() + ->each(function ($backup){ + + nlog("deleting {$backup->filename}"); + + if($backup->filename) + Storage::disk(config('filesystems.default'))->delete($backup->filename); + + $backup->delete(); + + }); + + } +} diff --git a/config/ninja.php b/config/ninja.php index 08f79e716c..26295c816b 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -155,6 +155,10 @@ return [ 'designs' => [ 'base_path' => resource_path('views/pdf-designs/'), ], + 'maintenance' => [ + 'delete_pdfs' => env('DELETE_PDF_DAYS', 0), + 'delete_backups' => env('DELETE_BACKUP_DAYS', 0), + ], 'log_pdf_html' => env('LOG_PDF_HTML', false), 'expanded_logging' => env('EXPANDED_LOGGING', false), 'snappdf_chromium_path' => env('SNAPPDF_CHROMIUM_PATH', false),