Skip to content

Commit 242479f

Browse files
committed
feat: #227 better node task groups <-> backups relationship management
1 parent 4a697e4 commit 242479f

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

Diff for: app/Actions/Workers/ExecuteWorker.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ public function handle(Service $service, Process $process, Worker $worker, ?Back
4545
]);
4646

4747
$backup = match ($worker->launchMode) {
48-
LaunchMode::BackupCreate => $this->createBackup($service, $process, $worker, $taskGroup),
48+
LaunchMode::BackupCreate => $this->createBackup($service, $process, $worker),
4949
LaunchMode::BackupRestore => $backup,
5050
default => null,
5151
};
5252

53+
if ($backup) {
54+
$taskGroup->backups()->attach($backup);
55+
}
56+
5357
$tasks = [];
5458

5559
$tasks = [
@@ -105,7 +109,7 @@ private function validate(Service $service, $process, $worker): array
105109
return [$service, $process, $worker];
106110
}
107111

108-
private function createBackup(Service $service, Process $process, Worker $worker, NodeTaskGroup $taskGroup): Backup
112+
private function createBackup(Service $service, Process $process, Worker $worker): Backup
109113
{
110114
$s3Storage = $service->swarm->data->findS3Storage($worker->backupCreate->s3StorageId);
111115
if ($s3Storage === null) {
@@ -123,7 +127,6 @@ private function createBackup(Service $service, Process $process, Worker $worker
123127

124128
$backup->forceFill([
125129
'team_id' => $service->team_id,
126-
'task_group_id' => $taskGroup->id,
127130
'service_id' => $service->id,
128131
'process' => $process->dockerName,
129132
'worker' => $worker->dockerName,

Diff for: app/Listeners/RecordBackupStatus.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public function subscribe(Dispatcher $dispatcher): array
3333
*/
3434
public function handleCompleted(BackupCreateCompleted $event): void
3535
{
36-
Backup::where('task_group_id', $event->taskGroup->id)->update(['status' => BackupStatus::Succeeded, 'ended_at' => now()]);
36+
$event->taskGroup->backups()->update(['status' => BackupStatus::Succeeded, 'ended_at' => now()]);
3737
}
3838

3939
public function handleFailed(BackupCreateFailed $event): void
4040
{
41-
Backup::where('task_group_id', $event->taskGroup->id)->update(['status' => BackupStatus::Failed, 'ended_at' => now()]);
41+
$event->taskGroup->backups()->update(['status' => BackupStatus::Failed, 'ended_at' => now()]);
4242
}
4343

4444
public function handleRemoveS3FileCompleted(RemoveS3FileCompleted $event): void

Diff for: app/Models/NodeTaskGroup.php

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Factories\HasFactory;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\Relations\BelongsTo;
11+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1112
use Illuminate\Database\Eloquent\Relations\HasMany;
1213
use Illuminate\Database\Eloquent\Relations\HasOne;
1314

@@ -54,6 +55,11 @@ public function node(): BelongsTo
5455
return $this->belongsTo(Node::class);
5556
}
5657

58+
public function backups(): BelongsToMany
59+
{
60+
return $this->belongsToMany(Backup::class);
61+
}
62+
5763
public function invoker(): BelongsTo
5864
{
5965
return $this->belongsTo(User::class, 'invoker_id');

Diff for: database/migrations/2024_10_10_183624_create_backups_table.php

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public function up(): void
1414
Schema::create('backups', function (Blueprint $table) {
1515
$table->id();
1616
$table->foreignId('team_id')->constrained('teams');
17-
$table->foreignId('task_group_id')->constrained('node_task_groups');
1817
$table->foreignId('service_id')->constrained('services');
1918
$table->string('process');
2019
$table->string('worker');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('backup_node_task_group', function (Blueprint $table) {
15+
$table->id();
16+
$table->foreignId('backup_id')->constrained('backups')->onDelete('cascade');
17+
$table->foreignId('node_task_group_id')->constrained('node_task_groups')->onDelete('cascade');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
Schema::drop('backup_node_task_group');
27+
}
28+
};

0 commit comments

Comments
 (0)