Skip to content

Commit

Permalink
Merge 26c3201 into d88b086
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Apr 24, 2024
2 parents d88b086 + 26c3201 commit 9cf4df8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
web: env RUBY_DEBUG_OPEN=true bin/rails server
web: bin/rails server
js: npm run build-dev -- --watch
css: npm run build-dev:css -- --watch
4 changes: 1 addition & 3 deletions app/models/media_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def file_info(file_path)
end

def get_md5_hash(file_path, with_mtime: false)
return unless File.exist?(file_path)

string = "#{file_path}#{File.mtime(file_path) if with_mtime}"
string = "#{file_path}#{File.mtime(file_path) if with_mtime && File.exist?(file_path)}"
Digest::MD5.base64digest(string)
end

Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Application < Rails::Application
# Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_lib(ignore: %w[assets tasks black_candy])
config.autoload_lib(ignore: %w[assets tasks black_candy puma])

config.exceptions_app = routes

Expand Down
32 changes: 7 additions & 25 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../lib/black_candy/config"

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
Expand All @@ -25,34 +23,18 @@
# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

plugin :solid_queue

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
preload_app!

on_booted do
MediaListener.start if Setting.enable_media_listener? && !MediaListener.running?
end

if ENV["RAILS_ENV"] == "production"
# Specifies that the worker count should equal the number of processors in production.
require "concurrent-ruby"
worker_count = Integer(ENV.fetch("WEB_CONCURRENCY") { Concurrent.physical_processor_count })
workers worker_count if worker_count > 1

on_worker_shutdown do
MediaListener.stop if MediaListener.running?
end
preload_app!
end

if ENV["RAILS_ENV"] == "development"
on_thread_exit do
MediaListener.stop if MediaListener.running?
end
end
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

plugin :solid_queue

plugin :media_listener
3 changes: 2 additions & 1 deletion config/solid_queue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ default: &default
concurrency_maintenance_interval: 30
workers:
- queues: [critical, high, default, low]
threads: 5
# 2 more threads are used by worker and heartbeat task in solid queue
threads: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } - 2 %>
processes: 1
polling_interval: 0.1

Expand Down
11 changes: 11 additions & 0 deletions lib/puma/plugin/media_listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "puma/plugin"

Puma::Plugin.create do
def start(launcher)
launcher.events.on_booted { MediaListener.start if Setting.enable_media_listener? }
launcher.events.on_stopped { MediaListener.stop }
launcher.events.on_restart { MediaListener.stop }
end
end
26 changes: 17 additions & 9 deletions test/models/media_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,26 @@ class MediaTest < ActiveSupport::TestCase
end

test "should remove records when selectively removed files" do
Media.sync(:removed, [file_fixture("artist1_album2.mp3"), file_fixture("artist1_album1.flac")])
create_tmp_dir(from: Setting.media_path) do |tmp_dir|
Media.sync_all(tmp_dir)

assert_nil Song.find_by(name: "mp3_sample")
assert_nil Song.find_by(name: "flac_sample")
assert_nil Album.find_by(name: "album2")
selected_files = [File.join(tmp_dir, "artist1_album2.mp3"), File.join(tmp_dir, "artist1_album1.flac")]
selected_files.each { |file_path| File.delete file_path }
Media.sync(:removed, selected_files)

Media.sync(:removed, [file_fixture("artist1_album1.m4a"), file_fixture("various_artists.mp3")])
assert_nil Song.find_by(name: "mp3_sample")
assert_nil Song.find_by(name: "flac_sample")
assert_nil Album.find_by(name: "album2")

assert_nil Song.find_by(name: "various_artists_sample")
assert_nil Song.find_by(name: "m4a_sample")
assert_nil Album.find_by(name: "album1")
assert_nil Artist.find_by(name: "artist1")
selected_files = [File.join(tmp_dir, "artist1_album1.m4a"), File.join(tmp_dir, "various_artists.mp3")]
selected_files.each { |file_path| File.delete file_path }
Media.sync(:removed, selected_files)

assert_nil Song.find_by(name: "various_artists_sample")
assert_nil Song.find_by(name: "m4a_sample")
assert_nil Album.find_by(name: "album1")
assert_nil Artist.find_by(name: "artist1")
end
end

test "should change associations when selectively modified album info on file" do
Expand Down

0 comments on commit 9cf4df8

Please sign in to comment.