Skip to content

Commit

Permalink
Tweak some options on media listener
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Dec 20, 2023
1 parent b09bd97 commit 689f06f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/models/media.rb
Expand Up @@ -8,6 +8,8 @@ class Media
class << self
def sync_all(dir = Setting.media_path)
sync(:all, MediaFile.file_paths(dir))
ensure
instance.broadcast_render_to "media_sync", partial: "media_syncing/syncing", locals: {syncing: false}
end

def sync(type, file_paths = [])
Expand Down Expand Up @@ -37,9 +39,7 @@ def syncing?

def syncing=(is_syncing)
return if is_syncing == syncing?

Rails.cache.write("media_syncing", is_syncing, expires_in: 1.hour)
instance.broadcast_render_to "media_sync", partial: "media_syncing/syncing", locals: {syncing: syncing?}
end

private
Expand Down
60 changes: 43 additions & 17 deletions app/models/media_listener.rb
@@ -1,31 +1,57 @@
# frozen_string_literal: true

class MediaListener
SERVICE_NAME = "media_listener_service"
PID_DIR = File.join(Rails.root, "tmp", "pids")
include Singleton

SERVICE_PATH = File.join(Rails.root, "lib", "daemons", "media_listener_service")
class Config
DEFAULTS = {
service_name: "media_listener_service",
pid_dir: File.join(Rails.root, "tmp", "pids")
}

class << self
def start
run("start")
end
attr_accessor :service_name, :pid_dir

def stop
run("stop")
def initialize
DEFAULTS.each do |key, value|
send("#{key}=", value)
end
end
end

def running?
pid_file_path = Daemons::PidFile.find_files(PID_DIR, SERVICE_NAME).first
return false unless pid_file_path.present?
class << self
delegate :start, :stop, :running?, to: :instance

pid_file = Daemons::PidFile.existing(pid_file_path)
Daemons::Pid.running?(pid_file.pid)
def config
yield instance.config
end
end

private
attr_reader :config

def run(command)
system "bundle exec #{SERVICE_PATH} #{command} -d #{PID_DIR} -n #{SERVICE_NAME}"
end
def initialize
@config = Config.new
end

def start
run("start")
end

def stop
run("stop")
end

def running?
pid_file_path = Daemons::PidFile.find_files(@config.pid_dir, @config.service_name).first
return false unless pid_file_path.present?

pid_file = Daemons::PidFile.existing(pid_file_path)
Daemons::Pid.running?(pid_file.pid)
end

private

def run(command)
system "bundle exec #{SERVICE_PATH} #{command} -d #{@config.pid_dir} -n #{@config.service_name}"
end
end
2 changes: 1 addition & 1 deletion app/views/media_syncing/_syncing.turbo_stream.erb
@@ -1,4 +1,4 @@
<%= turbo_stream.replace "turbo-media-syncing-button", partial: "media_syncing/button", locals: { syncing: syncing} %>
<%= turbo_stream.replace "turbo-media-syncing-button", partial: "media_syncing/button", locals: {syncing: syncing} %>
<% unless syncing %>
<%= render_flash(message: t("text.sync_completed")) %>
Expand Down
2 changes: 1 addition & 1 deletion lib/daemons/media_listener_service
Expand Up @@ -35,7 +35,7 @@ Daemons.run_proc(options[:name], daemon_options) do

supported_formates = MediaFile::SUPPORTED_FORMATS.map { |formate| %r{\.#{formate}$} }

listener = Listen.to(File.expand_path(Setting.media_path), only: supported_formates) do |modified, added, removed|
listener = Listen.to(File.expand_path(Setting.media_path), only: supported_formates, latency: 5, wait_for_delay: 10) do |modified, added, removed|
MediaSyncJob.perform_later(:modified, modified) if modified.present?
MediaSyncJob.perform_later(:added, added) if added.present?
MediaSyncJob.perform_later(:removed, removed) if removed.present?
Expand Down
4 changes: 2 additions & 2 deletions test/models/media_test.rb
Expand Up @@ -111,9 +111,9 @@ class MediaTest < ActiveSupport::TestCase
assert_equal Media.instance.object_id, Media.instance.object_id
end

test "should broadcast media sync stream when set syncing status" do
test "should broadcast media sync stream when sync all completed" do
assert_broadcasts("media_sync", 1) do
Media.syncing = true
Media.sync_all
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Expand Up @@ -33,6 +33,10 @@

WebMock.disable_net_connect!(allow_localhost: true, net_http_connect_on_start: true, allow: allowed_sites_for_webmock)

MediaListener.config do |config|
config.service_name = "media_listener_service_test"
end

class ActiveSupport::TestCase
include Turbo::Broadcastable::TestHelper
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
Expand Down

0 comments on commit 689f06f

Please sign in to comment.