From 4aa67ce02d671f52713e9413cdaf49f8741bc7af Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 10 May 2025 20:54:59 +0200 Subject: [PATCH] Move current_image caching to plugins --- app/Jobs/CommonFunctions.php | 6 +++--- ...ratePlaylistItemJob.php => GeneratePluginJob.php} | 10 +++++----- app/Models/PlaylistItem.php | 1 - app/Models/Plugin.php | 1 + ...he.php => 2025_05_10_182724_add_plugin_cache.php} | 6 +++--- routes/api.php | 12 ++++++------ 6 files changed, 18 insertions(+), 18 deletions(-) rename app/Jobs/{GeneratePlaylistItemJob.php => GeneratePluginJob.php} (66%) rename database/migrations/{2025_05_01_083230_add_playlist_item_cache.php => 2025_05_10_182724_add_plugin_cache.php} (76%) diff --git a/app/Jobs/CommonFunctions.php b/app/Jobs/CommonFunctions.php index 9362b76..f8ede1b 100644 --- a/app/Jobs/CommonFunctions.php +++ b/app/Jobs/CommonFunctions.php @@ -3,7 +3,7 @@ namespace App\Jobs; use App\Models\Device; -use App\Models\PlaylistItem; +use App\Models\Plugin; use Illuminate\Support\Facades\Storage; use Ramsey\Uuid\Uuid; use Spatie\Browsershot\Browsershot; @@ -62,8 +62,8 @@ class CommonFunctions public static function cleanupFolder(): void { $activeDeviceImageUuids = Device::pluck('current_screen_image')->filter()->toArray(); - $activePlaylistImageUuids = PlaylistItem::pluck('current_image')->filter()->toArray(); - $activeImageUuids = array_merge($activeDeviceImageUuids, $activePlaylistImageUuids); + $activePluginImageUuids = Plugin::pluck('current_image')->filter()->toArray(); + $activeImageUuids = array_merge($activeDeviceImageUuids, $activePluginImageUuids); $files = Storage::disk('public')->files('/images/generated/'); foreach ($files as $file) { diff --git a/app/Jobs/GeneratePlaylistItemJob.php b/app/Jobs/GeneratePluginJob.php similarity index 66% rename from app/Jobs/GeneratePlaylistItemJob.php rename to app/Jobs/GeneratePluginJob.php index 374f95d..d1e254b 100644 --- a/app/Jobs/GeneratePlaylistItemJob.php +++ b/app/Jobs/GeneratePluginJob.php @@ -2,14 +2,14 @@ namespace App\Jobs; -use App\Models\PlaylistItem; +use App\Models\Plugin; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -class GeneratePlaylistItemJob implements ShouldQueue +class GeneratePluginJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -17,7 +17,7 @@ class GeneratePlaylistItemJob implements ShouldQueue * Create a new job instance. */ public function __construct( - private readonly int $playlistItemId, + private readonly int $pluginId, private readonly string $markup ) {} @@ -28,8 +28,8 @@ class GeneratePlaylistItemJob implements ShouldQueue { $newImageUuid = CommonFunctions::generateImage($this->markup); - PlaylistItem::find($this->playlistItemId)->update(['current_image' => $newImageUuid]); - \Log::info("Playlist item $this->playlistItemId: updated with new image: $newImageUuid"); + Plugin::find($this->pluginId)->update(['current_image' => $newImageUuid]); + \Log::info("Plugin $this->pluginId: updated with new image: $newImageUuid"); CommonFunctions::cleanupFolder(); } diff --git a/app/Models/PlaylistItem.php b/app/Models/PlaylistItem.php index 88fb65a..4eba877 100644 --- a/app/Models/PlaylistItem.php +++ b/app/Models/PlaylistItem.php @@ -15,7 +15,6 @@ class PlaylistItem extends Model protected $casts = [ 'is_active' => 'boolean', 'last_displayed_at' => 'datetime', - 'current_image' => 'string', ]; public function playlist(): BelongsTo diff --git a/app/Models/Plugin.php b/app/Models/Plugin.php index fa5dbd6..63ac7ba 100644 --- a/app/Models/Plugin.php +++ b/app/Models/Plugin.php @@ -17,6 +17,7 @@ class Plugin extends Model 'data_payload' => 'json', 'data_payload_updated_at' => 'datetime', 'is_native' => 'boolean', + 'current_image' => 'string', ]; protected static function boot() diff --git a/database/migrations/2025_05_01_083230_add_playlist_item_cache.php b/database/migrations/2025_05_10_182724_add_plugin_cache.php similarity index 76% rename from database/migrations/2025_05_01_083230_add_playlist_item_cache.php rename to database/migrations/2025_05_10_182724_add_plugin_cache.php index 1a58e7f..a24f436 100644 --- a/database/migrations/2025_05_01_083230_add_playlist_item_cache.php +++ b/database/migrations/2025_05_10_182724_add_plugin_cache.php @@ -11,8 +11,8 @@ return new class extends Migration */ public function up(): void { - Schema::table('playlist_items', function (Blueprint $table) { - $table->string('current_image')->nullable()->after('is_active'); + Schema::table('plugins', function (Blueprint $table) { + $table->string('current_image')->nullable()->after('data_payload_updated_at'); }); } @@ -21,7 +21,7 @@ return new class extends Migration */ public function down(): void { - Schema::table('playlist_items', function (Blueprint $table) { + Schema::table('plugins', function (Blueprint $table) { $table->dropColumn('current_image'); }); } diff --git a/routes/api.php b/routes/api.php index 4aabc4d..b8bdd34 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,7 +1,7 @@ plugin; // Check and update stale data if needed - if ($plugin->isDataStale() || $nextPlaylistItem->last_displayed_at == null) { + if ($plugin->isDataStale() || $plugin->current_image == null) { $plugin->updateDataPayload(); if ($plugin->render_markup) { @@ -64,16 +64,16 @@ Route::get('/display', function (Request $request) { $markup = view($plugin->render_markup_view, ['data' => $plugin->data_payload])->render(); } - GeneratePlaylistItemJob::dispatchSync($nextPlaylistItem->id, $markup); + GeneratePluginJob::dispatchSync($plugin->id, $markup); } } - $nextPlaylistItem->refresh(); + $plugin->refresh(); - if ($nextPlaylistItem->current_image != null) + if ($plugin->current_image != null) { $nextPlaylistItem->update(['last_displayed_at' => now()]); - $device->update(['current_screen_image' => $nextPlaylistItem->current_image]); + $device->update(['current_screen_image' => $plugin->current_image]); } }