From 534d4e2417f7c1c6f208f215f2a70090a9a6ce58 Mon Sep 17 00:00:00 2001 From: Bohdan Shulha Date: Tue, 15 Oct 2024 17:46:48 +0200 Subject: [PATCH] feat: #227 better node task groups <-> backups relationship management --- app/Actions/Workers/ExecuteWorker.php | 9 ++++-- app/Listeners/RecordBackupStatus.php | 4 +-- app/Models/NodeTaskGroup.php | 6 ++++ ...2024_10_10_183624_create_backups_table.php | 1 - ..._create_backups_node_task_groups_table.php | 28 +++++++++++++++++++ 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2024_10_15_121905_create_backups_node_task_groups_table.php diff --git a/app/Actions/Workers/ExecuteWorker.php b/app/Actions/Workers/ExecuteWorker.php index d29d518..5bd1f0c 100644 --- a/app/Actions/Workers/ExecuteWorker.php +++ b/app/Actions/Workers/ExecuteWorker.php @@ -45,11 +45,15 @@ public function handle(Service $service, Process $process, Worker $worker, ?Back ]); $backup = match ($worker->launchMode) { - LaunchMode::BackupCreate => $this->createBackup($service, $process, $worker, $taskGroup), + LaunchMode::BackupCreate => $this->createBackup($service, $process, $worker), LaunchMode::BackupRestore => $backup, default => null, }; + if ($backup) { + $taskGroup->backups()->attach($backup); + } + $tasks = []; $tasks = [ @@ -105,7 +109,7 @@ private function validate(Service $service, $process, $worker): array return [$service, $process, $worker]; } - private function createBackup(Service $service, Process $process, Worker $worker, NodeTaskGroup $taskGroup): Backup + private function createBackup(Service $service, Process $process, Worker $worker): Backup { $s3Storage = $service->swarm->data->findS3Storage($worker->backupCreate->s3StorageId); if ($s3Storage === null) { @@ -123,7 +127,6 @@ private function createBackup(Service $service, Process $process, Worker $worker $backup->forceFill([ 'team_id' => $service->team_id, - 'task_group_id' => $taskGroup->id, 'service_id' => $service->id, 'process' => $process->dockerName, 'worker' => $worker->dockerName, diff --git a/app/Listeners/RecordBackupStatus.php b/app/Listeners/RecordBackupStatus.php index db4c84c..ab0a943 100644 --- a/app/Listeners/RecordBackupStatus.php +++ b/app/Listeners/RecordBackupStatus.php @@ -33,12 +33,12 @@ public function subscribe(Dispatcher $dispatcher): array */ public function handleCompleted(BackupCreateCompleted $event): void { - Backup::where('task_group_id', $event->taskGroup->id)->update(['status' => BackupStatus::Succeeded, 'ended_at' => now()]); + $event->taskGroup->backups()->update(['status' => BackupStatus::Succeeded, 'ended_at' => now()]); } public function handleFailed(BackupCreateFailed $event): void { - Backup::where('task_group_id', $event->taskGroup->id)->update(['status' => BackupStatus::Failed, 'ended_at' => now()]); + $event->taskGroup->backups()->update(['status' => BackupStatus::Failed, 'ended_at' => now()]); } public function handleRemoveS3FileCompleted(RemoveS3FileCompleted $event): void diff --git a/app/Models/NodeTaskGroup.php b/app/Models/NodeTaskGroup.php index f699543..3e6b57a 100644 --- a/app/Models/NodeTaskGroup.php +++ b/app/Models/NodeTaskGroup.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; @@ -54,6 +55,11 @@ public function node(): BelongsTo return $this->belongsTo(Node::class); } + public function backups(): BelongsToMany + { + return $this->belongsToMany(Backup::class); + } + public function invoker(): BelongsTo { return $this->belongsTo(User::class, 'invoker_id'); diff --git a/database/migrations/2024_10_10_183624_create_backups_table.php b/database/migrations/2024_10_10_183624_create_backups_table.php index 992e48f..4408468 100644 --- a/database/migrations/2024_10_10_183624_create_backups_table.php +++ b/database/migrations/2024_10_10_183624_create_backups_table.php @@ -14,7 +14,6 @@ public function up(): void Schema::create('backups', function (Blueprint $table) { $table->id(); $table->foreignId('team_id')->constrained('teams'); - $table->foreignId('task_group_id')->constrained('node_task_groups'); $table->foreignId('service_id')->constrained('services'); $table->string('process'); $table->string('worker'); diff --git a/database/migrations/2024_10_15_121905_create_backups_node_task_groups_table.php b/database/migrations/2024_10_15_121905_create_backups_node_task_groups_table.php new file mode 100644 index 0000000..1c4f663 --- /dev/null +++ b/database/migrations/2024_10_15_121905_create_backups_node_task_groups_table.php @@ -0,0 +1,28 @@ +id(); + $table->foreignId('backup_id')->constrained('backups')->onDelete('cascade'); + $table->foreignId('node_task_group_id')->constrained('node_task_groups')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::drop('backup_node_task_group'); + } +};