Skip to content

Commit

Permalink
Add puma webserver over thin
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Aug 14, 2024
1 parent 2b321c6 commit 0389a1b
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 109 deletions.
2 changes: 2 additions & 0 deletions .ruby-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ AllCops:
- vendor/**/*
- ".git/**/*"
- bin/bundle
- bin/puma
- bin/pumactl
- bin/rubocop
- bin/sidekiq
- bin/sidekiqmon
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ gem 'maxmind-db', '~> 1.2.0'
gem 'mahoro', '~> 0.5'
gem 'nokogiri', '~> 1.16.7'
gem 'open4', '~> 1.3.0'
gem 'puma', '~> 6.4.2'
gem 'rack', '~> 2.2.9'
gem 'rack-utf8_sanitizer', '~> 1.9.1'
gem 'recaptcha', '~> 5.17.0', require: 'recaptcha/rails'
Expand All @@ -128,7 +129,6 @@ gem 'strip_attributes', git: 'https://github.com/mysociety/strip_attributes.git'
gem 'stripe', '~> 5.55.0'
gem 'syck', '~> 1.4.1', require: false
gem 'syslog_protocol', '~> 0.9.0'
gem 'thin', '~> 1.8.2'
gem 'vpim', '~> 24.2.20'
gem 'will_paginate', '~> 4.0.1'
gem 'xapian-full-alaveteli', '~> 1.4.22.1'
Expand Down
10 changes: 3 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ GEM
bigdecimal
rexml
crass (1.0.6)
daemons (1.4.1)
dalli (3.2.8)
dante (0.2.0)
date (3.3.4)
Expand All @@ -210,7 +209,6 @@ GEM
rake (>= 12.0.0, < 14.0.0)
docile (1.4.0)
erubi (1.13.0)
eventmachine (1.2.7)
exception_notification (4.5.0)
actionmailer (>= 5.2, < 8)
activesupport (>= 5.2, < 8)
Expand Down Expand Up @@ -392,6 +390,8 @@ GEM
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (6.0.0)
puma (6.4.2)
nio4r (~> 2.0)
racc (1.8.1)
rack (2.2.9)
rack-test (2.1.0)
Expand Down Expand Up @@ -539,10 +539,6 @@ GEM
syck (1.4.1)
syslog_protocol (0.9.2)
text (1.3.1)
thin (1.8.2)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
thor (1.3.1)
tilt (2.0.10)
timeout (0.4.1)
Expand Down Expand Up @@ -649,6 +645,7 @@ DEPENDENCIES
open4 (~> 1.3.0)
pg (~> 1.5.7)
pry (~> 0.14.2)
puma (~> 6.4.2)
rack (~> 2.2.9)
rack-utf8_sanitizer (~> 1.9.1)
rails (~> 7.0.8)
Expand Down Expand Up @@ -679,7 +676,6 @@ DEPENDENCIES
stripe-ruby-mock!
syck (~> 1.4.1)
syslog_protocol (~> 0.9.0)
thin (~> 1.8.2)
turbo-rails (~> 2.0.6)
uglifier (~> 4.2.0)
unicode (~> 0.4.4)
Expand Down
27 changes: 27 additions & 0 deletions bin/puma
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'puma' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("puma", "puma")
27 changes: 27 additions & 0 deletions bin/pumactl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'pumactl' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("puma", "pumactl")
16 changes: 0 additions & 16 deletions bin/thin

This file was deleted.

43 changes: 43 additions & 0 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'concurrent-ruby'

require File.expand_path('load_env.rb', __dir__)

# This configuration file will be evaluated by Puma. The top-level methods that
# are invoked here are part of Puma's configuration DSL. For more information
# about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html.

# 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
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
max_threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count }
threads min_threads_count, max_threads_count

# Specifies that the worker count should equal the number of processors in
# production.
if ENV['RAILS_ENV'] == 'production'
concurrency = ENV.fetch('WEB_CONCURRENCY') do
Concurrent.physical_processor_count
end
worker_count = Integer(concurrency)
workers worker_count if worker_count > 1
end

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
worker_timeout 3600 if ENV.fetch('RAILS_ENV', 'development') == 'development'

# Specifies the `port` that Puma will listen on to receive requests; default is
# 3000.
port ENV.fetch('PORT') { 3000 }

# Specifies the `environment` that Puma will run in.
environment ENV.fetch('RAILS_ENV') { 'development' }

# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch('PIDFILE') { 'tmp/pids/server.pid' }

# Allow puma to be restarted by `bin/rails restart` command.
plugin :tmp_restart
23 changes: 23 additions & 0 deletions config/puma.service.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=<%= daemon_name %>
After=network.target

[Service]
Type=notify

WatchdogSec=10

User=<%= user %>
Group=<%= user %>
UMask=0002

WorkingDirectory=<%= vhost_dir %>/<%= vcspath %>

ExecStart=/bin/bash -lc 'bundle exec puma -C config/puma.rb'
ExecStop=/bin/bash -lc 'bundle exec pumactl stop -F config/puma.rb'

RestartSec=1
Restart=always

[Install]
WantedBy=multi-user.target
82 changes: 0 additions & 82 deletions config/sysvinit-thin.example

This file was deleted.

6 changes: 3 additions & 3 deletions lib/tasks/config_files.rake
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ namespace :config_files do
def daemons
[
{
path: '/etc/init.d',
name: 'thin',
template: 'config/sysvinit-thin.example',
path: '/etc/systemd/system',
name: 'puma.service',
template: 'config/puma.service.example',
condition: -> { ENV['RAILS_ENV'] == 'production' }
},
{
Expand Down
Empty file added tmp/pids/.keep
Empty file.

0 comments on commit 0389a1b

Please sign in to comment.