Skip to content

A self-hosted media server and file management system

fix: reduce duplicated task code by expanding managed task

aminnausind4e447732bb3
diff --git a/app/Jobs/CleanFolderPaths.php b/app/Jobs/CleanFolderPaths.php
index 73411042..2ed75564 100644
--- a/app/Jobs/CleanFolderPaths.php
+++ b/app/Jobs/CleanFolderPaths.php
@@ -9,7 +9,7 @@ use App\Models\SubTask;
use App\Services\TaskService;
use Illuminate\Support\Facades\Storage;
-class CleanFolderPaths extends ManagedTaskJob {
+class CleanFolderPaths extends ManagedTask {
/**
* Create a new job instance.
*/
@@ -28,21 +28,9 @@ class CleanFolderPaths extends ManagedTaskJob {
try {
$summary = $this->cleanFolderPaths($taskService);
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
- $taskService->updateSubTask($this->subTaskId, [
- 'status' => TaskStatus::COMPLETED,
- 'summary' => $summary,
- 'progress' => 100,
- 'ended_at' => $endedAt,
- 'duration' => $duration,
- ]);
+ $this->completeTask($taskService, $summary);
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
throw $th;
}
}
diff --git a/app/Jobs/CleanVideoPaths.php b/app/Jobs/CleanVideoPaths.php
index 65913670..717e11c2 100644
--- a/app/Jobs/CleanVideoPaths.php
+++ b/app/Jobs/CleanVideoPaths.php
@@ -8,7 +8,7 @@ use App\Models\Video;
use App\Services\TaskService;
use Illuminate\Support\Facades\Storage;
-class CleanVideoPaths extends ManagedTaskJob {
+class CleanVideoPaths extends ManagedTask {
/**
* Create a new job instance.
*/
@@ -27,21 +27,9 @@ class CleanVideoPaths extends ManagedTaskJob {
try {
$summary = $this->cleanVideoPaths($taskService);
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
- $taskService->updateSubTask($this->subTaskId, [
- 'status' => TaskStatus::COMPLETED,
- 'summary' => $summary,
- 'progress' => 100,
- 'ended_at' => $endedAt,
- 'duration' => $duration,
- ]);
+ $this->completeTask($taskService, $summary);
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
throw $th;
}
}
diff --git a/app/Jobs/EmbedUidInMetadata.php b/app/Jobs/EmbedUidInMetadata.php
index 930df1a1..659a2b81 100644
--- a/app/Jobs/EmbedUidInMetadata.php
+++ b/app/Jobs/EmbedUidInMetadata.php
@@ -4,7 +4,6 @@ namespace App\Jobs;
use App\Enums\TaskStatus;
use App\Models\SubTask;
-use App\Models\Task;
use App\Models\Video;
use App\Services\TaskService;
use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
@@ -13,7 +12,7 @@ use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
#[DeleteWhenMissingModels]
-class EmbedUidInMetadata extends ManagedTaskJob {
+class EmbedUidInMetadata extends ManagedTask {
protected $filePath;
protected $uuid;
@@ -46,17 +45,8 @@ class EmbedUidInMetadata extends ManagedTaskJob {
try {
$summary = $this->handleEmbed();
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
-
- $task = $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
- $taskService->updateSubTask($this->subTaskId, [
- 'status' => TaskStatus::COMPLETED,
- 'summary' => $summary,
- 'progress' => 100,
- 'ended_at' => $endedAt,
- 'duration' => $duration,
- ]);
+ $task = $this->completeTask($taskService, $summary);
+
if ($this->videoId) {
Video::where('id', $this->videoId)->update(['uuid' => $this->uuid]);
@@ -84,12 +74,10 @@ class EmbedUidInMetadata extends ManagedTaskJob {
'duration' => $duration < 0 ? $duration * -1 : $duration,
], $status === TaskStatus::COMPLETED);
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
dump($th->getMessage());
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
if ($this->batch()) {
+ // Batch error handling is in the job service
throw $th;
}
$taskService->updateTask($this->taskId, ['status' => TaskStatus::FAILED], true);
diff --git a/app/Jobs/GeneratePreviewImage.php b/app/Jobs/GeneratePreviewImage.php
index ab9717cb..a23fd7cc 100644
--- a/app/Jobs/GeneratePreviewImage.php
+++ b/app/Jobs/GeneratePreviewImage.php
@@ -8,7 +8,7 @@ use App\Services\GenerateImageException;
use App\Services\PreviewGeneratorService;
use App\Services\TaskService;
-class GeneratePreviewImage extends ManagedTaskJob {
+class GeneratePreviewImage extends ManagedTask {
protected $itemTitle;
public $timeout = 3600;
@@ -39,23 +39,9 @@ class GeneratePreviewImage extends ManagedTaskJob {
if (! $result) {
throw new GenerateImageException('Preview image generation failed. View logs for error.');
}
-
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
-
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
- $taskService->updateSubTask($this->subTaskId, [
- 'status' => TaskStatus::COMPLETED,
- 'summary' => 'Generated preview image.',
- 'progress' => 100,
- 'ended_at' => $endedAt,
- 'duration' => $duration,
- ]);
+ $this->completeTask($taskService, 'Generated preview image.');
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
throw $th;
}
}
diff --git a/app/Jobs/IndexFiles.php b/app/Jobs/IndexFiles.php
index dce088a1..e465bfba 100755
--- a/app/Jobs/IndexFiles.php
+++ b/app/Jobs/IndexFiles.php
@@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;
-class IndexFiles extends ManagedTaskJob {
+class IndexFiles extends ManagedTask {
protected $taskService;
protected $embedChain = [];
@@ -44,32 +44,17 @@ class IndexFiles extends ManagedTaskJob {
try {
$summary = $this->generateData();
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskUpdateData = ['sub_tasks_complete' => '++'];
-
- if (count($this->embedChain)) {
- $taskUpdateData = [...$taskUpdateData, 'sub_tasks_total' => count($this->embedChain), 'sub_tasks_current' => count($this->embedChain), 'sub_tasks_pending' => count($this->embedChain)];
- foreach ($this->embedChain as $embedTask) {
- $this->batch()->add($embedTask);
- }
- }
+ $taskCountUpdates = count($this->embedChain) ? ['sub_tasks_complete' => '++', 'sub_tasks_total' => count($this->embedChain), 'sub_tasks_current' => count($this->embedChain), 'sub_tasks_pending' => count($this->embedChain)] : ['sub_tasks_complete' => '++'];
+
+ $this->completeTask($taskService, $summary, $taskCountUpdates);
- $this->taskService->updateTaskCounts($this->taskId, $taskUpdateData, count($taskUpdateData) !== 1);
- $this->taskService->updateSubTask($this->subTaskId, [
- 'status' => TaskStatus::COMPLETED,
- 'summary' => $summary,
- 'progress' => 100,
- 'ended_at' => $endedAt,
- 'duration' => $duration,
- ]);
+ foreach ($this->embedChain as $embedTask) {
+ $this->batch()->add($embedTask);
+ }
} catch (BatchCancelledException $e) {
- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled During the Task']);
+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled During the Task']);
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
throw $th;
}
}
@@ -537,4 +522,5 @@ class IndexFiles extends ManagedTaskJob {
return 'Generated ' . $count . ' ' . $type . ' Changes';
}
}
-class BatchCancelledException extends \Exception {}
+class BatchCancelledException extends \Exception {
+}
diff --git a/app/Jobs/ManagedTask.php b/app/Jobs/ManagedTask.php
new file mode 100644
index 00000000..22ccbc2d
--- /dev/null
+++ b/app/Jobs/ManagedTask.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Enums\TaskStatus;
+use App\Models\Task;
+use App\Services\TaskService;
+use App\Traits\EnsuresTaskIsStarted;
+use App\Traits\HasUpsert;
+use Carbon\Carbon;
+use Illuminate\Bus\Batchable;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\DB;
+use LogicException;
+use Throwable;
+
+abstract class ManagedTask implements ShouldQueue {
+ use Batchable, Dispatchable, EnsuresTaskIsStarted, HasUpsert, InteractsWithQueue, Queueable, SerializesModels;
+
+ protected int $taskId;
+
+ protected ?int $subTaskId = null;
+
+ protected ?Carbon $startedAt = null;
+
+ /**
+ * Sets task to processing if not already set (atomic, also sets started at time)
+ * Subtracts task pending
+ *
+ * Sets subtask started at time
+ * Sets subtask to processing
+ * Sets subtask starting summary
+ */
+ public function beginTask(TaskService $taskService, string $summary = ''): void {
+ if (!$this->taskId) {
+ throw new LogicException('Task ID missing, cannot begin task');
+ }
+
+ if (!$this->subTaskId) {
+ throw new LogicException('SubTask ID missing, cannot begin task');
+ }
+
+ if ($this->batch()?->cancelled()) {
+ // Determine if the batch has been cancelled...
+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
+ $this->delete();
+
+ return;
+ }
+
+ $this->ensureTaskIsStarted($this->taskId);
+ $this->startedAt = now();
+
+ DB::transaction(function () use ($taskService, $summary) {
+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);
+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt, 'summary' => $summary]);
+ });
+ }
+
+ public function completeTask(TaskService $taskService, string $summary = '', array $taskCountUpdates = ['sub_tasks_complete' => '++']): ?Task {
+ $shouldBroadcastTaskUpdate = array_key_exists('sub_tasks_total', $taskCountUpdates);
+
+ $endedAt = now();
+ $duration = $this->getTaskDuration($endedAt);
+
+ $subTaskUpdates = [
+ 'status' => TaskStatus::COMPLETED,
+ 'summary' => $summary,
+ 'progress' => 100,
+ 'ended_at' => $endedAt,
+ 'duration' => $duration,
+ ];
+
+ DB::transaction(function () use ($taskService, $taskCountUpdates, $shouldBroadcastTaskUpdate, $subTaskUpdates) {
+ $taskService->updateTaskCounts($this->taskId, $taskCountUpdates, $shouldBroadcastTaskUpdate); // TODO: Move broadcasts outside of updates?
+ $taskService->updateSubTask($this->subTaskId, $subTaskUpdates);
+ });
+
+ return Task::find($this->taskId);
+ }
+
+ public function failTask(TaskService $taskService, Throwable $th): void {
+ $endedAt = now();
+ $duration = $this->getTaskDuration($endedAt);
+ $subTaskUpdates = ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration];
+
+ DB::transaction(function () use ($taskService, $subTaskUpdates) {
+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
+ $taskService->updateSubTask($this->subTaskId, $subTaskUpdates);
+ });
+ }
+
+ private function getTaskDuration(Carbon $endedAt): int {
+ $startedAt = $this->startedAt ?? $endedAt;
+ return (int) $startedAt->diffInSeconds($endedAt);
+ }
+}
diff --git a/app/Jobs/ManagedTaskJob.php b/app/Jobs/ManagedTaskJob.php
deleted file mode 100644
index b4206ec6..00000000
--- a/app/Jobs/ManagedTaskJob.php
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-
-namespace App\Jobs;
-
-use App\Enums\TaskStatus;
-use App\Services\TaskService;
-use App\Traits\EnsuresTaskIsStarted;
-use App\Traits\HasUpsert;
-use Carbon\Carbon;
-use Illuminate\Bus\Batchable;
-use Illuminate\Bus\Queueable;
-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Foundation\Bus\Dispatchable;
-use Illuminate\Queue\InteractsWithQueue;
-use Illuminate\Queue\SerializesModels;
-
-abstract class ManagedTaskJob implements ShouldQueue {
- use Batchable, Dispatchable, EnsuresTaskIsStarted, HasUpsert, InteractsWithQueue, Queueable, SerializesModels;
-
- protected int $taskId;
-
- protected ?int $subTaskId = null;
-
- protected ?Carbon $startedAt = null;
-
- /**
- * Sets task to processing if not already set (atomic, also sets started at time)
- * Subtracts task pending
- *
- * Sets subtask started at time
- * Sets subtask to processing
- * Sets subtask starting summary
- */
- public function beginTask(TaskService $taskService, string $summary = ''): void {
- if ($this->batch()?->cancelled()) {
- // Determine if the batch has been cancelled...
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
- $this->delete();
-
- return;
- }
-
- $this->ensureTaskIsStarted($this->taskId);
-
- $this->startedAt = now();
-
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt, 'summary' => $summary]);
- }
-}
diff --git a/app/Jobs/SyncFiles.php b/app/Jobs/SyncFiles.php
index 32cae93c..3ac48a84 100755
--- a/app/Jobs/SyncFiles.php
+++ b/app/Jobs/SyncFiles.php
@@ -11,7 +11,7 @@ use App\Services\TaskService;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
-class SyncFiles extends ManagedTaskJob {
+class SyncFiles extends ManagedTask {
/**
* Create a new job instance.
*/
@@ -32,15 +32,9 @@ class SyncFiles extends ManagedTaskJob {
try {
$this->syncCache($taskService);
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED, 'summary' => '', 'ended_at' => $endedAt, 'duration' => $duration, 'progress' => 100]);
+ $this->completeTask($taskService);
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
throw $th;
}
}
diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.php
index 1a623dd5..a120d402 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -16,7 +16,7 @@ use Ramsey\Uuid\Uuid;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
-class VerifyFiles extends ManagedTaskJob {
+class VerifyFiles extends ManagedTask {
/**
* Execute the job.
* id NOT_NULL -> INT8
@@ -57,30 +57,17 @@ class VerifyFiles extends ManagedTaskJob {
try {
$summary = $this->verifyFiles($taskService);
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
+ $taskCountUpdates = count($this->embedChain) ? ['sub_tasks_complete' => '++', 'sub_tasks_total' => count($this->embedChain), 'sub_tasks_pending' => count($this->embedChain)] : ['sub_tasks_complete' => '++'];
- if (count($this->embedChain)) {
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++', 'sub_tasks_total' => count($this->embedChain), 'sub_tasks_pending' => count($this->embedChain)]);
- foreach ($this->embedChain as $embedTask) {
- Bus::dispatch($embedTask);
- }
- } else {
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
- }
+ $this->completeTask($taskService, $summary, $taskCountUpdates);
- $taskService->updateSubTask($this->subTaskId, [
- 'status' => TaskStatus::COMPLETED,
- 'summary' => $summary,
- 'progress' => 100,
- 'ended_at' => $endedAt,
- 'duration' => $duration,
- ]);
+ // Starts other subtasks after updating current subtask and parent subtask states
+ // The parent task "ends" after the batch empties so in theory, this should delay that anyways and does not need to run before the task update
+ foreach ($this->embedChain as $embedTask) {
+ Bus::dispatch($embedTask);
+ }
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
throw $th;
}
}
diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.php
index da1b09f5..c34cc50d 100644
--- a/app/Jobs/VerifyFolders.php
+++ b/app/Jobs/VerifyFolders.php
@@ -13,7 +13,7 @@ use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
-class VerifyFolders extends ManagedTaskJob {
+class VerifyFolders extends ManagedTask {
/**
* Create a new job instance.
*/
@@ -31,21 +31,9 @@ class VerifyFolders extends ManagedTaskJob {
try {
$summary = $this->verifyFolders($taskService);
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
- $taskService->updateSubTask($this->subTaskId, [
- 'status' => TaskStatus::COMPLETED,
- 'summary' => $summary,
- 'progress' => 100,
- 'ended_at' => $endedAt,
- 'duration' => $duration,
- ]);
+ $this->completeTask($taskService, $summary);
} catch (\Throwable $th) {
- $endedAt = now();
- $duration = (int) $this->startedAt->diffInSeconds($endedAt);
- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);
- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $this->failTask($taskService, $th);
throw $th;
}
}