Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent inconsistent media syncing #374

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/jobs/media_sync_job.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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)
Expand Down
Loading