From 2eb7712e092f972d848e7e98bb906d5d9aed4c49 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 1 Mar 2024 18:24:14 +0100 Subject: [PATCH] fix: remove success application deployment job wip: daily backup status --- app/Console/Commands/Emails.php | 44 ++++++++++- app/Jobs/ApplicationDeploymentJob.php | 2 +- app/Jobs/DatabaseBackupStatusJob.php | 74 +++++++++++++++++++ app/Models/Application.php | 8 +- app/Models/ScheduledDatabaseBackup.php | 4 + app/Notifications/Database/DailyBackup.php | 50 +++++++++++++ resources/views/emails/daily-backup.blade.php | 19 +++++ 7 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 app/Jobs/DatabaseBackupStatusJob.php create mode 100644 app/Notifications/Database/DailyBackup.php create mode 100644 resources/views/emails/daily-backup.blade.php diff --git a/app/Console/Commands/Emails.php b/app/Console/Commands/Emails.php index 86f317c96..db4122b65 100644 --- a/app/Console/Commands/Emails.php +++ b/app/Console/Commands/Emails.php @@ -2,10 +2,12 @@ namespace App\Console\Commands; +use App\Jobs\DatabaseBackupStatusJob; use App\Jobs\SendConfirmationForWaitlistJob; use App\Models\Application; use App\Models\ApplicationPreview; use App\Models\ScheduledDatabaseBackup; +use App\Models\ScheduledDatabaseBackupExecution; use App\Models\Server; use App\Models\StandalonePostgresql; use App\Models\Team; @@ -15,6 +17,7 @@ use App\Notifications\Application\DeploymentSuccess; use App\Notifications\Application\StatusChanged; use App\Notifications\Database\BackupFailed; use App\Notifications\Database\BackupSuccess; +use App\Notifications\Database\DailyBackup; use App\Notifications\Test; use Exception; use Illuminate\Console\Command; @@ -54,6 +57,8 @@ class Emails extends Command options: [ 'updates' => 'Send Update Email to all users', 'emails-test' => 'Test', + 'database-backup-statuses-daily' => 'Database - Backup Statuses (Daily)', + 'application-deployment-success-daily' => 'Application - Deployment Success (Daily)', 'application-deployment-success' => 'Application - Deployment Success', 'application-deployment-failed' => 'Application - Deployment Failed', 'application-status-changed' => 'Application - Status Changed', @@ -67,8 +72,12 @@ class Emails extends Command ], ); $emailsGathered = ['realusers-before-trial', 'realusers-server-lost-connection']; - if (!in_array($type, $emailsGathered)) { - $this->email = text('Email Address to send to'); + if (isDev()) { + $this->email = "test@example.com"; + } else { + if (!in_array($type, $emailsGathered)) { + $this->email = text('Email Address to send to:'); + } } set_transanctional_email_settings(); @@ -102,7 +111,7 @@ class Emails extends Command $unsubscribeUrl = route('unsubscribe.marketing.emails', [ 'token' => encrypt($email), ]); - $this->mail->view('emails.updates',["unsubscribeUrl" => $unsubscribeUrl]); + $this->mail->view('emails.updates', ["unsubscribeUrl" => $unsubscribeUrl]); $this->sendEmail($email); } } @@ -111,6 +120,35 @@ class Emails extends Command $this->mail = (new Test())->toMail(); $this->sendEmail(); break; + case 'database-backup-statuses-daily': + $scheduled_backups = ScheduledDatabaseBackup::all(); + $databases = collect(); + foreach ($scheduled_backups as $scheduled_backup) { + $last_days_backups = $scheduled_backup->get_last_days_backup_status(); + if ($last_days_backups->isEmpty()) { + continue; + } + $failed = $last_days_backups->where('status', 'failed'); + $database = $scheduled_backup->database; + $databases->put($database->name, [ + 'failed_count' => $failed->count(), + ]); + } + $this->mail = (new DailyBackup($databases))->toMail(); + $this->sendEmail(); + break; + case 'application-deployment-success-daily': + $applications = Application::all(); + foreach ($applications as $application) { + $deployments = $application->get_last_days_deployments(); + ray($deployments); + if ($deployments->isEmpty()) { + continue; + } + $this->mail = (new DeploymentSuccess($application, 'test'))->toMail(); + $this->sendEmail(); + } + break; case 'application-deployment-success': $application = Application::all()->first(); $this->mail = (new DeploymentSuccess($application, 'test'))->toMail(); diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index becf6387a..cded183aa 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1651,7 +1651,7 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); if (!$this->only_this_server) { $this->deploy_to_additional_destinations(); } - $this->application->environment->project->team?->notify(new DeploymentSuccess($this->application, $this->deployment_uuid, $this->preview)); + // $this->application->environment->project->team?->notify(new DeploymentSuccess($this->application, $this->deployment_uuid, $this->preview)); } } diff --git a/app/Jobs/DatabaseBackupStatusJob.php b/app/Jobs/DatabaseBackupStatusJob.php new file mode 100644 index 000000000..b92ed13e9 --- /dev/null +++ b/app/Jobs/DatabaseBackupStatusJob.php @@ -0,0 +1,74 @@ +scheduledDatabaseBackups()->get(); + // if ($scheduled_backups->isEmpty()) { + // continue; + // } + // foreach ($scheduled_backups as $scheduled_backup) { + // $last_days_backups = $scheduled_backup->get_last_days_backup_status(); + // if ($last_days_backups->isEmpty()) { + // continue; + // } + // $failed = $last_days_backups->where('status', 'failed'); + // } + // } + + + + + + + + + // $scheduled_backups = ScheduledDatabaseBackup::all(); + // $databases = collect(); + // $teams = collect(); + // foreach ($scheduled_backups as $scheduled_backup) { + // $last_days_backups = $scheduled_backup->get_last_days_backup_status(); + // if ($last_days_backups->isEmpty()) { + // continue; + // } + // $failed = $last_days_backups->where('status', 'failed'); + // $database = $scheduled_backup->database; + // $team = $database->team(); + // $teams->put($team->id, $team); + // $databases->put("{$team->id}:{$database->name}", [ + // 'failed_count' => $failed->count(), + // ]); + // } + // foreach ($databases as $name => $database) { + // [$team_id, $name] = explode(':', $name); + // $team = $teams->get($team_id); + // $team?->notify(new DailyBackup($databases)); + // } + } +} diff --git a/app/Models/Application.php b/app/Models/Application.php index 4c9f3e1af..4cb941215 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -65,7 +65,8 @@ class Application extends BaseModel return $this->belongsToMany(StandaloneDocker::class, 'additional_destinations') ->withPivot('server_id', 'status'); } - public function is_public_repository(): bool { + public function is_public_repository(): bool + { if (data_get($this, 'source.is_public')) { return true; } @@ -401,7 +402,10 @@ class Application extends BaseModel } return false; } - + public function get_last_days_deployments() + { + return ApplicationDeploymentQueue::where('application_id', $this->id)->where('created_at', '>=', now()->subDays(7))->orderBy('created_at', 'desc')->get(); + } public function deployments(int $skip = 0, int $take = 10) { $deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->orderBy('created_at', 'desc'); diff --git a/app/Models/ScheduledDatabaseBackup.php b/app/Models/ScheduledDatabaseBackup.php index 5c41fc4dc..2cf62cac2 100644 --- a/app/Models/ScheduledDatabaseBackup.php +++ b/app/Models/ScheduledDatabaseBackup.php @@ -30,4 +30,8 @@ class ScheduledDatabaseBackup extends BaseModel { return $this->belongsTo(S3Storage::class, 's3_storage_id'); } + public function get_last_days_backup_status($days = 7) + { + return $this->hasMany(ScheduledDatabaseBackupExecution::class)->where('created_at', '>=', now()->subDays($days))->get(); + } } diff --git a/app/Notifications/Database/DailyBackup.php b/app/Notifications/Database/DailyBackup.php new file mode 100644 index 000000000..dfa508fbd --- /dev/null +++ b/app/Notifications/Database/DailyBackup.php @@ -0,0 +1,50 @@ +subject("Coolify: Daily backup statuses"); + $mail->view('emails.daily-backup', [ + 'databases' => $this->databases, + ]); + return $mail; + } + + public function toDiscord(): string + { + return "Coolify: Daily backup statuses"; + } + public function toTelegram(): array + { + $message = "Coolify: Daily backup statuses"; + return [ + "message" => $message, + ]; + } +} diff --git a/resources/views/emails/daily-backup.blade.php b/resources/views/emails/daily-backup.blade.php new file mode 100644 index 000000000..25d887387 --- /dev/null +++ b/resources/views/emails/daily-backup.blade.php @@ -0,0 +1,19 @@ + +@foreach ($databases as $database_name => $databases) + +@if(data_get($databases,'failed_count') > 0) + +
+ +"{{ $database_name }}" backups: There were some failed backups. Please login and check the logs for more details. + +
+ +@else + +"{{ $database_name }}" backups: All backups were successful. + +@endif + +@endforeach +