Skip to content

Commit

Permalink
Use puma hook to start and stop media listener service automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Dec 19, 2023
1 parent 87ebac7 commit c042fc9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ gem "cssbundling-rails", "~> 1.2.0"
gem "jsbundling-rails", "~> 1.1.2"

# Use Puma as the app server
gem "puma", "~> 6.3.0"
gem "puma", "~> 6.4.0"

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem "jbuilder", "~> 2.11.5"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ GEM
psych (5.1.1.1)
stringio
public_suffix (5.0.3)
puma (6.3.1)
puma (6.4.0)
nio4r (~> 2.0)
racc (1.7.1)
rack (3.0.8)
Expand Down Expand Up @@ -415,7 +415,7 @@ DEPENDENCIES
pagy (~> 6.0.0)
pg (~> 1.3.2)
propshaft (~> 0.7.0)
puma (~> 6.3.0)
puma (~> 6.4.0)
rails (~> 7.1.0)
ransack (~> 4.1.0)
redis (~> 4.0)
Expand Down
7 changes: 0 additions & 7 deletions config/initializers/media_listener.rb

This file was deleted.

28 changes: 21 additions & 7 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

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

# 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
Expand All @@ -40,6 +33,27 @@
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

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
end

if ENV["RAILS_ENV"] == "development"
on_thread_exit do
MediaListener.stop if MediaListener.running?
end
end

if BlackCandy::Config.embedded_sidekiq?
# Preloading the application is necessary to ensure
# the configuration in your initializer runs before
Expand Down

0 comments on commit c042fc9

Please sign in to comment.