Skip to content

A self-hosted media server and file management system

task live updates

aminnausinc3e628e222cc
diff --git a/app/Events/LibraryUpdated.php b/app/Events/LibraryUpdated.php
new file mode 100644
index 00000000..1236d843
--- /dev/null
+++ b/app/Events/LibraryUpdated.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Events;
+
+use App\Http\Resources\CategoryResource;
+use App\Models\Category;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class LibraryUpdated implements ShouldBroadcast {
+ use Dispatchable, InteractsWithSockets, SerializesModels;
+
+ /**
+ * The name of the queue on which to place the broadcasting job.
+ *
+ * @var string
+ */
+ public $queue = 'high';
+
+ /**
+ * Create a new event instance.
+ */
+ public function __construct(public Category $library) {
+ //
+ }
+
+ /**
+ * Get the channels the event should broadcast on.
+ *
+ * @return array<int, \Illuminate\Broadcasting\Channel>
+ */
+ public function broadcastOn(): array {
+ return [
+ new PrivateChannel('dashboard.libraries'),
+ ];
+ }
+
+ /**
+ * Get the data to broadcast.
+ */
+ public function broadcastWith(): array {
+ return ['library' => new CategoryResource($this->library)];
+ }
+}
diff --git a/app/Events/SubTaskUpdated.php b/app/Events/SubTaskUpdated.php
new file mode 100644
index 00000000..e4d9b6ba
--- /dev/null
+++ b/app/Events/SubTaskUpdated.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Events;
+
+use App\Http\Resources\SubTasksResource;
+use App\Models\SubTask;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class SubTaskUpdated implements ShouldBroadcast {
+ use Dispatchable, InteractsWithSockets, SerializesModels;
+
+ /**
+ * The name of the queue on which to place the broadcasting job.
+ *
+ * @var string
+ */
+ public $queue = 'high';
+
+ /**
+ * Create a new event instance.
+ */
+ public function __construct(public SubTask $subTask) {
+ //
+ }
+
+ /**
+ * Get the channels the event should broadcast on.
+ *
+ * @return array<int, \Illuminate\Broadcasting\Channel>
+ */
+ public function broadcastOn(): array {
+ try {
+ // code...
+ return [
+ new PrivateChannel("tasks.{$this->subTask->task_id}.subtasks"),
+ ];
+ } catch (\Throwable $th) {
+ dump($th->getMessage());
+ }
+ }
+
+ /**
+ * Get the data to broadcast.
+ */
+ public function broadcastWith(): array {
+ return ['subTask' => new SubTasksResource($this->subTask)];
+ }
+}
diff --git a/app/Events/TaskEnded.php b/app/Events/TaskEnded.php
index 825f5f02..2919359b 100644
--- a/app/Events/TaskEnded.php
+++ b/app/Events/TaskEnded.php
@@ -15,6 +15,13 @@ use Illuminate\Queue\SerializesModels;
class TaskEnded implements ShouldBroadcast {
use Dispatchable, InteractsWithSockets, SerializesModels;
+ /**
+ * The name of the queue on which to place the broadcasting job.
+ *
+ * @var string
+ */
+ public $queue = 'high';
+
/**
* Create a new event instance.
*/
@@ -32,7 +39,7 @@ class TaskEnded implements ShouldBroadcast {
// code...
return [
new PrivateChannel("tasks.{$this->task->id}"),
- new PrivateChannel('dashboard.tasks'),
+ // new PrivateChannel('dashboard.tasks'),
];
} catch (\Throwable $th) {
dump($th->getMessage());
diff --git a/app/Events/TaskUpdated.php b/app/Events/TaskUpdated.php
new file mode 100644
index 00000000..f2df7fb1
--- /dev/null
+++ b/app/Events/TaskUpdated.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Events;
+
+use App\Http\Resources\TasksResource;
+use App\Models\Task;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class TaskUpdated implements ShouldBroadcast {
+ use Dispatchable, InteractsWithSockets, SerializesModels;
+
+ /**
+ * The name of the queue on which to place the broadcasting job.
+ *
+ * @var string
+ */
+ public $queue = 'high';
+
+ /**
+ * Create a new event instance.
+ */
+ public function __construct(public Task $task) {
+ //
+ }
+
+ /**
+ * Get the channels the event should broadcast on.
+ *
+ * @return array<int, \Illuminate\Broadcasting\Channel>
+ */
+ public function broadcastOn(): array {
+ try {
+ return [
+ new PrivateChannel('dashboard.tasks'),
+ ];
+ } catch (\Throwable $th) {
+ dump($th->getMessage());
+ }
+ }
+
+ /**
+ * Get the data to broadcast.
+ */
+ public function broadcastWith(): array {
+ return ['task' => new TasksResource($this->task)];
+ }
+}
diff --git a/app/Http/Controllers/Api/V1/TasksController.php b/app/Http/Controllers/Api/V1/TasksController.php
index 54817fe6..f52beee1 100644
--- a/app/Http/Controllers/Api/V1/TasksController.php
+++ b/app/Http/Controllers/Api/V1/TasksController.php
@@ -2,12 +2,15 @@
namespace App\Http\Controllers\Api\V1;
+use App\Enums\TaskStatus;
use App\Http\Controllers\Controller;
use App\Http\Resources\TasksResource;
use App\Models\Task;
+use App\Services\TaskService;
use App\Traits\HttpResponses;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\DB;
class TasksController extends Controller {
@@ -61,6 +64,7 @@ class TasksController extends Controller {
'count_cancelled' => 0,
'avg_count_sub_tasks' => $taskCounts->avg_count_sub_tasks,
'count_tasks' => $taskCounts->count_tasks,
+ 'count_running' => Task::where('status', TaskStatus::PROCESSING)->count(),
'count_subtasks' => $results->count_subtasks,
];
@@ -80,6 +84,25 @@ class TasksController extends Controller {
return $this->isNotAuthorised() ?? $task->delete() ? $this->success('', 'Success', 200) : $this->error('', 'Not found', 404);
}
+ public function cancel(Task $task, TaskService $taskService) {
+ if ($task->batch_id) {
+ $batch = Bus::findBatch($task->batch_id);
+ if ($batch) {
+ $batch->cancel();
+ $taskService->updateTask($task->id, ['status' => TaskStatus::CANCELLED]);
+
+ return response()->json(['message' => 'Batch canceled successfully.']);
+ } else {
+ $taskService->updateTask($task->id, ['status' => TaskStatus::INCOMPLETE]);
+
+ return response()->json(['message' => 'Batch not found.'], 404);
+ }
+ }
+ $taskService->updateTask($task->id, ['status' => TaskStatus::INCOMPLETE]);
+
+ return response()->json(['message' => 'No batch ID associated with this task.'], 400);
+ }
+
private function isNotAuthorised() {
if (Auth::id() != 1) {
return $this->error('', 'Unauthorised request.', 403);
diff --git a/app/Http/Controllers/DirectoryController.php b/app/Http/Controllers/DirectoryController.php
index dd90a416..d9b7a17d 100755
--- a/app/Http/Controllers/DirectoryController.php
+++ b/app/Http/Controllers/DirectoryController.php
@@ -17,6 +17,7 @@ use App\Models\Category;
use App\Models\Folder;
use App\Models\Task;
use App\Models\Video;
+use App\Services\TaskService;
use App\Traits\HttpResponses;
use Illuminate\Bus\Batch;
use Illuminate\Http\Request;
@@ -27,6 +28,12 @@ use Illuminate\Support\Facades\Log;
class DirectoryController extends Controller {
use HttpResponses;
+ protected $taskService;
+
+ public function __construct(TaskService $taskService) {
+ $this->taskService = $taskService;
+ }
+
public function showDirectoryAPI(Request $request) {
// All this does is convert url to ids and names
// IDEALLY it should also load data to prevent requiring more api requests
@@ -304,8 +311,8 @@ class DirectoryController extends Controller {
}
}
- public static function setupTask($userId, $name, $description = '', $taskCount = 0) {
- return Task::create([
+ public function setupTask($userId, $name, $description = '', $taskCount = 0) {
+ return $this->taskService->createTask([
'user_id' => $userId,
'name' => $name,
'description' => $description,
@@ -314,7 +321,7 @@ class DirectoryController extends Controller {
]);
}
- public static function setupBatch($chain, Task $task) {
+ public function setupBatch($chain, Task $task) {
return Bus::batch($chain)->progress(function (Batch $batch) {
// $task->refresh();
// $task->update([
@@ -330,7 +337,7 @@ class DirectoryController extends Controller {
Log::error('Batch failed', ['task_id' => $task->id, 'error' => $e->getMessage()]);
})->finally(function (Batch $batch) use ($task) {
$task->refresh();
- $status = $task->status !== TaskStatus::PROCESSING ? $task->status : ($batch->cancelled() ? TaskStatus::CANCELLED : ($batch->processedJobs() !== $task->sub_tasks_total ? TaskStatus::INCOMPLETE : TaskStatus::COMPLETED));
+ $status = $task->status !== TaskStatus::PROCESSING ? $task->status : ($batch->cancelled() ? TaskStatus::CANCELLED : ($batch->processedJobs() > $task->sub_tasks_total ? TaskStatus::INCOMPLETE : TaskStatus::COMPLETED));
$ended_at = now();
try {
@@ -341,7 +348,7 @@ class DirectoryController extends Controller {
$duration = 0;
}
- $task->update([
+ $this->taskService->updateTask($task->id, [
'status' => $status,
// 'sub_tasks_pending' => $batch->pendingJobs,
// 'sub_tasks_failed' => $batch->failedJobs,
@@ -352,7 +359,7 @@ class DirectoryController extends Controller {
broadcast(new TaskEnded($task));
})->before(function (Batch $batch) use ($task) {
- $task->update([
+ $this->taskService->updateTask($task->id, [
'status' => TaskStatus::PROCESSING,
'started_at' => now(),
]);
diff --git a/app/Jobs/CleanFolderPaths.php b/app/Jobs/CleanFolderPaths.php
index 5989eaf0..63abc4b9 100644
--- a/app/Jobs/CleanFolderPaths.php
+++ b/app/Jobs/CleanFolderPaths.php
@@ -5,13 +5,13 @@ namespace App\Jobs;
use App\Enums\TaskStatus;
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\DB;
use Illuminate\Support\Facades\Storage;
class CleanFolderPaths implements ShouldQueue {
@@ -23,6 +23,8 @@ class CleanFolderPaths implements ShouldQueue {
protected $startedAt;
+ protected $taskService;
+
/**
* Create a new job instance.
*/
@@ -36,24 +38,26 @@ class CleanFolderPaths implements ShouldQueue {
/**
* Execute the job.
*/
- public function handle(): void {
+ public function handle(TaskService $taskService): void {
+ $this->taskService = $taskService;
+
if ($this->batch() && $this->batch()->cancelled()) {
// Determine if the batch has been cancelled...
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
return;
}
$this->startedAt = now();
- DB::table('tasks')->where('id', $this->taskId)->decrement('sub_tasks_pending');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
try {
$summary = $this->cleanFolderPaths();
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
- SubTask::where('id', $this->subTaskId)->update([
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
+ $this->taskService->updateSubTask($this->subTaskId, [
'status' => TaskStatus::COMPLETED,
'summary' => $summary,
'progress' => 100,
@@ -63,8 +67,8 @@ class CleanFolderPaths implements ShouldQueue {
} catch (\Throwable $th) {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_failed');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $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]);
throw $th;
}
}
@@ -93,7 +97,7 @@ class CleanFolderPaths implements ShouldQueue {
array_push($transactions, [...$stored, ...$changes]);
}
- SubTask::where('id', $this->subTaskId)->update(['progress' => (int) (($index + 1) / count($this->folders) * 100)]);
+ $this->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.php
index 5927ae1b..fd495449 100644
--- a/app/Jobs/CleanVideoPaths.php
+++ b/app/Jobs/CleanVideoPaths.php
@@ -5,13 +5,13 @@ namespace App\Jobs;
use App\Enums\TaskStatus;
use App\Models\SubTask;
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\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
-use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class CleanVideoPaths implements ShouldQueue {
@@ -23,6 +23,8 @@ class CleanVideoPaths implements ShouldQueue {
protected $startedAt;
+ protected $taskService;
+
/**
* Create a new job instance.
*/
@@ -36,24 +38,26 @@ class CleanVideoPaths implements ShouldQueue {
/**
* Execute the job.
*/
- public function handle(): void {
+ public function handle(TaskService $taskService): void {
+ $this->taskService = $taskService;
+
if ($this->batch() && $this->batch()->cancelled()) {
// Determine if the batch has been cancelled...
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
return;
}
$this->startedAt = now();
- DB::table('tasks')->where('id', $this->taskId)->decrement('sub_tasks_pending');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
try {
$summary = $this->cleanVideoPaths();
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
- SubTask::where('id', $this->subTaskId)->update([
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
+ $this->taskService->updateSubTask($this->subTaskId, [
'status' => TaskStatus::COMPLETED,
'summary' => $summary,
'progress' => 100,
@@ -63,8 +67,8 @@ class CleanVideoPaths implements ShouldQueue {
} catch (\Throwable $th) {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_failed');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $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]);
throw $th;
}
}
@@ -93,7 +97,7 @@ class CleanVideoPaths implements ShouldQueue {
array_push($transactions, [...$stored, ...$changes]);
}
- SubTask::where('id', $this->subTaskId)->update(['progress' => (int) (($index + 1) / count($this->videos) * 100)]);
+ $this->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.php
index 68f9c66c..6e02c716 100644
--- a/app/Jobs/EmbedUidInMetadata.php
+++ b/app/Jobs/EmbedUidInMetadata.php
@@ -5,6 +5,7 @@ namespace App\Jobs;
use App\Enums\TaskStatus;
use App\Models\SubTask;
use App\Models\Task;
+use App\Services\TaskService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -30,6 +31,8 @@ class EmbedUidInMetadata implements ShouldQueue {
protected $startedAt;
+ protected $taskService;
+
/**
* Create a new job instance.
*
@@ -66,10 +69,10 @@ class EmbedUidInMetadata implements ShouldQueue {
/**
* Execute the job.
- *
- * @return void
*/
- public function handle() {
+ public function handle(TaskService $taskService): void {
+ $this->taskService = $taskService;
+
$this->startedAt = now();
DB::beginTransaction();
@@ -86,13 +89,13 @@ class EmbedUidInMetadata implements ShouldQueue {
Log::error($e);
}
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt, 'summary' => "Adding uuid to $this->filePath"]);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt, 'summary' => "Adding uuid to $this->filePath"]);
try {
$summary = $this->handleEmbed();
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- // DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
+ // $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
$task = Task::where('id', $this->taskId)->first();
if ($task) {
@@ -106,7 +109,7 @@ class EmbedUidInMetadata implements ShouldQueue {
}
}
- SubTask::where('id', $this->subTaskId)->update([
+ $this->taskService->updateSubTask($this->subTaskId, [
'status' => TaskStatus::COMPLETED,
'summary' => $summary,
'progress' => 100,
@@ -117,8 +120,8 @@ class EmbedUidInMetadata implements ShouldQueue {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
dump($th->getMessage());
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_failed');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $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]);
throw $th;
}
}
diff --git a/app/Jobs/IndexFiles.php b/app/Jobs/IndexFiles.php
index ac734cf0..2a10e5b3 100755
--- a/app/Jobs/IndexFiles.php
+++ b/app/Jobs/IndexFiles.php
@@ -9,6 +9,7 @@ use App\Models\Metadata;
use App\Models\Series;
use App\Models\SubTask;
use App\Models\Video;
+use App\Services\TaskService;
use Exception;
use FFMpeg\FFProbe as FFMpegFFProbe;
use Illuminate\Bus\Batchable;
@@ -20,6 +21,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
+use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;
@@ -35,6 +37,8 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
protected $startedAt;
+ protected $taskService;
+
/**
* Create a new job instance.
*/
@@ -47,10 +51,12 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
/**
* Execute the job.
*/
- public function handle(): void {
+ public function handle(TaskService $taskService): void {
+ $this->taskService = $taskService;
+
if ($this->batch()->cancelled()) {
// Determine if the batch has been cancelled...
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
return;
}
@@ -58,15 +64,15 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
dump('Starting Index Files');
$this->startedAt = now();
- DB::table('tasks')->where('id', $this->taskId)->decrement('sub_tasks_pending');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt, 'summary' => 'Starting Index Files']);
+ $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();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
- SubTask::where('id', $this->subTaskId)->update([
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
+ $this->taskService->updateSubTask($this->subTaskId, [
'status' => TaskStatus::COMPLETED,
'summary' => $summary,
'progress' => 100,
@@ -76,8 +82,8 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
} catch (\Throwable $th) {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_failed');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $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]);
throw $th;
}
}
@@ -289,7 +295,7 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
$data['next_ID'] = $currentID;
$data['categoryStructure'] = $current;
- SubTask::where('id', $this->subTaskId)->update(['summary' => 'Generated ' . count($changes) . ' Library Changes', 'progress' => 10]);
+ $this->taskService->updateSubTask($this->subTaskId, ['summary' => 'Generated ' . count($changes) . ' Library Changes', 'progress' => 10]);
return ['categoryChanges' => $changes, 'data' => $data];
}
@@ -367,7 +373,7 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
$data['next_ID'] = $currentID;
$data['folderStructure'] = $current;
- SubTask::where('id', $this->subTaskId)->update(['summary' => 'Generated ' . count($changes) . ' Folder Changes', 'progress' => 30]);
+ $this->taskService->updateSubTask($this->subTaskId, ['summary' => 'Generated ' . count($changes) . ' Folder Changes', 'progress' => 30]);
return ['folderChanges' => $changes, 'data' => $data, 'cost' => $cost, 'seriesChanges' => $seriesChanges];
}
@@ -428,7 +434,6 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
dump("$path$folder");
- // $count = 0;
foreach ($files as $file) {
$cost++;
$absolutePath = str_replace('\\', '/', Storage::disk('public')->path('')) . $file;
@@ -441,12 +446,8 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
$fileMetaData = $this->getFileMetadata($absolutePath);
$uuid = isset($fileMetaData['format']['tags']['uid']) ? $fileMetaData['format']['tags']['uid'] : (isset($fileMetaData['format']['tags']['UID']) ? $fileMetaData['format']['tags']['UID'] : null);
if (! $uuid || ! Uuid::isValid($uuid)) {
- // dd('F');
$uuid = Str::uuid()->toString();
- // if ($count < 1) {
EmbedUidInMetadata::dispatch($absolutePath, $uuid, $this->taskId);
- // }
- // $count += 1;
}
$name = basename($file);
@@ -487,7 +488,7 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
$data['next_ID'] = $currentID;
$data['videoStructure'] = $current;
- SubTask::where('id', $this->subTaskId)->update(['summary' => 'Generated ' . count($changes) . ' Video Changes', 'progress' => 80]);
+ $this->taskService->updateSubTask($this->subTaskId, ['summary' => 'Generated ' . count($changes) . ' Video Changes', 'progress' => 80]);
return ['videoChanges' => $changes, 'data' => $data, 'cost' => $cost, 'updatedFolderStructure' => $foldersCopy, 'metadataChanges' => $metadataChanges];
}
@@ -540,6 +541,8 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
// $tags = $ffprobe->format($filePath)->get('tags'); // extracts file information
// return isset($tags['uid']) ? $tags['uid'] : (isset($tags['UID']) ? $tags['UID'] : null);
+ $ext = pathinfo($filePath, PATHINFO_EXTENSION);
+
$command = [
'ffprobe',
'-v',
@@ -547,6 +550,7 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
'-print_format',
'json',
'-show_format',
+ '-show_streams',
$filePath,
];
@@ -560,6 +564,9 @@ class IndexFiles implements ShouldBeUnique, ShouldQueue {
$output = $process->getOutput(); // Decode JSON output
$metadata = json_decode($output, true);
+ if ($ext === 'ogg') {
+ $metadata['format'] = $metadata['streams'][0] ?? [];
+ }
return $metadata;
} catch (\Throwable $th) {
diff --git a/app/Jobs/SyncFiles.php b/app/Jobs/SyncFiles.php
index 37787df5..2d800702 100755
--- a/app/Jobs/SyncFiles.php
+++ b/app/Jobs/SyncFiles.php
@@ -7,6 +7,7 @@ use App\Models\Category;
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;
@@ -26,6 +27,8 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {
protected $startedAt;
+ protected $taskService;
+
/**
* Create a new job instance.
*/
@@ -39,29 +42,31 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {
/**
* Execute the job.
*/
- public function handle(): void {
+ public function handle(TaskService $taskService): void {
+ $this->taskService = $taskService;
+
if ($this->batch() && $this->batch()->cancelled()) {
// Determine if the batch has been cancelled...
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
return;
}
$this->startedAt = now();
- DB::table('tasks')->where('id', $this->taskId)->decrement('sub_tasks_pending');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
try {
$this->syncCache();
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::COMPLETED, 'summary' => '', 'ended_at' => $endedAt, 'duration' => $duration, 'progress' => 100]);
+ $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]);
} catch (\Throwable $th) {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_failed');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $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]);
throw $th;
}
}
@@ -109,7 +114,7 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {
}
private function generateCategories() {
- SubTask::where('id', $this->subTaskId)->update(['summary' => 'Generating Categories']);
+ $this->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
@@ -154,7 +159,7 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {
}
private function generateFolders($path) {
- SubTask::where('id', $this->subTaskId)->update(['summary' => 'Generating Folders', 'progress' => 25]);
+ $this->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;
@@ -203,7 +208,7 @@ class SyncFiles implements ShouldBeUnique, ShouldQueue {
}
private function generateVideos($path, $folderStructure) {
- SubTask::where('id', $this->subTaskId)->update(['summary' => 'Generating Videos', 'progress' => 50]);
+ $this->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.php
index fa3e0964..83f4e650 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -6,13 +6,14 @@ use App\Enums\TaskStatus;
use App\Models\Metadata;
use App\Models\Record;
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\DB;
+use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
@@ -24,22 +25,6 @@ use Symfony\Component\Process\Process;
class VerifyFiles implements ShouldQueue {
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $taskId;
-
- protected $subTaskId;
-
- protected $startedAt;
-
- /**
- * Create a new job instance.
- */
- public function __construct(public $videos, $taskId) {
- //
- $subTask = SubTask::create(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Verify ' . count($videos) . ' Files']); //
- $this->taskId = $taskId;
- $this->subTaskId = $subTask->id;
- }
-
/**
* Execute the job.
* id NOT_NULL -> INT8
@@ -59,24 +44,49 @@ class VerifyFiles implements ShouldQueue {
* file_size -> INT8
* date_scanned -> INT8
*/
- public function handle(): void {
+ protected $taskId;
+
+ protected $subTaskId;
+
+ protected $startedAt;
+
+ protected $taskService;
+
+ /**
+ * 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']);
+
+ $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...
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
+ // SubTask::where('id', $this->subTaskId)->update();
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
return;
}
$this->startedAt = now();
- DB::table('tasks')->where('id', $this->taskId)->decrement('sub_tasks_pending');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
try {
$summary = $this->verifyFiles();
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
- SubTask::where('id', $this->subTaskId)->update([
+ // DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
+ $this->taskService->updateSubTask($this->subTaskId, [
'status' => TaskStatus::COMPLETED,
'summary' => $summary,
'progress' => 100,
@@ -86,8 +96,8 @@ class VerifyFiles implements ShouldQueue {
} catch (\Throwable $th) {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_failed');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $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]);
throw $th;
}
}
@@ -150,9 +160,9 @@ class VerifyFiles implements ShouldQueue {
$mime_type = isset($changes['mime_type']) ? $changes['mime_type'] : $metadata->mime_type;
- $audioMetadata = (is_null($metadata->description) || (is_null($metadata->episode) && is_null($metadata->season))) && str_starts_with($mime_type, 'audio') ? $this->getAudioDescription($filePath, $fileMetaData ?? null) : [];
+ $audioMetadata = ((is_null($metadata->description) || (! is_null($metadata->date_scanned) && filemtime($filePath) > strtotime($metadata->date_scanned))) && is_null($metadata->season)) && str_starts_with($mime_type, 'audio') ? $this->getAudioDescription($filePath, $fileMetaData ?? null) : [];
- if (is_null($metadata->poster_url) && $mime_type && str_starts_with($mime_type, 'audio')) {
+ if ((is_null($metadata->poster_url) || filemtime($filePath)) && $mime_type && str_starts_with($mime_type, 'audio')) {
$relativePath = $video->folder->path . '/' . $metadata->id;
$coverArtPath = "posters/audio/$relativePath-$uuid.png";
@@ -218,8 +228,7 @@ class VerifyFiles implements ShouldQueue {
// dump($video->name);
}
$index += 1;
- SubTask::where('id', $this->subTaskId)->update(['progress' => (int) (($index / count($this->videos)) * 100)]);
-
+ $this->taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->videos)) * 100)]);
// dump($metadata->toArray());
}
} catch (\Throwable $th) {
diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.php
index 772ebc5a..b48f9df4 100644
--- a/app/Jobs/VerifyFolders.php
+++ b/app/Jobs/VerifyFolders.php
@@ -6,13 +6,13 @@ use App\Enums\TaskStatus;
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\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
-use Illuminate\Support\Facades\DB;
class VerifyFolders implements ShouldQueue {
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@@ -23,6 +23,8 @@ class VerifyFolders implements ShouldQueue {
protected $startedAt;
+ protected $taskService;
+
/**
* Create a new job instance.
*/
@@ -35,24 +37,26 @@ class VerifyFolders implements ShouldQueue {
/**
* Execute the job.
*/
- public function handle(): void {
+ public function handle(TaskService $taskService): void {
+ $this->taskService = $taskService;
+
if ($this->batch()->cancelled()) {
// Determine if the batch has been cancelled...
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::CANCELLED, 'summary' => 'Parent Task was Cancelled']);
return;
}
$this->startedAt = now();
- DB::table('tasks')->where('id', $this->taskId)->decrement('sub_tasks_pending');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_pending' => '--']);
+ $this->taskService->updateSubTask($this->subTaskId, ['status' => TaskStatus::PROCESSING, 'started_at' => $this->startedAt]);
try {
$summary = $this->VerifyFolders();
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_complete');
- SubTask::where('id', $this->subTaskId)->update([
+ $this->taskService->updateTaskCounts($this->taskId, ['sub_tasks_complete' => '++'], false);
+ $this->taskService->updateSubTask($this->subTaskId, [
'status' => TaskStatus::COMPLETED,
'summary' => $summary,
'progress' => 100,
@@ -62,8 +66,8 @@ class VerifyFolders implements ShouldQueue {
} catch (\Throwable $th) {
$endedAt = now();
$duration = (int) $this->startedAt->diffInSeconds($endedAt);
- DB::table('tasks')->where('id', $this->taskId)->increment('sub_tasks_failed');
- SubTask::where('id', $this->subTaskId)->update(['status' => TaskStatus::FAILED, 'summary' => 'Error: ' . $th->getMessage(), 'ended_at' => $endedAt, 'duration' => $duration]);
+ $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]);
throw $th;
}
}
@@ -102,7 +106,7 @@ class VerifyFolders implements ShouldQueue {
}
$index += 1;
- SubTask::where('id', $this->subTaskId)->update(['progress' => (int) (($index / count($this->folders)) * 100)]);
+ $this->taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->folders)) * 100)]);
// dump($series->toArray());
} catch (\Throwable $th) {
diff --git a/app/Services/PulseAPI.php b/app/Services/PulseAPI.php
deleted file mode 100644
index e00a176b..00000000
--- a/app/Services/PulseAPI.php
+++ /dev/null
@@ -1,31 +0,0 @@
-<?php
-
-namespace App\Services;
-
-use App\Http\Resources\Pulse\CacheResource;
-use App\Http\Resources\Pulse\ExceptionsResource;
-use App\Http\Resources\Pulse\QueueResource;
-use App\Http\Resources\Pulse\ServersResource;
-use App\Http\Resources\Pulse\SlowJobsResource;
-use App\Http\Resources\Pulse\SlowOutgoingRequestsResource;
-use App\Http\Resources\Pulse\SlowQueriesResource;
-use App\Http\Resources\Pulse\SlowRequestsResource;
-use App\Http\Resources\Pulse\UsageResource;
-use Illuminate\Support\Collection;
-use Robertogallea\PulseApi\Enum\PulseResourcesEnum;
-
-class PulseAPI {
- public static function getDefaultResources(): Collection {
- return collect([
- PulseResourcesEnum::SERVERS->value => ServersResource::class,
- PulseResourcesEnum::USAGE->value => UsageResource::class,
- PulseResourcesEnum::QUEUES->value => QueueResource::class,
- PulseResourcesEnum::CACHE->value => CacheResource::class,
- PulseResourcesEnum::SLOW_QUERIES->value => SlowQueriesResource::class,
- PulseResourcesEnum::EXCEPTIONS->value => ExceptionsResource::class,
- PulseResourcesEnum::SLOW_REQUESTS->value => SlowRequestsResource::class,
- PulseResourcesEnum::SLOW_JOBS->value => SlowJobsResource::class,
- PulseResourcesEnum::SLOW_OUTGOING_REQUESTS->value => SlowOutgoingRequestsResource::class,
- ]);
- }
-}
diff --git a/app/Services/TaskService.php b/app/Services/TaskService.php
new file mode 100644
index 00000000..71507af5
--- /dev/null
+++ b/app/Services/TaskService.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Services;
+
+use App\Events\SubTaskUpdated;
+use App\Events\TaskUpdated;
+use App\Models\SubTask;
+use App\Models\Task;
+
+class TaskService {
+ public function createTask(array $attrs): Task {
+ $task = Task::create($attrs);
+ broadcast(new TaskUpdated($task));
+
+ return $task;
+ }
+
+ public function createSubTask(array $attrs): SubTask {
+ $subTask = SubTask::create($attrs);
+ // broadcast(new SubTaskUpdated($subTask));
+
+ return $subTask;
+ }
+
+ /**
+ * Update Task by ID.
+ */
+ public function updateTask(int $taskId, array $attr): ?Task {
+ $task = Task::find($taskId);
+
+ if (! $task) {
+ return null;
+ }
+
+ foreach ($attr as $key => $value) {
+ $task->{$key} = $value;
+ }
+ $task->save();
+
+ broadcast(new TaskUpdated($task));
+
+ return $task;
+ }
+
+ /**
+ * Update SubTask by ID.
+ */
+ public function updateSubTask(int $subTaskId, array $attr, bool $broadcast = false): ?SubTask {
+ $subTask = SubTask::find($subTaskId);
+
+ if (! $subTask) {
+ return null;
+ }
+
+ foreach ($attr as $key => $value) {
+ $subTask->{$key} = $value;
+ }
+ $subTask->save();
+ // if ($broadcast === true) broadcast(new SubTaskUpdated($subTask));
+
+ return $subTask;
+ }
+
+ public function updateTaskCounts(int $taskId, array $attr, bool $broadcast = true): ?Task {
+ $task = Task::find($taskId);
+
+ if (! $task) {
+ return null;
+ }
+
+ foreach ($attr as $key => $value) {
+ if ($value === '++') {
+ $task->{$key}++;
+ } else {
+ $task->{$key}--;
+ }
+ }
+ $task->save();
+ if ($broadcast === true) {
+ broadcast(new TaskUpdated($task));
+ }
+
+ return $task;
+ }
+}
diff --git a/config/queue.php b/config/queue.php
index 116bd8d0..eb0b505b 100755
--- a/config/queue.php
+++ b/config/queue.php
@@ -43,6 +43,15 @@ return [
'after_commit' => false,
],
+ 'database_high' => [
+ 'driver' => 'database',
+ 'connection' => env('DB_QUEUE_CONNECTION'),
+ 'table' => 'jobs_high',
+ 'queue' => 'high',
+ 'retry_after' => (int) env('DB_QUEUE_RETRY_AFTER', 90),
+ 'after_commit' => false,
+ ],
+
'beanstalkd' => [
'driver' => 'beanstalkd',
'host' => env('BEANSTALKD_QUEUE_HOST', 'localhost'),
diff --git a/database/migrations/2025_01_04_181821_create_jobs_high_table.php b/database/migrations/2025_01_04_181821_create_jobs_high_table.php
new file mode 100644
index 00000000..ae5ebda4
--- /dev/null
+++ b/database/migrations/2025_01_04_181821_create_jobs_high_table.php
@@ -0,0 +1,29 @@
+<?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('jobs_high', function (Blueprint $table) {
+ $table->bigIncrements('id');
+ $table->string('queue')->index();
+ $table->longText('payload');
+ $table->unsignedTinyInteger('attempts');
+ $table->unsignedInteger('reserved_at')->nullable();
+ $table->unsignedInteger('available_at');
+ $table->unsignedInteger('created_at');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::dropIfExists('jobs_high');
+ }
+};
diff --git a/package.json b/package.json
index d86c3c21..6b5ee23f 100755
--- a/package.json
+++ b/package.json
@@ -6,6 +6,7 @@
"php:serve": "php artisan serve",
"php:pail": "php artisan pail",
"php:queue": "php artisan queue:listen",
+ "php:queue-high": "php artisan queue:listen --queue=high",
"php:pulse": "php artisan pulse:check",
"php:scheduler": "php artisan schedule:work",
"php:reverb": "php artisan reverb:start --host=\"0.0.0.0\" --port=6001 ",
@@ -14,7 +15,7 @@
"vite:dev2": "concurrently -k -p \"[{name}]\" -c \"blue.bold,green.bold,red.bold\" \"npm:php:serve\" \"npm:php:pail\" \"npm:dev\"",
"vite:cron": "concurrently -k -p \"[{name}]\" -c \"blue.bold,green.bold,red.bold,white.bold\" \"npm:php:queue\" \"npm:php:scheduler\" \"npm:dev\" \"npm:php:pulse\"",
"vite:cronws": "concurrently -k -p \"[{name}]\" -c \"blue.bold,green.bold,red.bold,white.bold,magenta.bold\" \"npm:php:queue\" \"npm:php:scheduler\" \"npm:dev\" \"npm:php:pulse\" \"npm:php:reverb\"",
- "vite:cronwsd": "concurrently -k -p \"[{name}]\" -c \"blue.bold,green.bold,red.bold,white.bold,magenta.bold\" \"npm:php:queue\" \"npm:php:scheduler\" \"npm:dev\" \"npm:php:pulse\" \"npm:php:reverb-debug\"",
+ "vite:cronwsd": "concurrently -k -p \"[{name}]\" -c \"blue.bold,green.bold,red.bold,white.bold,magenta.bold,yellow.bold\" \"npm:php:queue\" \"npm:php:scheduler\" \"npm:dev\" \"npm:php:pulse\" \"npm:php:reverb-debug\" \"npm:php:queue-high\"",
"vite:php": "concurrently -k -p \"[{name}]\" -c \"blue.bold,green.bold,white.bold,magenta.bold\" \"npm:php:queue\" \"npm:php:scheduler\" \"npm:php:pulse\" \"npm:php:reverb-debug\"",
"build": "npm run pint && npm run type-check && npm run build-only",
"build-only": "vite build",
diff --git a/resources/js/components/cards/TaskCard.vue b/resources/js/components/cards/TaskCard.vue
index 24eb10ca..e22d0a50 100644
--- a/resources/js/components/cards/TaskCard.vue
+++ b/resources/js/components/cards/TaskCard.vue
@@ -128,16 +128,15 @@ watch(
</h4>
</span>
- <span :class="`grid xs:grid-cols-2 col-span-3 md:col-span-2 gap-x-4`">
- <h4 class="text-xs text-neutral-500 dark:text-neutral-400 truncate line-clamp-1 capitalize w-20" title="Pending Tasks">
- Sub Tasks: {{ data.sub_tasks_total }}
- </h4>
- <h4 class="text-xs text-neutral-500 dark:text-neutral-400 truncate line-clamp-1 capitalize" v-if="data.sub_tasks_failed" title="Failed Tasks">
+ <span
+ :class="`grid xs:grid-cols-2 col-span-3 md:col-span-2 gap-x-4`"
+ :title="`Sub Tasks: ${data.sub_tasks_total}\n\nPending: ${data.sub_tasks_pending}\n\nCompleted: ${data.sub_tasks_complete}\n\nFailed: ${data.sub_tasks_failed}`"
+ >
+ <h4 class="text-xs text-neutral-500 dark:text-neutral-400 truncate line-clamp-1 capitalize w-20">Sub Tasks: {{ data.sub_tasks_total }}</h4>
+ <h4 class="text-xs text-neutral-500 dark:text-neutral-400 truncate line-clamp-1 capitalize" v-if="data.sub_tasks_failed">
Failed: {{ data.sub_tasks_failed }}
</h4>
- <h4 class="text-xs text-neutral-500 dark:text-neutral-400 truncate line-clamp-1 capitalize" v-else title="Pending Tasks">
- Pending: {{ data.sub_tasks_pending }}
- </h4>
+ <h4 class="text-xs text-neutral-500 dark:text-neutral-400 truncate line-clamp-1 capitalize" v-else>Pending: {{ data.sub_tasks_pending }}</h4>
</span>
</div>
</div>
diff --git a/resources/js/components/dashboard/DashboardAnalytics.vue b/resources/js/components/dashboard/DashboardAnalytics.vue
index 28a08e3f..ddfd131e 100644
--- a/resources/js/components/dashboard/DashboardAnalytics.vue
+++ b/resources/js/components/dashboard/DashboardAnalytics.vue
@@ -107,7 +107,9 @@ watch(
<ButtonText
class="h-8 text-rose-600 dark:!bg-rose-700 disabled:opacity-60"
:title="'Scan and Index All Files For Metadata'"
- @click.stop.prevent="handleStartTask('scan')"
+ @click.stop.prevent="handleStartTask('scan').then(() => {
+ taskPopover?.handleClose();
+ })"
>
<template #text> Scan All Files </template>
<template #icon> <LucideFolderTree class="-order-1 h-4 w-4" /></template>
diff --git a/resources/js/components/dashboard/DashboardTasks.vue b/resources/js/components/dashboard/DashboardTasks.vue
index 7523aad7..5a7e8c8d 100644
--- a/resources/js/components/dashboard/DashboardTasks.vue
+++ b/resources/js/components/dashboard/DashboardTasks.vue
@@ -2,9 +2,12 @@
import type { TaskStatsResponse } from '@/types/types';
import type { TaskResource } from '@/types/resources';
-import { computed, onMounted, onUnmounted, ref, useTemplateRef } from 'vue';
-import { cancelTask, deleteSubTask, deleteTask, getTasks, getTaskStats } from '@/service/siteAPI';
+import { computed, onMounted, onUnmounted, ref, useTemplateRef, type Ref } from 'vue';
+import { cancelTask, deleteSubTask, deleteTask, getTaskStats } from '@/service/siteAPI';
+import { useDashboardStore } from '@/stores/DashboardStore';
import { handleStartTask } from '@/service/taskService';
+import { useQueryClient } from '@tanstack/vue-query';
+import { storeToRefs } from 'pinia';
import { toast } from '@/service/toaster/toastService';
import ButtonText from '@/components/inputs/ButtonText.vue';
@@ -20,12 +23,9 @@ import ProiconsArrowSync from '~icons/proicons/arrow-sync';
import LucideFolderTree from '~icons/lucide/folder-tree';
import LucideFolderSync from '~icons/lucide/folder-sync';
import ProiconsAdd from '~icons/proicons/add';
+import { subscribeToDaskboardTasks } from '@/service/wsService';
-const cancelModal = useModal({ title: 'Cancel task?', submitText: 'Confim' });
-const deleteModal = useModal({ title: 'Remove task from records?', submitText: 'Confim' });
-
-const searchQuery = ref('');
-const sortingOptions = ref([
+const sortingOptions = [
{
title: 'Add Time',
value: 'created_at',
@@ -61,19 +61,26 @@ const sortingOptions = ref([
value: 'duration',
disabled: false,
},
-]);
+];
-const cachedID = ref<number | null>(null);
-const taskStats = ref<TaskStatsResponse>();
-const tasks = ref<TaskResource[]>([]);
-const isScreenSmall = ref(false);
-const isScreenLarge = ref(false);
+const { stateTasks } = storeToRefs(useDashboardStore()) as { stateTasks: Ref<TaskResource[]> };
+
+const liveUpdate = subscribeToDaskboardTasks();
+const queryClient = useQueryClient();
const taskPopover = useTemplateRef('taskPopover');
+const cancelModal = useModal({ title: 'Cancel task?', submitText: 'Confim' });
+const deleteModal = useModal({ title: 'Remove task from records?', submitText: 'Confim' });
+
+const isScreenSmall = ref(false);
+const isScreenLarge = ref(false);
+const searchQuery = ref('');
+const taskStats = ref<TaskStatsResponse>();
+const cachedID = ref<number | null>(null);
const filteredTasks = computed(() => {
let tempList = searchQuery.value
- ? tasks.value.filter((task: TaskResource) => {
+ ? stateTasks.value.filter((task: TaskResource) => {
{
try {
let strRepresentation = [task.name, task.summary, task.description, task.created_at].join(' ').toLowerCase();
@@ -84,12 +91,12 @@ const filteredTasks = computed(() => {
}
}
})
- : tasks.value;
+ : stateTasks.value;
return tempList;
});
const handleSort = async (column = 'date', dir = 1) => {
- let tempList = tasks.value.sort((taskA: TaskResource, taskB: TaskResource) => {
+ let tempList = [...stateTasks.value].sort((taskA: TaskResource, taskB: TaskResource) => {
if (column === 'created_at' || column === 'started_at' || column === 'ended_at') {
let dateA = new Date(taskA?.[column] ?? '');
let dateB = new Date(taskB?.[column] ?? '');
@@ -101,7 +108,7 @@ const handleSort = async (column = 'date', dir = 1) => {
if (valueA && valueB && typeof valueA === 'number' && typeof valueB === 'number') return (valueA - valueB) * dir;
return `${valueA}`?.localeCompare(`${valueB}`) * dir;
});
- tasks.value = tempList;
+ stateTasks.value = tempList;
return tempList;
};
@@ -166,10 +173,13 @@ const submitSubTaskDelete = async (id: number) => {
const loadData = async () => {
const { data: rawTaskStats } = await getTaskStats();
- taskStats.value = rawTaskStats ?? undefined;
- const { data: rawTasks } = await getTasks();
- tasks.value = rawTasks?.data?.length > 0 ? rawTasks.data : [];
+ taskStats.value = rawTaskStats;
+
+ queryClient.invalidateQueries({ queryKey: ['tasks'] });
+
+ // const { data: rawTasks } = await getTasks();
+ // stateTasks.value = rawTasks?.data?.length > 0 ? rawTasks.data : [];
};
const updateScreenSize = () => {
@@ -179,12 +189,14 @@ const updateScreenSize = () => {
onMounted(() => {
updateScreenSize();
+ // For showing and hiding charts with a v-if instead of css for performance reasons
window.addEventListener('resize', updateScreenSize);
loadData();
});
onUnmounted(() => {
window.removeEventListener('resize', updateScreenSize);
+ if (liveUpdate) liveUpdate.unsubscribe();
});
</script>
@@ -244,7 +256,11 @@ onUnmounted(() => {
<ButtonText
class="h-8 text-rose-600 dark:!bg-rose-700 disabled:opacity-60"
:title="'Scan and Index All Files For Metadata'"
- @click.stop.prevent="handleStartTask('scan')"
+ @click.stop.prevent="
+ handleStartTask('scan').then(() => {
+ taskPopover?.handleClose();
+ })
+ "
>
<template #text> Scan All Files </template>
<template #icon> <LucideFolderTree class="-order-1 h-4 w-4" /></template>
@@ -271,8 +287,8 @@ onUnmounted(() => {
</ButtonText>
</div>
<div class="capitalize text-sm font-medium text-neutral-600 dark:text-neutral-300 flex flex-col gap-1 w-fit text-end">
- <p class="w-fit">Running Tasks: {{ tasks.filter((task) => task.status === 'processing').length }}</p>
- <p class="w-fit">Total Tasks: {{ tasks.length }}</p>
+ <p class="w-fit">Running Tasks: {{ taskStats?.count_running }}</p>
+ <p class="w-fit">Total Tasks: {{ stateTasks.length ?? taskStats?.count_tasks }}</p>
</div>
</div>
<TableBase
diff --git a/resources/js/components/pinesUI/ModalBase.vue b/resources/js/components/pinesUI/ModalBase.vue
index 35e59009..9ab5e5cb 100755
--- a/resources/js/components/pinesUI/ModalBase.vue
+++ b/resources/js/components/pinesUI/ModalBase.vue
@@ -1,4 +1,4 @@
-<script setup>
+<script setup lang="ts">
import { OnClickOutside } from '@vueuse/components';
import { UseFocusTrap } from '@vueuse/integrations/useFocusTrap/component';
import { useAppStore } from '@/stores/AppStore';
@@ -6,35 +6,27 @@ import { ref, watch } from 'vue';
import ButtonCorner from '@/components/inputs/ButtonCorner.vue';
-const props = defineProps({
- modalData: {
- required: true,
+const props = withDefaults(
+ defineProps<{
+ modalData: any;
+ action?: () => void;
+ useControls?: boolean;
+ isProcessing?: boolean;
+ }>(),
+ {
+ action: () => {},
+ useControls: true,
+ isProcessing: false,
},
- action: {
- require: false,
- type: Function,
- default: () => {},
- },
- useControls: {
- type: Boolean,
- required: false,
- default: true,
- },
-
- isProcessing: {
- type: Boolean,
- required: false,
- default: false,
- },
-});
+);
-const processing = ref(false);
const { setScrollLock } = useAppStore();
+const processing = ref(false);
-const submitModal = async (action, modalData) => {
- processing = true;
+const submitModal = async (action: any, modalData: any) => {
+ processing.value = true;
await action();
- processing = false;
+ processing.value = false;
modalData.toggleModal(false);
};
diff --git a/resources/js/service/queries.ts b/resources/js/service/queries.ts
index f6dc00d6..cc1148ec 100644
--- a/resources/js/service/queries.ts
+++ b/resources/js/service/queries.ts
@@ -1,11 +1,11 @@
import type { PulseResponse, SiteAnalyticsResponse } from '@/types/types.ts';
import type { Ref } from 'vue';
-import { getSiteAnalytics, getPulse, getUsers } from '@/service/siteAPI.ts';
+import { getSiteAnalytics, getPulse, getUsers, getTasks } from '@/service/siteAPI.ts';
import { useQuery } from '@tanstack/vue-query';
import mediaAPI, { getCategories } from '@/service/mediaAPI.ts';
-import type { CategoryResource, UserResource } from '@/types/resources';
+import type { CategoryResource, TaskResource, UserResource } from '@/types/resources';
export const useGetVideoTags = () => {
return useQuery({
@@ -68,6 +68,16 @@ export const useGetUsers = () => {
});
};
+export const useGetTasks = () => {
+ return useQuery<{ data: TaskResource[] }>({
+ queryKey: ['tasks'],
+ queryFn: async () => {
+ const { data: response } = await getTasks();
+ return response;
+ },
+ });
+};
+
// export const useGetChatRoomMessagesQuery = (chat_id: number | null) => {
// return useInfiniteQuery({
// queryKey: ['chatroom-messages', chat_id],
diff --git a/resources/js/service/wsService.ts b/resources/js/service/wsService.ts
index 72899a96..61870ca4 100644
--- a/resources/js/service/wsService.ts
+++ b/resources/js/service/wsService.ts
@@ -1,23 +1,33 @@
-import { toast } from './toaster/toastService';
+import { useDashboardStore } from '@/stores/DashboardStore';
+import { toast } from '@/service/toaster/toastService';
+/**
+ * Subscribes to a specific task ID and shows a notification once it ends.
+ *
+ * @param taskId The specific Task ID
+ */
export function subscribeToTask(taskId: number) {
if (isNaN(taskId) || taskId <= 0) return;
window.Echo.private(`tasks.${taskId}`).listen('TaskEnded', (event: any) => {
- console.log(event);
toast.add(`"${event?.task?.name}" ${event?.task?.status}.`, { type: event?.task?.status_key > 0 ? 'success' : 'danger' });
window.Echo.leave(`tasks.${taskId}`);
});
+ return { unsubscribe: () => window.Echo.leave(`tasks.${taskId}`) };
}
-export function subscribeToNotifications() {}
-
export function subscribeToDaskboardTasks() {
window.Echo.private(`dashboard.tasks`).listen('TaskUpdated', (event: any) => {
// console.log(event);
// Update StateTasks??
+ if (!event.task) return;
+
+ const { updateSingleTask } = useDashboardStore();
+ updateSingleTask(event.task);
});
+
+ return { unsubscribe: () => window.Echo.leave('dashboard.tasks') };
}
/**
diff --git a/resources/js/stores/DashboardStore.ts b/resources/js/stores/DashboardStore.ts
index 8de4e7a7..3b68c8b4 100644
--- a/resources/js/stores/DashboardStore.ts
+++ b/resources/js/stores/DashboardStore.ts
@@ -1,12 +1,13 @@
import { type CategoryResource, type TaskResource, type UserResource } from '@/types/resources';
import { ref, watch, type Ref } from 'vue';
-import { useGetCategories, useGetUsers } from '@/service/queries';
+import { useGetCategories, useGetTasks, useGetUsers } from '@/service/queries';
import { defineStore } from 'pinia';
export const useDashboardStore = defineStore('Dashboard', () => {
const { data: rawCategories, isLoading: isLoadingLibraries } = useGetCategories();
const { data: rawUsers, isLoading: isLoadingUsers } = useGetUsers();
+ const { data: rawTasks, isLoading: isLoadingTasks } = useGetTasks();
const stateLibraries = ref<CategoryResource[]>([]);
const stateTasks = ref<TaskResource[]>([]);
@@ -20,17 +21,43 @@ export const useDashboardStore = defineStore('Dashboard', () => {
stateUsers.value = v?.data ?? [];
});
+ watch(rawTasks, (v) => {
+ stateTasks.value = v?.data ?? [];
+ });
+
+ const updateSingleTask = (data: TaskResource) => {
+ if (stateTasks.value.findIndex((task) => task.id === data.id) === -1) {
+ stateTasks.value = [data, ...stateTasks.value];
+ return;
+ }
+ stateTasks.value = stateTasks.value.map((task) => (task.id === data.id ? data : task));
+ };
+
+ const updateSingleLibrary = (data: CategoryResource) => {
+ if (stateLibraries.value.findIndex((lib) => lib.id === data.id) === -1) {
+ stateLibraries.value = [data, ...stateLibraries.value];
+ return;
+ }
+ stateLibraries.value = stateLibraries.value.map((lib) => (lib.id === data.id ? data : lib));
+ };
+
return {
stateLibraries,
stateTasks,
stateUsers,
isLoadingLibraries,
isLoadingUsers,
+ isLoadingTasks,
+ updateSingleTask,
+ updateSingleLibrary,
} as {
stateLibraries: Ref<CategoryResource[]>;
stateTasks: Ref<TaskResource[]>;
stateUsers: Ref<UserResource[]>;
isLoadingLibraries: boolean;
isLoadingUsers: boolean;
+ isLoadingTasks: boolean;
+ updateSingleTask: (data: TaskResource) => void;
+ updateSingleLibrary: (data: CategoryResource) => void;
};
});
diff --git a/resources/js/types/types.ts b/resources/js/types/types.ts
index 41cb925b..ab233185 100644
--- a/resources/js/types/types.ts
+++ b/resources/js/types/types.ts
@@ -123,6 +123,7 @@ export interface TaskStatsResponse {
count_cancelled: number;
avg_count_sub_tasks: number;
count_tasks: number;
+ count_running: number;
count_subtasks: number;
}
diff --git a/routes/api.php b/routes/api.php
index 0bb4ab51..3f5b9f8a 100755
--- a/routes/api.php
+++ b/routes/api.php
@@ -52,6 +52,7 @@ Route::group(['middleware' => ['auth:sanctum']], function () {
Route::post('/verify-folders/{category?}', [DirectoryController::class, 'verifyFolders']);
Route::post('/scan/{category?}', [DirectoryController::class, 'scanFiles']);
Route::post('/clean', [DirectoryController::class, 'cleanPaths']);
+ Route::post('/cancel/{task}', [TasksController::class, 'cancel']);
});
});
diff --git a/routes/channels.php b/routes/channels.php
index a2c13ebe..cbe3a324 100644
--- a/routes/channels.php
+++ b/routes/channels.php
@@ -6,6 +6,10 @@ Broadcast::channel('tasks.{id}', function ($user, $id) {
return (int) $user->id === 1;
});
+Broadcast::channel('tasks.{id}.subtasks', function ($user, $id) {
+ return (int) $user->id === 1;
+});
+
Broadcast::channel('notifications.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});