Reputation: 183
New Rails 7 application with css-bundling and bootstrap. Now supposed to run bin/dev for dev server, using puma
It does not load my config/puma.rb
I commented out the line for the port in bin/dev, and it just binds to port 5000 (config/puma.rb says to go to port 3001)
bin/dev:
#!/usr/bin/env sh
if ! gem list foreman -i --silent; then
echo "Installing foreman..."
gem install foreman
fi
# Default to port 3000 if not specified
# export PORT="${PORT:-3000}"
exec foreman start -f Procfile.dev "$@"
Procfile.dev:
web: env RUBY_DEBUG_OPEN=true bin/rails server -c config/puma.rb
css: yarn watch:css
I tried to force bin/rails server to use my config file, but get a LoadError.
Running 'rails s' works just like it used to, binds to correct ip and port, so I don't believe anything to be wrong with my config/puma.rb file.
I tried to add the following line to application.rb, even though I didn't think this would work...and it didn't:
config.autoload_paths << Rails.root.join('config')
config/puma.rb contains this line:
bind 'tcp://0.0.0.0:3001'
I'm not simply trying to assign a port, I'm also trying to bind the address, this is running in a vm, so I want to access it from my host OS, not 'localhost'
I could change the Procfile.dev to run rails server -b -p, etc...but I would rather simply let it use my same config file, which works when I simply run 'rails s'
Do I need to include the config/ directory somehow. I can't find any other configurations that may be overriding this, although I'm not very familiar with foreman
-UPDATE-
Moved my puma.rb file (originally under config/puma.rb to bin/config/puma.rb
Changed Profile.dev, web: line to
web: env RUBY_DEBUG_OPEN=true bin/rails server -c ./config/puma.rb
This finds the config file now, but I get a different error:
rails_app/config/puma.rb:8:in
<main>': undefined method
threads' for main:Object (NoMethodError)threads threads_count, threads_count
Interestingly enough, this is looking to the puma.rb under the normal rails app config directory, not the copy I placed in bin/config/puma.rb
rails_app/config/puma.rb:
# 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.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count
bind 'tcp://0.0.0.0:3001'
environment ENV.fetch("RAILS_ENV") { "development" }
workers(ENV.fetch("RAILS_ENV") == "production" ? 3 : 0)
plugin :tmp_restart
The only setting I have changed is the bind address, the rest was generated by rails.
This config file work when running 'rails s', so where does the 'threads' method come from, and why the NoMethodError when running bin/dev instead of just simply 'rails s'?
-UPDATE 2-
This all began trying to install bootstrap and migrate from importmaps to esbuild. The idea is to use bin/dev to start the dev server to watch for changes in stylesheets and javascript, and re-bundle/compile the files.
Output from 'rails s' command:
> => Booting Puma
> => Rails 7.1.2 application starting in development
> => Run `bin/rails server --help` for more startup options Puma starting in single mode...
> * Puma version: 5.6.5 (ruby 3.1.1-p18) ("Birdie's Version")
> * Min threads: 5
> * Max threads: 5
> * Environment: development
> * PID: 43056
> * Listening on http://0.0.0.0:3001 Use Ctrl-C to stop
Using this Procfile.dev:
web: env RUBY_DEBUG_OPEN=true bin/rails server
css: yarn watch:css
js: yarn build --watch
Output of 'bin/dev' command:
01:58:33 web.1 | started with pid 43170
01:58:33 css.1 | started with pid 43171
01:58:33 js.1 | started with pid 43172
01:58:33 web.1 | DEBUGGER: Debugger can attach via UNIX domain socket (/run/user/1000/ruby-debug-zak-43170)
01:58:34 js.1 | yarn run v1.22.19
01:58:34 css.1 | yarn run v1.22.19
01:58:34 css.1 | $ nodemon --watch ./app/assets/stylesheets/ --ext scss --exec "yarn build:css"
01:58:34 js.1 | $ esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=/assets --watch
01:58:34 web.1 | => Booting Puma
01:58:34 web.1 | => Rails 7.1.2 application starting in development
01:58:34 web.1 | => Run `bin/rails server --help` for more startup options
01:58:34 css.1 | [nodemon] 3.0.1
01:58:34 css.1 | [nodemon] to restart at any time, enter `rs`
01:58:34 css.1 | [nodemon] watching path(s): app/assets/stylesheets/**/*
01:58:34 css.1 | [nodemon] watching extensions: scss
01:58:34 css.1 | [nodemon] starting `yarn build:css`
01:58:34 js.1 | [watch] build finished, watching for changes...
01:58:34 web.1 | Puma starting in single mode...
01:58:34 web.1 | * Puma version: 5.6.5 (ruby 3.1.1-p18) ("Birdie's Version")
01:58:34 web.1 | * Min threads: 5
01:58:34 web.1 | * Max threads: 5
01:58:34 web.1 | * Environment: development
01:58:34 web.1 | * PID: 43170
01:58:34 web.1 | * Listening on http://127.0.0.1:5000
01:58:34 web.1 | * Listening on http://[::1]:5000
01:58:34 web.1 | Use Ctrl-C to stop
01:58:34 css.1 | $ yarn build:css:compile && yarn build:css:prefix
01:58:34 css.1 | $ sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules
01:58:35 css.1 | $ postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css
01:58:36 css.1 | [nodemon] clean exit - waiting for changes before restart
The issue is the bind address, I need http://0.0.0.0, and running 'bin/dev' shows http://127.0.0.1, port is irrelevant.
I changed my Procfile.dev to this:
web: env RUBY_DEBUG_OPEN=true bin/rails server -c ./config/puma.rb
css: yarn watch:css
js: yarn build --watch
And running 'bin/dev' throws an error on 'threads' in my puma.rb config
02:02:51 web.1 | started with pid 43565
02:02:51 css.1 | started with pid 43566
02:02:51 js.1 | started with pid 43567
02:02:51 css.1 | yarn run v1.22.19
02:02:51 js.1 | yarn run v1.22.19
02:02:51 js.1 | $ esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=/assets --watch
02:02:51 css.1 | $ nodemon --watch ./app/assets/stylesheets/ --ext scss --exec "yarn build:css"
02:02:51 web.1 | DEBUGGER: Debugger can attach via UNIX domain socket (/run/user/1000/ruby-debug-zak-43565)
02:02:51 css.1 | [nodemon] 3.0.1
02:02:51 css.1 | [nodemon] to restart at any time, enter `rs`
02:02:51 css.1 | [nodemon] watching path(s): app/assets/stylesheets/**/*
02:02:51 css.1 | [nodemon] watching extensions: scss
02:02:51 css.1 | [nodemon] starting `yarn build:css`
02:02:51 web.1 | => Booting Puma
02:02:51 web.1 | => Rails 7.1.2 application starting in development
02:02:51 web.1 | => Run `bin/rails server --help` for more startup options
02:02:51 web.1 | Exiting
02:02:51 web.1 | /home/zak/wDevLocal/Development/Castellatus/config/puma.rb:8:in `<main>': undefined method `threads' for main:Object (NoMethodError)
02:02:51 web.1 |
02:02:51 web.1 | threads threads_count, threads_count
02:02:51 web.1 | ^^^^^^^
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/zeitwerk-2.6.8/lib/zeitwerk/kernel.rb:38:in `require'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/rack-2.2.7/lib/rack/builder.rb:68:in `parse_file'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/rack-2.2.7/lib/rack/server.rb:349:in `build_app_and_options_from_config'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/rack-2.2.7/lib/rack/server.rb:249:in `app'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/rack-2.2.7/lib/rack/server.rb:422:in `wrapped_app'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/commands/server/server_command.rb:76:in `log_to_stdout'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/commands/server/server_command.rb:36:in `start'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/commands/server/server_command.rb:145:in `block in perform'
02:02:51 web.1 | from <internal:kernel>:90:in `tap'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/commands/server/server_command.rb:136:in `perform'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/thor-1.2.2/lib/thor/command.rb:27:in `run'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/thor-1.2.2/lib/thor/invocation.rb:127:in `invoke_command'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/command/base.rb:178:in `invoke_command'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/thor-1.2.2/lib/thor.rb:392:in `dispatch'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/command/base.rb:73:in `perform'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/command.rb:71:in `block in invoke'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/command.rb:149:in `with_argv'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/command.rb:69:in `invoke'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/railties-7.1.2/lib/rails/commands.rb:18:in `<main>'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
02:02:51 web.1 | from /home/zak/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
02:02:51 web.1 | from bin/rails:4:in `<main>'
02:02:51 js.1 | [watch] build finished, watching for changes...
02:02:51 css.1 | $ yarn build:css:compile && yarn build:css:prefix
02:02:52 css.1 | $ sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules
02:02:52 web.1 | exited with code 1
02:02:52 system | sending SIGTERM to all processes
02:02:52 css.1 | exited with code 1
02:02:52 js.1 | exited with code 1
So it is finding my puma.rb config file, but throws an error, can't find the 'threads' method in the rails_app/config/puma.rb file:
# 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.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
# port ENV.fetch("PORT") { 3000 }
bind 'tcp://0.0.0.0:3001'
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
workers(ENV.fetch("RAILS_ENV") == "production" ? 3 : 0)
# 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. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!
# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end
# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
# on_worker_boot do
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end
#
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
Foreman (I have never used before, have not looked into what it actually does) is running what looks like the same command as when I just run 'rails s'.
I investigated further and ran 'rails s -c ./config/puma.rb'...this time I did in fact get the same 'threads' No Method Error. I only get this error when 'rails s' is run with the -c ./config/puma.rb flag. However, I know it is loading the bind 0.0.0.0 address from this config file when I run 'rails s'. If I take out the bind address line, it binds to 127.0.0.1.
Maybe will figure it out eventually. I'm not running puma.rb in production so, I have decided to change my Procfile.dev to this:
web: env RUBY_DEBUG_OPEN=true bin/rails server -b 0.0.0.0 -p 3001
css: yarn watch:css
js: yarn build --watch
This works.
Upvotes: 0
Views: 517