Skip to content

Commit

Permalink
Fix parallel sync can not clear records when files was deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Jul 11, 2024
1 parent 5ced149 commit b927970
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
13 changes: 2 additions & 11 deletions app/jobs/media_sync_all_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@

class MediaSyncAllJob < MediaSyncJob
def perform(dir = Setting.media_path)
parallel_processor_count = self.class.parallel_processor_count

file_paths = MediaFile.file_paths(dir)
file_md5_hashes = Parallel.map(file_paths, in_processes: parallel_processor_count) do |file_path|
file_md5_hashes = Parallel.map(file_paths, in_processes: self.class.parallel_processor_count) do |file_path|
MediaFile.get_md5_hash(file_path, with_mtime: true)
end

existing_songs = Song.where(md5_hash: file_md5_hashes)
added_file_paths = file_paths - existing_songs.pluck(:file_path)

return if added_file_paths.blank?

grouped_file_paths = (parallel_processor_count > 0) ? added_file_paths.in_groups(parallel_processor_count, false).compact_blank : [added_file_paths]

added_song_hashes = Parallel.map(grouped_file_paths, in_processes: parallel_processor_count) do |paths|
Media.sync(:added, paths)
end.flatten.compact
added_song_hashes = added_file_paths.blank? ? [] : parallel_sync(:added, added_file_paths).flatten.compact

Media.clean_up(added_song_hashes + existing_songs.pluck(:md5_hash))
end
Expand Down
17 changes: 10 additions & 7 deletions app/jobs/media_sync_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ class MediaSyncJob < ApplicationJob
end

def perform(type, file_paths = [])
parallel_processor_count = self.class.parallel_processor_count

grouped_file_paths = (parallel_processor_count > 0) ? file_paths.in_groups(parallel_processor_count, false).compact_blank : [file_paths]

Parallel.each grouped_file_paths, in_processes: parallel_processor_count do |paths|
Media.sync(type, paths)
end
parallel_sync(type, file_paths)
end

def self.parallel_processor_count
Expand All @@ -34,6 +28,15 @@ def self.parallel_processor_count

private

def parallel_sync(type, file_paths)
parallel_processor_count = self.class.parallel_processor_count
grouped_file_paths = (parallel_processor_count > 0) ? file_paths.in_groups(parallel_processor_count, false).compact_blank : [file_paths]

Parallel.each grouped_file_paths, in_processes: parallel_processor_count do |paths|
Media.sync(type, paths)
end
end

def before_sync
Media.syncing = true
end
Expand Down
4 changes: 3 additions & 1 deletion test/jobs/media_sync_all_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ class MediaSyncAllJobTest < ActiveJob::TestCase
end

test "should clear records on database when delete file" do
clear_media_data

create_tmp_dir(from: Setting.media_path) do |tmp_dir|
MediaSyncAllJob.perform_now(tmp_dir)
File.delete File.join(tmp_dir, "artist2_album3.ogg")
File.delete File.join(tmp_dir, "artist2_album3.wav")
File.delete File.join(tmp_dir, "artist2_album3.opus")
File.delete File.join(tmp_dir, "artist2_album3.oga")
File.delete File.join(tmp_dir, "artist2_album3.wma")

MediaSyncAllJob.perform_now(tmp_dir)

assert_nil Song.find_by(name: "ogg_sample")
assert_nil Song.find_by(name: "wav_sample")
assert_nil Song.find_by(name: "opus_sample")
Expand Down

0 comments on commit b927970

Please sign in to comment.