Skip to content

Commit

Permalink
Merge pull request #374 from blackcandy-org/media-syncing
Browse files Browse the repository at this point in the history
Prevent inconsistent media syncing
  • Loading branch information
aidewoode authored May 6, 2024
2 parents 890d726 + 167f79c commit 0f1d352
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
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

0 comments on commit 0f1d352

Please sign in to comment.