-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #389 from blackcandy-org/parallel-sync
Parallel media sync
- Loading branch information
Showing
32 changed files
with
546 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class MediaSyncAllJob < MediaSyncJob | ||
def perform(dir = Setting.media_path) | ||
file_paths = MediaFile.file_paths(dir) | ||
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) | ||
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 | ||
|
||
private | ||
|
||
def after_sync(_) | ||
Media.instance.broadcast_render_to "media_sync", partial: "media_syncing/syncing", locals: {syncing: false} | ||
super(fetch_external_metadata: true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
class MediaSyncJob < ApplicationJob | ||
include BlackCandy::Configurable | ||
|
||
has_config :parallel_processor_count, default: Parallel.processor_count, env_prefix: "media_sync" | ||
|
||
queue_as :critical | ||
|
||
# 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 | ||
Media.sync(type, file_paths) | ||
before_perform :before_sync | ||
|
||
after_perform do |job| | ||
sync_type = job.arguments.first | ||
after_sync(fetch_external_metadata: sync_type != :removed) | ||
end | ||
|
||
def perform(type, file_paths = []) | ||
parallel_sync(type, file_paths) | ||
end | ||
|
||
def self.parallel_processor_count | ||
return 0 unless Setting.enable_parallel_media_sync? | ||
config.parallel_processor_count | ||
end | ||
|
||
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 | ||
|
||
def after_sync(fetch_external_metadata: true) | ||
Media.syncing = false | ||
Media.fetch_external_metadata if fetch_external_metadata | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.