|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -# Puma configuration file for ProStaff API |
| 3 | +# ================================ |
| 4 | +# Puma Configuration - ProStaff API |
| 5 | +# Optimized for Docker / Coolify |
| 6 | +# ================================ |
4 | 7 |
|
5 | | -# Bind to 0.0.0.0 to allow external connections (required for Docker/Railway) |
6 | | -# Note: Use bind instead of port to avoid double-binding issues |
| 8 | +# Bind obrigatório para container |
7 | 9 | bind "tcp://0.0.0.0:#{ENV.fetch('PORT', 3000)}" |
8 | 10 |
|
9 | | -# Specifies the `environment` that Puma will run in. |
10 | | -# If PORT is set (common in production environments), default to production |
11 | | -default_env = ENV['PORT'] ? 'production' : 'development' |
12 | | -environment ENV.fetch('RAILS_ENV', default_env) |
| 11 | +# Ambiente |
| 12 | +environment ENV.fetch('RAILS_ENV', 'production') |
13 | 13 |
|
14 | | -# Specifies the `pidfile` that Puma will use. |
| 14 | +# PID (necessário para evitar erro de container restart) |
15 | 15 | pidfile ENV.fetch('PIDFILE', 'tmp/pids/server.pid') |
16 | 16 |
|
17 | | -# Specifies the number of `workers` to boot in clustered mode. |
18 | | -# Workers are forked web server processes. If using threads and workers together |
19 | | -# the concurrency of the application would be max `threads` * `workers`. |
20 | | -# Workers do not work on JRuby or Windows (both of which do not support |
21 | | -# processes). |
22 | | -workers ENV.fetch('WEB_CONCURRENCY', 2) |
23 | | - |
24 | | -# Use the `preload_app!` method when specifying a `workers` number. |
25 | | -# This directive tells Puma to first boot the application and load code |
26 | | -# before forking the application. This takes advantage of Copy On Write |
27 | | -# process behavior so workers use less memory. |
| 17 | +# Threads |
| 18 | +max_threads = ENV.fetch('RAILS_MAX_THREADS', 5).to_i |
| 19 | +min_threads = ENV.fetch('RAILS_MIN_THREADS', max_threads).to_i |
| 20 | +threads min_threads, max_threads |
| 21 | + |
| 22 | +# Workers (IMPORTANTE para container pequeno) |
| 23 | +# Se não definir WEB_CONCURRENCY, usa 2 |
| 24 | +workers ENV.fetch('WEB_CONCURRENCY', 2).to_i |
| 25 | + |
| 26 | +# Preload melhora uso de memória |
28 | 27 | preload_app! |
29 | 28 |
|
30 | | -# Allow puma to be restarted by `rails restart` command. |
31 | 29 | plugin :tmp_restart |
32 | 30 |
|
33 | | -# Specifies the number of `threads` to use per worker. |
34 | | -# This controls how many threads Puma will use to process requests. |
35 | | -# The default is set to 5 threads as a decent default for most Ruby/Rails apps. |
36 | | -max_threads_count = ENV.fetch('RAILS_MAX_THREADS', 5) |
37 | | -min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count } |
38 | | -threads min_threads_count, max_threads_count |
39 | | - |
40 | | -# === Production Optimizations === |
41 | | -if %w[production staging].include?(ENV['RAILS_ENV']) |
42 | | - # Increase workers in production |
43 | | - workers ENV.fetch('WEB_CONCURRENCY', 4) |
44 | | - |
45 | | - # Bind to socket for better nginx integration (optional) |
46 | | - # bind "unix://#{ENV.fetch('APP_ROOT', Dir.pwd)}/tmp/sockets/puma.sock" |
47 | | - |
48 | | - # Logging |
49 | | - stdout_redirect( |
50 | | - ENV.fetch('PUMA_STDOUT_LOG') { "#{Dir.pwd}/log/puma_access.log" }, |
51 | | - ENV.fetch('PUMA_STDERR_LOG') { "#{Dir.pwd}/log/puma_error.log" }, |
52 | | - true |
53 | | - ) |
54 | | - |
55 | | - # Worker timeout (seconds) |
56 | | - # Kill workers if they hang for more than this time |
57 | | - worker_timeout ENV.fetch('PUMA_WORKER_TIMEOUT', 60).to_i |
| 31 | +# ================================ |
| 32 | +# Production Settings |
| 33 | +# ================================ |
| 34 | +if ENV['RAILS_ENV'] == 'production' |
58 | 35 |
|
59 | | - # Worker boot timeout |
| 36 | + # Timeout de worker |
| 37 | + worker_timeout ENV.fetch('PUMA_WORKER_TIMEOUT', 60).to_i |
60 | 38 | worker_boot_timeout ENV.fetch('PUMA_WORKER_BOOT_TIMEOUT', 60).to_i |
61 | | - |
62 | | - # Worker shutdown timeout |
63 | 39 | worker_shutdown_timeout ENV.fetch('PUMA_WORKER_SHUTDOWN_TIMEOUT', 30).to_i |
64 | 40 |
|
65 | | - # === Phased Restart (Zero Downtime Deploys) === |
66 | | - # This allows Puma to restart workers one at a time |
67 | | - # instead of all at once during a restart |
68 | | - # Use: pumactl phased-restart |
69 | | - on_worker_boot do |
70 | | - # Worker specific setup for Rails |
71 | | - # This is needed for preload_app to work with ActiveRecord |
72 | | - ActiveRecord::Base.establish_connection if defined?(ActiveRecord) |
| 41 | + # Nakayoshi Fork (Puma 7+) |
| 42 | + if respond_to?(:nakayoshi_fork) && ENV.fetch('PUMA_NAKAYOSHI_FORK', 'true') == 'true' |
| 43 | + nakayoshi_fork |
73 | 44 | end |
74 | 45 |
|
| 46 | + # ActiveRecord fix para preload |
75 | 47 | before_fork do |
76 | | - # Disconnect from database before forking |
77 | 48 | ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord) |
78 | 49 | end |
79 | 50 |
|
80 | | - # === Nakayoshi Fork (Memory Optimization) === |
81 | | - # This reduces memory usage by running GC before forking |
82 | | - # Note: nakayoshi_fork is only available in Puma 7+ |
83 | | - if respond_to?(:nakayoshi_fork) && ENV.fetch('PUMA_NAKAYOSHI_FORK', 'true') == 'true' |
84 | | - nakayoshi_fork |
| 51 | + on_worker_boot do |
| 52 | + ActiveRecord::Base.establish_connection if defined?(ActiveRecord) |
85 | 53 | end |
86 | 54 |
|
87 | | - # === Low Level Configuration === |
88 | | - # Note: Some methods like backlog and tcp_nopush are only available in Puma 7+ |
89 | | - # Commenting out for Puma 6 compatibility |
90 | | - |
91 | | - # Configure the queue for accepting connections (Puma 7+ only) |
92 | | - # backlog ENV.fetch('PUMA_BACKLOG', 1024).to_i if respond_to?(:backlog) |
93 | | - |
94 | | - # Set the TCP_CORK and TCP_NODELAY options (Puma 7+ only) |
95 | | - # tcp_nopush true if respond_to?(:tcp_nopush) && ENV.fetch('PUMA_TCP_NOPUSH', 'true') == 'true' |
96 | | - |
97 | | - # === Monitoring === |
98 | | - # Activate control/status app |
99 | | - # Allows you to query Puma for stats and control it |
100 | | - # Access via: pumactl stats -C unix://#{Dir.pwd}/tmp/sockets/pumactl.sock |
101 | | - activate_control_app "unix://#{Dir.pwd}/tmp/sockets/pumactl.sock", { no_token: true } |
| 55 | + # Removido activate_control_app |
102 | 56 | end |
103 | 57 |
|
104 | | -# === Development Optimizations === |
| 58 | +# ================================ |
| 59 | +# Development |
| 60 | +# ================================ |
105 | 61 | if ENV['RAILS_ENV'] == 'development' |
106 | | - # Use fewer workers in development |
107 | 62 | workers 0 |
108 | | - |
109 | | - # Verbose logging in development |
110 | 63 | debug true if ENV.fetch('PUMA_DEBUG', 'false') == 'true' |
111 | 64 | end |
112 | 65 |
|
113 | | -# === Callbacks === |
| 66 | +# ================================ |
| 67 | +# Logs (container-friendly) |
| 68 | +# ================================ |
| 69 | +# Melhor prática para Docker: |
| 70 | +# logar no STDOUT ao invés de arquivos |
| 71 | +stdout_redirect nil, nil, true |
| 72 | + |
| 73 | +# ================================ |
| 74 | +# Boot logs |
| 75 | +# ================================ |
114 | 76 | on_booted do |
115 | | - puts "🚀 Puma booted (PID: #{Process.pid})" |
| 77 | + puts " Puma booted (PID: #{Process.pid})" |
116 | 78 | puts " Environment: #{ENV['RAILS_ENV']}" |
117 | 79 | puts " Workers: #{ENV.fetch('WEB_CONCURRENCY', 2)}" |
118 | | - puts " Threads: #{min_threads_count}-#{max_threads_count}" |
| 80 | + puts " Threads: #{min_threads}-#{max_threads}" |
119 | 81 | puts " Port: #{ENV.fetch('PORT', 3000)}" |
120 | 82 | end |
121 | 83 |
|
122 | | -on_worker_boot do |worker_index| |
123 | | - puts "👷 Worker #{worker_index} booted (PID: #{Process.pid})" |
124 | | -end |
125 | | - |
126 | | -on_worker_shutdown do |worker_index| |
127 | | - puts "👷 Worker #{worker_index} shutting down (PID: #{Process.pid})" |
128 | | -end |
129 | | - |
130 | | -# === Health Check Endpoint === |
131 | | -# This is automatically handled by Rails /up endpoint |
132 | | -# No additional configuration needed here |
0 commit comments