Skip to content

Commit

Permalink
feat: #227 better node task groups <-> backups relationship management
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Oct 15, 2024
1 parent 1c971bf commit 534d4e2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
9 changes: 6 additions & 3 deletions app/Actions/Workers/ExecuteWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/RecordBackupStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/Models/NodeTaskGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('backup_node_task_group', function (Blueprint $table) {
$table->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');
}
};

0 comments on commit 534d4e2

Please sign in to comment.