Heroku Postgres connection capped at 5/20 - ruby

I have a Sinatra app running on Heroku free plan with Postgres, and I tried to set the database connection to 20 but after I pushed and run heroku pg:info the connection is still 5/20. It also wouldn't use all 20 connections even if I try to to a loader.io load test on it.
So I wonder what could I do to make it utilize all the 20 available connections or I am having some misunderstanding here?
# database.yml (for ActiveRecord)
production:
adapter: postgresql
encoding: unicode
database: mydb
username: <%= ENV['PG_USER'] %>
password: <%= ENV['PG_PASS'] %>
pool: <%= ENV['DB_POOL'] || ENV['RAILS_MAX_THREADS'] || 20 %>
# Procfile
web: bundle exec puma -t 5:20 -p $PORT
# puma.rb
preload_app!
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
# config.ru
require_relative './config/init'
use Rack::SSL if ENV['RACK_ENV'] == 'production'
run Sinatra::Application
The DB_POOL variable has been set to 20 on Heroku but it still doesn't do anything. Any help will be appreciated.

I fixed this by running puma with -C config/puma.rb

Related

Can't access sinatra service outside of container

I am getting ERR_EMPTY_RESPONSE in the browser after lunching the Sinatra container in docker. If I connect to the docker container and run curl localhost:4567 it works, but outside of the container - doesn't
web.rb
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end
Dockerfile
FROM ruby:3.0.2
EXPOSE 4567
COPY ./web.rb .
RUN gem install sinatra
RUN gem install puma
CMD ["ruby", "web.rb"]
The solution was in binding application to 0.0.0.0 host
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end

Trying to start Ruby on Rails app with Postgresql --noob

This is my 3rd day in a row trying to start a new app using Postgresql w/ Ruby on rails with wsl ubuntu.....(i'm on windows)
I'm a beginner. I've learned the basic syntax, but getting a practice app running on Postgres i just cant seem to do it. ill literally pay for help if someone can help me out.
Everytime i try to start the database i get
chronokross#DESKTOP-V7DS5HD:/mnt/c/code/nathantest$ rake db:create
Database 'chrono_development' already exists
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Couldn't create 'chrono_test' database. Please check your configuration.
rake aborted!
ActiveRecord::NoDatabaseError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Caused by:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Tasks: TOP => db:create
(See full trace by running task with --trace)
chronokross#DESKTOP-V7DS5HD:/mnt/c/code/nathantest
I've followed millions of guides. I dont know how to connect postgresql on windows to my ubunix server ROR app.
my discord is ChronoKross#1560.
They say to edit my databse yml file. This is how i did it
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: chrono_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
username: chronokross
# The password associated with the postgres role (username).
password: *****
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: chrono_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass#localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: chrono_production
username: chronokross
password: *****
PLEASE PLEASE I BEG FOR HELP. I've been studying 8hrs a day but this is a HUGE road block, i need someone to tell me what to do in noob terms or screenshare with me.
again my discord is ChronoKross#1560

"PG::ConnectionBad - could not connect to server: Connection refused" Sinatra server on Heroku

My Mad Libs program (code, Heroku app; earlier version on repl.it) runs fine in my Ubuntu VirtualBox, and for a few minutes after pushing the code to Heroku, it was actually working on Heroku, too. Then after maybe 15-30 minutes either I did something or something happened on the server, and ever since the app page says "Internal Server Error" and the relevant part of server logs read:
2018-04-14T02:03:45.717579+00:00 heroku[web.1]: Starting process with command `bundle exec ruby app.rb -p 7330`
2018-04-14T02:03:48.443468+00:00 app[web.1]: [2018-04-14 02:03:48] INFO ruby 2.4.0 (2016-12-24) [x86_64-linux]
2018-04-14T02:03:48.443866+00:00 app[web.1]: == Sinatra (v2.0.1) has taken the stage on 7330 for production with backup from WEBrick
2018-04-14T02:03:48.443437+00:00 app[web.1]: [2018-04-14 02:03:48] INFO WEBrick 1.3.1
2018-04-14T02:03:48.444166+00:00 app[web.1]: [2018-04-14 02:03:48] INFO WEBrick::HTTPServer#start: pid=4 port=7330
2018-04-14T02:03:48.855874+00:00 heroku[web.1]: State changed from starting to up
2018-04-14T02:04:04.372745+00:00 heroku[router]: at=info method=GET path="/" host=youmadlibs.herokuapp.com request_id=118d681f-3402-44aa-9d04-75d930461195 fwd="74.135.49.116" dyno=web.1 connect=0ms service=20ms status=500 bytes=854 protocol=https
2018-04-14T02:04:04.368607+00:00 app[web.1]: 2018-04-14 02:04:04 - PG::ConnectionBad - could not connect to server: Connection refused
2018-04-14T02:04:04.368644+00:00 app[web.1]: Is the server running on host "localhost" (127.0.0.1) and accepting
2018-04-14T02:04:04.368648+00:00 app[web.1]: TCP/IP connections on port 5432?
2018-04-14T02:04:04.368651+00:00 app[web.1]: :
2018-04-14T02:04:04.368653+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `initialize'
2018-04-14T02:04:04.368654+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `new'
2018-04-14T02:04:04.368656+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `connect'
2018-04-14T02:04:04.368659+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:684:in `connect'
etc.
The relevant lines seem to be:
PG::ConnectionBad - could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
I've tried a bunch of stuff, like restarting the server, reducing the dynos to 0 and then re-upping to 1, and running $ heroku run psql -h HEROKU_POSTGRESQL_SILVER_URL -U deploy youmadlibs . I read the Heroku documentation for connecting Ruby to databases, and for database.yml, it has host: localhost. I tried that (despite the fact that it had worked before with the setting I have below, which I took from a tutorial). I also tried a different set of database.yml settings from this tutorial. No dice! I read many answers here and elsewhere and can't figure it out. How can I get the database-server connection reestablished?
database.yml:
development:
adapter: sqlite3
database: db/madlibs.sqlite
host: localhost
pool: 5
timeout: 5000
production:
url: <%= ENV['DATABASE_URL'] %>
adapter: postgresql
database: mydb
host: <%= ENV['DATABASE_HOST'] %>
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
pool: 5
timeout: 5000
.env:
DATABASE_URL=<...my database name...>
config/environment.rb:
configure :development do
set :database, {adapter: "sqlite3", database: "db/madlibs.sqlite3"}
set :show_exceptions, true
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
Gemfile:
source 'https://rubygems.org'
ruby '2.4.0'
gem 'sinatra', '2.0.1'
gem 'pg', '1.0.0'
gem 'activerecord', '5.2.0'
gem 'sinatra-activerecord', '2.0.13'
gem 'sinatra-flash', '0.3.0' # Probably not nec.
gem 'sinatra-redirect-with-flash', '0.2.1' # Probably not nec.
gem 'rake', '10.4.2'
gem 'rack', '2.0.3'
gem 'sass', '3.4.24'
gem 'json', '2.1.0'
group :development do
gem 'sqlite3', '1.3.13'
gem 'tux', '0.3.0' # Maybe unnec.
end
group :production do
end
The top of app.rb:
require 'sinatra'
require 'sinatra/activerecord'
require './config/environment'
enable :sessions
class Madlib < ActiveRecord::Base
end
You don't need the other lines if you have the db url, it can be a 1 liner, and also let postgres timeout it's default, no need to set it. You probably want a larger pool size in prod. Go to the postgres database you should have added to your app in Heroku. Click on the View Credentials button. Copy the data from there to your local .env file and also add .env to your .gitignore file because you don't want to commit your private credentials to your public repo.
Then on heroku
database.yml:
development:
adapter: sqlite3
database: db/madlibs.sqlite
host: localhost
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
host: <%= ENV['DB_HOST'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
port: <%= ENV['DB_PORT'] %>
database: <%= ENV['DB_NAME'] %>
pool: 25
Alternatively, you could just use 1 environment variable which concatenates all of this into a URI which would look something like
#postgresql://username:password#db-shared-us-east-1-or-something.blah:1234/db
#If you use this format you can use just a single environment variable somthinglike
production:
adapter: postgresql
encoding: unicode
url: <%= ENV['DATABASE_URI'] %>
pool: 25
In either case you will need to set whatever environment variables you'll use in your database.yml file set in your Heroku dashboard at: https://dashboard.heroku.com/apps/yourappname/settings and click on Reveal Config Vars. This is where you will set the needed vars as this is where your app will load them from. Set either the vars in my first example, or a single one as in my 2nd example. Either way should work.
Make sure you have the pg gem in your production group in Gemfile. And make sure you've set your environment variables on your heroku server.

Install rails with WSL and postgresql

I'm trying to setup a rails environment development on a Windows 10.
I follow the tutorial of 'go_rails' (https://gorails.com/setup/windows/10)
Most of the installation seems to worked fine (when i type rby -v or rails -v in the bash it's return the expected result).
My issue is with postresql which is used for the project i work on it.
Following the instructions of the tutorial i install Postgresql (10) directly on Windows. It's seems to work since in can login using the pgadmin on windows or by typing 'psql -p 5432 -h localhost -U postgres' in the bash.
So it's look like postgresql is working, but when i do a rake db:create in bash, i got an error : could not connect to the server: No such file or directory. Is the server running locally and accepting connections on Unix domain socket '/var/run/postgresql/.s.PGSQL.5432'
In the postgresql.conf (C:/Programms/.../Data/postgresql.conf) the listen_addresses is set to '*'.
A bit after there is a line named '#unix_socket_directoris = ''', do you thing i should set something in there?
I really need to get that project work.
Thanks for your help
The problem is likely that you've installed the Windows binary for PostgreSQL, but you're trying to connect to it from Windows Subsystem for Linux using a Unix socket, which doesn't exist.
You need to use TCP/IP to connect rather than a Unix socket. When typing psql on the command line, add the option --host=127.0.0.1 to connect via TCP/IP.
I just went through the entire Go Rails tutorial and I also had trouble at the rake db:create step. All you need to do is add host: 127.0.0.1 to your database.yml as shown below. Make sure to Start PostgreSQL before you run rake db:create.
default: &default
host: 127.0.0.1
adapter: postgresql
encoding: unicode
username: postgres
password: ENV['POSTGRESQL_PASSWORD']
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
username: app_username
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
Add host: localhost to your default: in database.yml. Without something telling rails where to look for the host, it looks for the process in linux, which, of course, won't work, since windows processes are not exposed to the wsl layer.

Heroku: database configuration does not specify adapter

I have an app deployed in heroku that uses postgresql as database. I have created a new model that will connect to a remote mysql database like the following:
# other_database.rb(apps/models/)
class OtherDatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection "remote_#{Rails.env}"
end
# other_model.rb(apps/models/)
class OtherModel < OtherDatabase
self.table_name = "users"
end
i have also edited the database.yml file and added these entries:
remote_development:
adapter: mysql2
encoding: utf8
reconnect: false
database: app_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
remote_production:
adapter: mysql2
encoding: utf8
reconnect: true
database: app_production
pool: 40
username: root
password: secretpassword
socket: /var/run/mysqld/mysqld.sock
host: 12.34.56.78
The remote database uses mysql as its database.
It works fine in my local machine, but when I deploy it to heroku, it creates an error page so I looked up on the heroku logs, and I found this one out:
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/connection_specification.rb:52:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'
I am spent almost all my time searching for some answers but to no avail. Hope anyone can help out with this issue.
TIA.
Eralph
Heroku deletes and recreates the database.yml file. This means that what ever you put in your database.yml file will be totally ignored by heroku.
Pop your db credentials in your establish_connection method call like so:
establish_connection(
adapter: 'mysql2',
encoding: 'utf8',
reconnect: true,
database: 'app_production',
pool: 40,
username: 'root',
password: 'secretpassword',
socket: '/var/run/mysqld/mysqld.sock',
host: '12.34.56.78'
)
However it would be better to use a database URL and store it in an environment variable in heroku.
heroku config:set MYSQL_DB_URL=http://example.com/rest_of_the_url
then use
establish_connection(ENV['MYSQL_DB_URL'])
Example and docs for a url can be found here: http://forums.mysql.com/read.php?39,10734,10750

Resources