From 167f79cfa84c592c735e35d651949134f22f2f3e Mon Sep 17 00:00:00 2001 From: aidewoode Date: Mon, 6 May 2024 16:11:38 +0800 Subject: [PATCH] Prevent inconsistent media syncing --- app/jobs/media_sync_job.rb | 5 +++-- app/models/media.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/jobs/media_sync_job.rb b/app/jobs/media_sync_job.rb index 93a43559..c906df6b 100644 --- a/app/jobs/media_sync_job.rb +++ b/app/jobs/media_sync_job.rb @@ -1,9 +1,10 @@ class MediaSyncJob < ApplicationJob queue_as :critical - def perform(type = :all, file_paths = []) - return if Media.syncing? + # Limits the concurrency to 1 to prevent inconsistent media syncing data. + limits_concurrency to: 1, key: :media_sync + def perform(type = :all, file_paths = []) if type == :all Media.sync_all else diff --git a/app/models/media.rb b/app/models/media.rb index 023b2507..3b699f5c 100644 --- a/app/models/media.rb +++ b/app/models/media.rb @@ -72,7 +72,7 @@ def attach(file_info) various_artist = Artist.find_or_create_by!(various: true) if various_artist?(file_info) album = Album.find_or_initialize_by( - artist: various_artist || artist, + artist_id: various_artist&.id || artist.id, name: file_info[:album_name] || Album::UNKNOWN_NAME ) @@ -83,7 +83,7 @@ def attach(file_info) end song = Song.find_or_initialize_by(md5_hash: file_info[:md5_hash]) - song.update!(song_info(file_info).merge(album: album, artist: artist)) + song.update!(song_info(file_info).merge(album_id: album.id, artist_id: artist.id)) end def song_info(file_info)