init: scheduled backups

This commit is contained in:
Andras Bacsai
2023-08-08 17:28:36 +02:00
parent b4b1c671bd
commit 46909dca85
11 changed files with 159 additions and 4 deletions

View File

@@ -2,12 +2,14 @@
namespace App\Console;
use App\Jobs\BackupDatabaseJob;
use App\Jobs\CheckResaleLicenseJob;
use App\Jobs\CheckResaleLicenseKeys;
use App\Jobs\DockerCleanupJob;
use App\Jobs\InstanceApplicationsStatusJob;
use App\Jobs\InstanceAutoUpdateJob;
use App\Jobs\ProxyCheckJob;
use App\Models\ScheduledDatabaseBackup;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -15,10 +17,12 @@ class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
// $schedule->call(fn() => $this->check_scheduled_backups($schedule))->everyTenSeconds();
if (isDev()) {
$schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new InstanceApplicationsStatusJob)->everyMinute();
$schedule->job(new ProxyCheckJob)->everyFiveMinutes();
// $schedule->job(new CheckResaleLicenseJob)->hourly();
// $schedule->job(new DockerCleanupJob)->everyOddHour();
// $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
@@ -30,6 +34,24 @@ class Kernel extends ConsoleKernel
$schedule->job(new DockerCleanupJob)->everyTenMinutes();
$schedule->job(new InstanceAutoUpdateJob)->everyTenMinutes();
}
$this->check_scheduled_backups($schedule);
}
private function check_scheduled_backups($schedule)
{
ray('check_scheduled_backups');
$scheduled_backups = ScheduledDatabaseBackup::all();
if ($scheduled_backups->isEmpty()) {
ray('no scheduled backups');
return;
}
foreach ($scheduled_backups as $scheduled_backup) {
if (!$scheduled_backup->enabled) continue;
$schedule->job(new BackupDatabaseJob(
backup: $scheduled_backup
))->cron($scheduled_backup->frequency);
}
}
protected function commands(): void

View File

@@ -11,6 +11,7 @@ use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Throwable;
class Controller extends BaseController
{
@@ -43,7 +44,6 @@ class Controller extends BaseController
$s3s = S3Storage::ownedByCurrentTeam()->get();
$resources = 0;
foreach ($projects as $project) {
ray($project->postgresqls);
$resources += $project->applications->count();
$resources += $project->postgresqls->count();
}
@@ -140,7 +140,7 @@ class Controller extends BaseController
$invitation->delete();
abort(401);
}
} catch (\Throwable $th) {
} catch (Throwable $th) {
throw $th;
}
}
@@ -158,7 +158,7 @@ class Controller extends BaseController
}
$invitation->delete();
return redirect()->route('team.show');
} catch (\Throwable $th) {
} catch (Throwable $th) {
throw $th;
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Http\Livewire\Dev;
use App\Models\ScheduledDatabaseBackup;
use Livewire\Component;
class ScheduledBackups extends Component
{
public $scheduledDatabaseBackup;
public function mount()
{
$this->scheduledDatabaseBackup = ScheduledDatabaseBackup::all();
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class BackupDatabaseJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(public $backup)
{
}
public function handle()
{
ray('BackupDatabaseJob');
ray($this->backup);
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ScheduledDatabaseBackup extends Model
{
protected $guarded = [];
public function database()
{
return $this->morphTo();
}
}

View File

@@ -64,6 +64,11 @@ class StandalonePostgresql extends BaseModel
return $this->morphTo();
}
public function scheduled_database_backups()
{
return $this->morphMany(ScheduledDatabaseBackup::class, 'database');
}
public function environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class);

View File

@@ -2,6 +2,7 @@
namespace App\Notifications\Channels;
use Exception;
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
@@ -15,7 +16,7 @@ class EmailChannel
$recepients = $notifiable->getRecepients($notification);
if (count($recepients) === 0) {
throw new \Exception('No email recipients found');
throw new Exception('No email recipients found');
}
$mailMessage = $notification->toMail($notifiable);