A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
fix: broken job state without queue worker #198
aminnausin333d1e5deecb
diff --git a/app/Jobs/CleanFolderPaths.php b/app/Jobs/CleanFolderPaths.phpindex 38c87d58..73411042 100644--- a/app/Jobs/CleanFolderPaths.php+++ b/app/Jobs/CleanFolderPaths.php@@ -7,25 +7,9 @@ use App\Exceptions\DataLostException;use App\Models\Folder;use App\Models\SubTask;use App\Services\TaskService;-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\Storage;-class CleanFolderPaths implements ShouldQueue {- use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;-- protected $taskId;-- protected $subTaskId;-- protected $startedAt;-- protected $taskService;-+class CleanFolderPaths extends ManagedTaskJob {/*** Create a new job instance.*/@@ -40,25 +24,14 @@ class CleanFolderPaths implements ShouldQueue {* Execute the job.*/public function handle(TaskService $taskService): void {- $this->taskService = $taskService;-- if ($this->batch() && $this->batch()->cancelled()) {- // Determine if the batch has been cancelled...- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }-- $this->startedAt = now();- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);+ $this->beginTask($taskService);try {- $summary = $this->cleanFolderPaths();+ $summary = $this->cleanFolderPaths($taskService);$endedAt = now();$duration = (int) $this->startedAt->diffInSeconds($endedAt);- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);- $this->taskService->updateSubTask($this->subTaskId, [+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED,'summary' => $summary,'progress' => 100,@@ -68,13 +41,13 @@ class CleanFolderPaths implements ShouldQueue {} 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]);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);throw $th;}}- private function cleanFolderPaths() {+ private function cleanFolderPaths(TaskService $taskService) {if (count($this->folders) == 0) {throw new DataLostException('Folder Data Lost');}@@ -98,7 +71,7 @@ class CleanFolderPaths implements ShouldQueue {array_push($transactions, [...$stored, ...$changes]);}- $this->taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index + 1) / count($this->folders) * 100)]);+ $taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index + 1) / count($this->folders) * 100)]);} catch (\Throwable $th) {$error = true;$errorMessage = 'Error cannot clean folder path ' . $th->getMessage() . ' Cancelling ' . count($transactions) . ' updates';diff --git a/app/Jobs/CleanVideoPaths.php b/app/Jobs/CleanVideoPaths.phpindex 4c86c510..69afd45f 100644--- a/app/Jobs/CleanVideoPaths.php+++ b/app/Jobs/CleanVideoPaths.php@@ -14,16 +14,9 @@ use Illuminate\Queue\InteractsWithQueue;use Illuminate\Queue\SerializesModels;use Illuminate\Support\Facades\Storage;-class CleanVideoPaths implements ShouldQueue {- use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;+class CleanVideoPaths extends ManagedTaskJob {- protected $taskId;- protected $subTaskId;-- protected $startedAt;-- protected $taskService;/*** Create a new job instance.@@ -39,25 +32,14 @@ class CleanVideoPaths implements ShouldQueue {* Execute the job.*/public function handle(TaskService $taskService): void {- $this->taskService = $taskService;-- if ($this->batch() && $this->batch()->cancelled()) {- // Determine if the batch has been cancelled...- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }-- $this->startedAt = now();- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);+ $this->beginTask($taskService);try {- $summary = $this->cleanVideoPaths();+ $summary = $this->cleanVideoPaths($taskService);$endedAt = now();$duration = (int) $this->startedAt->diffInSeconds($endedAt);- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);- $this->taskService->updateSubTask($this->subTaskId, [+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED,'summary' => $summary,'progress' => 100,@@ -67,13 +49,13 @@ class CleanVideoPaths implements ShouldQueue {} 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]);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);throw $th;}}- private function cleanVideoPaths() {+ private function cleanVideoPaths(TaskService $taskService) {if (count($this->videos) == 0) {throw new \Exception('Video Data Lost');}@@ -97,7 +79,7 @@ class CleanVideoPaths implements ShouldQueue {array_push($transactions, [...$stored, ...$changes]);}- $this->taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index + 1) / count($this->videos) * 100)]);+ $taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index + 1) / count($this->videos) * 100)]);} catch (\Throwable $th) {$error = true;$errorMessage = 'Error cannot clean file path ' . $th->getMessage() . ' Cancelling ' . count($transactions) . ' updates';diff --git a/app/Jobs/EmbedUidInMetadata.php b/app/Jobs/EmbedUidInMetadata.phpindex 5398a1b7..4803d5b5 100644--- a/app/Jobs/EmbedUidInMetadata.php+++ b/app/Jobs/EmbedUidInMetadata.php@@ -3,39 +3,25 @@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\Bus\Batchable;-use Illuminate\Bus\Queueable;-use Illuminate\Contracts\Queue\ShouldQueue;-use Illuminate\Foundation\Bus\Dispatchable;use Illuminate\Queue\Attributes\DeleteWhenMissingModels;-use Illuminate\Queue\InteractsWithQueue;-use Illuminate\Queue\SerializesModels;use Illuminate\Support\Facades\App;use Illuminate\Support\Facades\Log;use Symfony\Component\Process\Exception\ProcessFailedException;use Symfony\Component\Process\Process;#[DeleteWhenMissingModels]-class EmbedUidInMetadata implements ShouldQueue {- use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;+class EmbedUidInMetadata extends ManagedTaskJob {protected $filePath;protected $uuid;- protected $taskId;-- protected $subTaskId;-protected $videoId;- protected $startedAt;-- protected $taskService;-public $timeout = 86400;/**@@ -48,8 +34,7 @@ class EmbedUidInMetadata implements ShouldQueue {$this->uuid = $uuid;$this->videoId = $videoId;- $this->taskService = App::make(TaskService::class);- $subTask = $this->taskService->createSubTask(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Embed UID in video file ' . basename(dirname($filePath)) . '/' . basename($filePath)]);+ $subTask = SubTask::create(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Embed UID in video file ' . basename(dirname($filePath)) . '/' . basename($filePath)]);$this->taskId = $taskId;$this->subTaskId = $subTask->id;@@ -59,26 +44,15 @@ class EmbedUidInMetadata implements ShouldQueue {* Execute the job.*/public function handle(TaskService $taskService): void {- $this->taskService = $taskService;-- if ($this->batch()?->cancelled() || (($task = Task::find($this->taskId)) && $task->status == TaskStatus::CANCELLED)) {- // Determine if the batch or parent task has been cancelled...- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }-- $this->startedAt = now();- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt, 'summary' => "Adding uuid to $this->filePath"]);+ $this->beginTask($taskService, "Adding uuid to $this->filePath");try {$summary = $this->handleEmbed();$endedAt = now();$duration = (int) $this->startedAt->diffInSeconds($endedAt);- $task = $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);- $this->taskService->updateSubTask($this->subTaskId, [+ $task = $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED,'summary' => $summary,'progress' => 100,@@ -106,7 +80,7 @@ class EmbedUidInMetadata implements ShouldQueue {$started_at = $task->started_at ? \Carbon\Carbon::parse($task->started_at) : null;$duration = $started_at ? (int) $ended_at->diffInSeconds($started_at) : 0;- $this->taskService->updateTask($task->id, [+ $taskService->updateTask($task->id, ['status' => $status,'ended_at' => $ended_at,'duration' => $duration < 0 ? $duration * -1 : $duration,@@ -115,12 +89,12 @@ class EmbedUidInMetadata implements ShouldQueue {$endedAt = now();$duration = (int) $this->startedAt->diffInSeconds($endedAt);dump($th->getMessage());- $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]);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);if ($this->batch()) {throw $th;}- $this->taskService->updateTask($this->taskId, ['status' => TaskStatus::FAILED], true);+ $taskService->updateTask($this->taskId, ['status' => TaskStatus::FAILED], true);Log::error('Task Failed', ['task_id' => $this->taskId, 'subtask_id' => $this->subTaskId, 'error' => $th->getMessage()]);}}diff --git a/app/Jobs/GeneratePreviewImage.php b/app/Jobs/GeneratePreviewImage.phpindex f9c7e127..ab9717cb 100644--- a/app/Jobs/GeneratePreviewImage.php+++ b/app/Jobs/GeneratePreviewImage.php@@ -3,24 +3,12 @@namespace App\Jobs;use App\Enums\TaskStatus;+use App\Models\SubTask;use App\Services\GenerateImageException;use App\Services\PreviewGeneratorService;use App\Services\TaskService;-use Illuminate\Bus\Batchable;-use Illuminate\Contracts\Queue\ShouldQueue;-use Illuminate\Foundation\Bus\Dispatchable;-use Illuminate\Foundation\Queue\Queueable;-use Illuminate\Queue\InteractsWithQueue;-use Illuminate\Queue\SerializesModels;-use Illuminate\Support\Facades\App;--class GeneratePreviewImage implements ShouldQueue {- use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;-- protected $subTaskId;-- protected $startedAt;+class GeneratePreviewImage extends ManagedTaskJob {protected $itemTitle;public $timeout = 3600;@@ -33,28 +21,18 @@ class GeneratePreviewImage implements ShouldQueue {public function __construct(public array $data,public string $path,- public int $taskId,+ int $taskId,) {- $taskService = App::make(TaskService::class);$this->itemTitle = $data['title'] ?? $path;- $subTask = $taskService->createSubTask(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => "Regenerate Preview Image for '{$this->itemTitle}'"]);+ $subTask = SubTask::create(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => "Regenerate Preview Image for '{$this->itemTitle}'"]);$this->taskId = $taskId;$this->subTaskId = $subTask->id;}public function handle(PreviewGeneratorService $previewGenerator, TaskService $taskService) {- if ($this->batch()->cancelled()) {- // Determine if the batch has been cancelled...- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }-- $this->startedAt = now();- $taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);+ $this->beginTask($taskService);try {$result = $previewGenerator->generateImage($this->data, $this->path, true);diff --git a/app/Jobs/IndexFiles.php b/app/Jobs/IndexFiles.phpindex 9b953422..a170ff9a 100755--- a/app/Jobs/IndexFiles.php+++ b/app/Jobs/IndexFiles.php@@ -10,27 +10,12 @@ use App\Models\Series;use App\Models\SubTask;use App\Models\Video;use App\Services\TaskService;-use Illuminate\Bus\Batchable;-use Illuminate\Bus\Queueable;-use Illuminate\Contracts\Queue\ShouldBeUnique;-use Illuminate\Contracts\Queue\ShouldQueue;-use Illuminate\Foundation\Bus\Dispatchable;-use Illuminate\Queue\InteractsWithQueue;-use Illuminate\Queue\SerializesModels;use Illuminate\Support\Facades\File;-use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Storage;use Illuminate\Support\Str;use Ramsey\Uuid\Uuid;-class IndexFiles implements ShouldBeUnique, ShouldQueue {- use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;-- protected $taskId;-- protected $subTaskId;-- protected $startedAt;+class IndexFiles extends ManagedTaskJob {protected $taskService;@@ -53,21 +38,11 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {* Execute the job.*/public function handle(TaskService $taskService): void {- $this->taskService = $taskService;-- if ($this->batch()->cancelled()) {- // Determine if the batch has been cancelled...- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }+ $this->$taskService = $taskService; // Only for this job for compatibility since this will be re-written soon+ $this->beginTask($taskService, 'Starting Index Files');dump('Starting Index Files');- $this->startedAt = now();- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt, 'summary' => 'Starting Index Files']);-try {$summary = $this->generateData();$endedAt = now();@@ -563,4 +538,5 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {return 'Generated ' . $count . ' ' . $type . ' Changes';}}-class BatchCancelledException extends \Exception {}+class BatchCancelledException extends \Exception {+}diff --git a/app/Jobs/ManagedTaskJob.php b/app/Jobs/ManagedTaskJob.phpnew file mode 100644index 00000000..b4206ec6--- /dev/null+++ b/app/Jobs/ManagedTaskJob.php@@ -0,0 +1,50 @@+<?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.phpindex e97f7be6..dfbf8218 100755--- a/app/Jobs/SyncFiles.php+++ b/app/Jobs/SyncFiles.php@@ -8,27 +8,10 @@ use App\Models\Folder;use App\Models\SubTask;use App\Models\Video;use App\Services\TaskService;-use Illuminate\Bus\Batchable;-use Illuminate\Bus\Queueable;-use Illuminate\Contracts\Queue\ShouldBeUnique;-use Illuminate\Contracts\Queue\ShouldQueue;-use Illuminate\Foundation\Bus\Dispatchable;-use Illuminate\Queue\InteractsWithQueue;-use Illuminate\Queue\SerializesModels;use Illuminate\Support\Facades\Storage;use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;-class SyncFiles implements ShouldBeUnique, ShouldQueue {- use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;-- protected $taskId;-- protected $subTaskId;-- protected $startedAt;-- protected $taskService;-+class SyncFiles extends ManagedTaskJob {/*** Create a new job instance.*/@@ -45,35 +28,24 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {* Execute the job.*/public function handle(TaskService $taskService): void {- $this->taskService = $taskService;-- if ($this->batch() && $this->batch()->cancelled()) {- // Determine if the batch has been cancelled...- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }-- $this->startedAt = now();- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);+ $this->beginTask($taskService);try {- $this->syncCache();+ $this->syncCache($taskService);$endedAt = now();$duration = (int) $this->startedAt->diffInSeconds($endedAt);- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED, 'summary' => '', 'ended_at' => $endedAt, 'duration' => $duration, 'progress' => 100]);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED, 'summary' => '', 'ended_at' => $endedAt, 'duration' => $duration, 'progress' => 100]);} 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]);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);throw $th;}}- public function syncCache() {+ public function syncCache(TaskService $taskService) {// Idea: Compare categories folders and videos json files with data on sql server. Sync local copies with sql server if this is master storage (ie all files should be available)// -> then if you index files, it should delete sql entries correctly if anything there does not exist locally@@ -87,8 +59,8 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {throw new NotFoundHttpException($error);}- $directories = $this->generateCategories();- $subDirectories = $this->generateFolders();+ $directories = $this->generateCategories($taskService);+ $subDirectories = $this->generateFolders($taskService);$files = $this->generateVideos($subDirectories['data']['folderStructure'], $directories['data']['categoryStructure']);if (isset($files['updatedFolderStructure'])) {@@ -115,8 +87,8 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {}}- private function generateCategories() {- $this->taskService->updateSubTask($this->subTaskId, ['summary' => 'Generating Categories']);+ private function generateCategories(TaskService $taskService) {+ $taskService->updateSubTask($this->subTaskId, ['summary' => 'Generating Categories']);$data = Storage::json('categories.json') ?? ['next_ID' => 1, 'categoryStructure' => []]; // array("anime"=>1,"tv"=>2,"yogscast"=>3); // read from json$scanned = Category::all(); // read folder structure@@ -160,8 +132,8 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {return ['categoryChanges' => $changes, 'data' => $data];}- private function generateFolders() {- $this->taskService->updateSubTask($this->subTaskId, ['summary' => 'Generating Folders', 'progress' => 25]);+ private function generateFolders(TaskService $taskService) {+ $taskService->updateSubTask($this->subTaskId, ['summary' => 'Generating Folders', 'progress' => 25]);$data = Storage::json('folders.json') ?? ['next_ID' => 1, 'folderStructure' => []]; // array("anime/frieren"=>array("id"=>0,"name"=>"frieren"),"starwars/andor"=>array("id"=1,"name"="andor")); // read from json$cost = 0;@@ -209,8 +181,8 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {return ['folderChanges' => $changes, 'data' => $data, 'cost' => $cost];}- private function generateVideos($folderStructure) {- $this->taskService->updateSubTask($this->subTaskId, ['summary' => 'Generating Videos', 'progress' => 50]);+ private function generateVideos(TaskService $taskService, $folderStructure) {+ $taskService->updateSubTask($this->subTaskId, ['summary' => 'Generating Videos', 'progress' => 50]);$data = Storage::json('videos.json') ?? ['next_ID' => 1, 'videoStructure' => []]; // array("anime/frieren/S1E01.mp4"=>array("id"=>0,"name"=>"S1E01"),"starwars/andor/S1E01.mkv"=>array("id"=1,"name"="S1E01.mkv")); // read from json$scanned = Video::all();diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.phpindex b3261f3a..beeb2e4a 100755--- a/app/Jobs/VerifyFiles.php+++ b/app/Jobs/VerifyFiles.php@@ -6,15 +6,8 @@ use App\Enums\MediaType;use App\Enums\TaskStatus;use App\Models\Metadata;use App\Models\Record;+use App\Models\SubTask;use App\Services\TaskService;-use App\Traits\HasUpsert;-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\App;use Illuminate\Support\Facades\Bus;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Storage;@@ -23,8 +16,7 @@ use Ramsey\Uuid\Uuid;use Symfony\Component\Process\Exception\ProcessFailedException;use Symfony\Component\Process\Process;-class VerifyFiles implements ShouldQueue {- use Batchable, Dispatchable, HasUpsert, InteractsWithQueue, Queueable, SerializesModels;+class VerifyFiles extends ManagedTaskJob {/*** Execute the job.@@ -47,13 +39,6 @@ class VerifyFiles implements ShouldQueue {* artist -> VARCHAR* album -> VARCHAR*/- protected $taskId;-- protected $subTaskId;-- protected $startedAt;-- protected $taskService;protected $embedChain = [];@@ -63,44 +48,30 @@ class VerifyFiles implements ShouldQueue {* Create a new job instance.*/public function __construct(public $videos, int $taskId) {- //-- $this->taskService = App::make(TaskService::class);- $subTask = $this->taskService->createSubTask(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Verify ' . count($videos) . ' Files']);+ $subTask = SubTask::create(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Verify ' . count($videos) . ' Files']);$this->taskId = $taskId;$this->subTaskId = $subTask->id;}public function handle(TaskService $taskService): void {- $this->taskService = $taskService;-- if ($this->batch()->cancelled()) {- // Determine if the batch has been cancelled...- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }-- $this->startedAt = now();- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);+ $this->beginTask($taskService);try {- $summary = $this->verifyFiles();+ $summary = $this->verifyFiles($taskService);$endedAt = now();$duration = (int) $this->startedAt->diffInSeconds($endedAt);if (count($this->embedChain)) {- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++', 'sub_tasks_total' => count($this->embedChain), 'sub_tasks_pending' => 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 {- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);}- $this->taskService->updateSubTask($this->subTaskId, [+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED,'summary' => $summary,'progress' => 100,@@ -110,13 +81,13 @@ class VerifyFiles implements ShouldQueue {} 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]);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);throw $th;}}- private function verifyFiles() {+ private function verifyFiles(TaskService $taskService) {$transactions = [];$error = false;$index = 0;@@ -318,7 +289,7 @@ class VerifyFiles implements ShouldQueue {*/}$index += 1;- $this->taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->videos)) * 100)]);+ $taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->videos)) * 100)]);}} catch (\Throwable $th) {$ids = array_column($transactions, 'id');diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.phpindex 27c12d2d..da1b09f5 100644--- a/app/Jobs/VerifyFolders.php+++ b/app/Jobs/VerifyFolders.php@@ -7,29 +7,13 @@ use App\Exceptions\DataLostException;use App\Models\Series;use App\Models\SubTask;use App\Services\TaskService;-use App\Traits\HasUpsert;-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 Carbon\Carbon;use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Storage;use Illuminate\Support\Str;-class VerifyFolders implements ShouldQueue {- use Batchable, Dispatchable, HasUpsert, InteractsWithQueue, Queueable, SerializesModels;-- protected $taskId;-- protected $subTaskId;-- protected $startedAt;-- protected $taskService;-+class VerifyFolders extends ManagedTaskJob {/*** Create a new job instance.*/@@ -43,25 +27,14 @@ class VerifyFolders implements ShouldQueue {* Execute the job.*/public function handle(TaskService $taskService): void {- $this->taskService = $taskService;-- if ($this->batch()->cancelled()) {- // Determine if the batch has been cancelled...- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);-- return;- }-- $this->startedAt = now();- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);- $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);+ $this->beginTask($taskService);try {- $summary = $this->verifyFolders();+ $summary = $this->verifyFolders($taskService);$endedAt = now();$duration = (int) $this->startedAt->diffInSeconds($endedAt);- $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);- $this->taskService->updateSubTask($this->subTaskId, [+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::COMPLETED,'summary' => $summary,'progress' => 100,@@ -71,13 +44,13 @@ class VerifyFolders implements ShouldQueue {} 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]);+ $taskService->updateTaskCounts($this->taskId, ['sub_tasks_failed' => '++']);+ $taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);throw $th;}}- private function verifyFolders() {+ private function verifyFolders(TaskService $taskService) {if (count($this->folders) == 0) {throw new DataLostException('Folder Data Lost');}@@ -134,7 +107,7 @@ class VerifyFolders implements ShouldQueue {}if (! empty($changes)) {- array_push($transactions, [...$stored, ...$changes]);+ array_push($transactions, [...$stored, ...$changes, 'updated_at' => Carbon::now(config('app.timezone'))]);/*** DEBUG*@@ -145,7 +118,7 @@ class VerifyFolders implements ShouldQueue {}$index += 1;- $this->taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->folders)) * 100)]);+ $taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->folders)) * 100)]);} catch (\Throwable $th) {$error = true;@@ -158,7 +131,7 @@ class VerifyFolders implements ShouldQueue {return 'No Changes Found';}- Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'raw_thumbnail_url', 'total_size', 'primary_media_type']);+ Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'raw_thumbnail_url', 'total_size', 'primary_media_type', 'updated_at']);$summary = 'Updated ' . count($transactions) . ' folders from id ' . ($transactions[0]['folder_id']) . ' to ' . ($transactions[count($transactions) - 1]['folder_id']);dump($summary);diff --git a/app/Services/FileJobService.php b/app/Services/FileJobService.phpindex 2c9c35e4..7ce81c0c 100644--- a/app/Services/FileJobService.php+++ b/app/Services/FileJobService.php@@ -20,7 +20,8 @@ use Illuminate\Support\Facades\Bus;use Illuminate\Support\Facades\Log;class FileJobService {- public function __construct(protected TaskService $taskService) {}+ public function __construct(protected TaskService $taskService) {+ }public function scanFiles(array $data, ?Category $category = null) {$name = 'Scan Files';@@ -145,7 +146,7 @@ class FileJobService {$query = Folder::orderBy('id');if ($category) {- $query->whereHas('category', fn ($q) => $q->where('id', $category->id))+ $query->whereHas('category', fn($q) => $q->where('id', $category->id))->with('category');}@@ -285,11 +286,9 @@ class FileJobService {public function setupBatch(array $chain, Task $task, ?callable $callback = null, ?array $taskData = []) {return Bus::batch($chain)- ->catch(fn (Batch $batch, \Throwable $e) => $this->handleOperationFailure($task, $e))- ->finally(fn (Batch $batch) => $this->finalizeBatch($batch, $task, $callback))- ->before(fn (Batch $batch) => $this->taskService->updateTask($task->id, array_merge([- 'status' => TaskStatus::PROCESSING,- 'started_at' => $task->started_at ?? now(),+ ->catch(fn(Batch $batch, \Throwable $e) => $this->handleOperationFailure($task, $e))+ ->finally(fn(Batch $batch) => $this->finalizeBatch($batch, $task, $callback))+ ->before(fn(Batch $batch) => $this->taskService->updateTask($task->id, array_merge(['batch_id' => $batch->id,], $taskData)))->name($task->name)diff --git a/app/Traits/EnsuresTaskIsStarted.php b/app/Traits/EnsuresTaskIsStarted.phpnew file mode 100644index 00000000..d6cd45f7--- /dev/null+++ b/app/Traits/EnsuresTaskIsStarted.php@@ -0,0 +1,18 @@+<?php++namespace App\Traits;++use App\Enums\TaskStatus;+use App\Models\Task;++trait EnsuresTaskIsStarted {+ public function ensureTaskIsStarted(int $taskId): bool {+ return Task::where('id', $taskId)+ ->where('status', TaskStatus::PENDING)+ ->whereNull('started_at')+ ->update([+ 'status' => TaskStatus::PROCESSING,+ 'started_at' => now(),+ ]) === 1;+ }+}