Slanger: Eventmachine port exception - ruby-on-rails

I've installed slanger (and redis) for a rails project. Every rails instance is off.
I run redis-server and it works properly. But then I run:
slanger --app_key MyPusherApiKey --secret MyPusherSecret
and it raises the next exception:
my-directory/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/eventmachine-1.0.9.1/lib/eventmachine.rb:534:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)
from my-directory/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/eventmachine-1.0.9.1/lib/eventmachine.rb:534:in `start_server'
from my-directory/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/em-websocket-0.5.1/lib/em-websocket/websocket.rb:46:in `run'
from my-directory/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/em-websocket-0.5.1/lib/em-websocket/websocket.rb:39:in `block in start'
from my-directory/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/eventmachine-1.0.9.1/lib/eventmachine.rb:170:in `call'
from my-directory/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/eventmachine-1.0.9.1/lib/eventmachine.rb:170:in `run'
I thought that if the port was in use, I could just change it like this:
slanger --app_key MyPusherApiKey --secret MyPusherSecret -a 0.0.0.0:3646
But I still get the same error. Any ideas?

Finally I saw that the port 8080 was being used by Nginx.
Stopped nginx and I got it working fine.
nginx -s stop

Related

Can't start Rails localhost - (no processes running on port 3000)

tried to start my rails server, but I got that error
=> Booting Puma
=> Rails 5.1.4 application starting in development
=> Run `rails server -h` for more startup options
[25156] Puma starting in cluster mode...
[25156] * Version 3.9.1 (ruby 2.4.1-p111), codename: Private Caller
[25156] * Min threads: 5, max threads: 5
[25156] * Environment: development
[25156] * Process workers: 2
[25156] * Preloading application
[25156] * Listening on tcp://0.0.0.0:3000
Exiting
/home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/binder.rb:269:in `initialize': Address already in use - bind(2) for "0.0.0.0" port 3000 (Errno::EADDRINUSE)
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/binder.rb:269:in `new'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/binder.rb:269:in `add_tcp_listener'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/binder.rb:105:in `block in parse'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/binder.rb:88:in `each'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/binder.rb:88:in `parse'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/runner.rb:144:in `load_and_bind'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/cluster.rb:391:in `run'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/puma/launcher.rb:174:in `run'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/puma-3.9.1/lib/rack/handler/puma.rb:69:in `run'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/server.rb:297:in `start'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:44:in `start'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:135:in `block in perform'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
from /home/ahmed/Rails/sample_app/bin/rails:9:in `require'
from /home/ahmed/Rails/sample_app/bin/rails:9:in `<top (required)>'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
from /home/ahmed/.rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
from /home/ahmed/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /home/ahmed/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /home/ahmed/Rails/sample_app/bin/spring:15:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
I thought there's some process or something running on port 3000, so I tried to find it using
lsof -wni tcp:3000
but the command returns nothing, so that looks very strange, any solutions?
Update #1
I run this code from another question on stackoverflow and I got that
➜ ~ sudo lsof -iTCP -sTCP:LISTEN -P | grep :3000
ruby 1595 beef-xss 17u IPv4 32839 0t0 TCP *:3000 (LISTEN)
so I tried
kill -9 1595
and
kill -9 32839
but nothing worked
also I restarted my computer, but the same problem exists, and there are no files inside my "tmp/pids" folders
Update #2
Solved it after uninstalling something called beef-xss using
sudo apt-get --purge remove beef-xss
If you are using a Linux operating system
First we will use the fuser command which identifies which processes are using a file or a socket.
fuser -n tcp 3000
3000 / tcp: 7425
The -n parameter is used when we want to identify processes that are using TCP or UDP sockets, in my case I want to know who is using port 3000.
The command returns the pid of the process, with this data and with the help of the ps command we will obtain more information about it.
ps x | grep 7425
7425 pts / 3 Tl 1:15 /home/carlos/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
script / rails s
With this we see that an instance of the rails test server was hung, to free the port we killed the process using the pid that we already have.
kill -9 7425
And then we see that indeed the process has been completed.
ps x | grep 7425
7973 pts / 3 S + 0:00 grep --color = auto 7425
[1] + Finished (killed) rails s
I hope it will be you useful
The article of the answer http://community.logicalbricks.com/node/103
Have you tried to delete the pid files in tmp/pids?
If there is anything in the folder try to find a Puma process with pgrep -lf puma and take a look if there's any process of that.
I think you have run another server on the same port and you did not stop this unfortunately and command prompt closed, that's why it's showing this error. SO you can resolve this if you restart your computer.
Just restart your computer then try again.
Also, you can try to remove the files from tmp/pids/
I run this code from another question on stackoverflow and I got that
➜ ~ sudo lsof -iTCP -sTCP:LISTEN -P | grep :3000
ruby 1595 beef-xss 17u IPv4 32839 0t0 TCP *:3000 (LISTEN)
so I tried
kill -9 1595
and
kill -9 32839
but nothing worked
also I restarted my computer, but the same problem exists, and there are no files inside my "tmp/pids" folders
Update #2
Solved it after uninstalling something called beef-xss using
sudo apt-get --purge remove beef-xss

`connection refused` to a local container while running doing`docker-compose build app`

I have a Rails application in containers and I would like to use a service like geminabox or gemstash to proxy https://rubygems.org et have a faster bundle install command.
I have started a gemstash container publishing the port 9292 and with curl on my host I can reach the service.
In the Dockerfile of my Rails application I have added the following line, before the bundle install:
RUN bundle config mirror.https://rubygems.org http://localhost:9292
When I run docker-compose build app (app is the service name) I have bundler failing with the following:
Errno::EADDRNOTAVAIL: Cannot assign requested address - connect(2) for "localhost" port 9292
/usr/local/lib/ruby/2.2.0/net/http.rb:879:in `initialize'
/usr/local/lib/ruby/2.2.0/net/http.rb:879:in `open'
/usr/local/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
/usr/local/lib/ruby/2.2.0/timeout.rb:88:in `block in timeout'
/usr/local/lib/ruby/2.2.0/timeout.rb:98:in `call'
/usr/local/lib/ruby/2.2.0/timeout.rb:98:in `timeout'
/usr/local/lib/ruby/2.2.0/net/http.rb:878:in `connect'
/usr/local/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
/usr/local/lib/ruby/2.2.0/net/http.rb:858:in `start'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb:702:in `start'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb:633:in `connection_for'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/vendored_persistent.rb:23:in `connection_for'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb:996:in `request'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/fetcher/downloader.rb:51:in `request'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/fetcher/downloader.rb:17:in `fetch'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/fetcher/compact_index.rb:117:in `call'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/compact_index_client/updater.rb:51:in `block in update'
/usr/local/lib/ruby/2.2.0/tmpdir.rb:88:in `mktmpdir'
/usr/local/bundle/gems/bundler-1.16.1/lib/bundler/compact_index_client/updater.rb:31:in `update'
...
I tried also to start the gemstash container with --network host in order to remove the network isolation but then even the curl command on my host is refused.
How can I have the service accessible from the docker-compose build command?
Docker builds images in default bridge network by default. We can change that with docker build --network <network>, but not with docker-compose build. Compose hasn't supported this yet. So, the only way to access gemstash container during the image build is starting it with --network host type and access it via host's IP address.
1. docker-compose.yaml is something like:
version: '3'
services:
gemstash:
image: <gemstash_image>
network_mode: "host"
rails_app:
build:
context: .
args:
IP_ADDRESS: ${IP_ADDRESS}
2. After gemstash container is started:
docker-compose up -d gemstash
we see that it is reachable on host's IP:
netstat -nlpt | grep 9292
tcp 0 0 0.0.0.0:9292 0.0.0.0:* LISTEN -
3. Dockerfile is modified to:
ARG IP_ADDRESS
RUN bundle config mirror.https://rubygems.org http://$IP_ADDRESS:9292
where IP_ADDRESS is the host's IP address.
4. Inject IP_ADDRESS variable to docker-compose.yaml. For this we create .env file and define our variable there (example is for linux host):
IP_ADDRESS=$(ip a | grep <interface> | grep inet | awk '{print $2}' | awk -F'/' '{print $1}')
echo "IP_ADDRESS=$IP_ADDRESS" > .env
5. Now we can build rails_app and it could reach gemstash container:
docker-compose build

Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

Before anything, please note that I have found several similar questions on Stack Overflow and articles all over the web, but none of those helped me fix my issue:
PG Error could not connect to server: Connection refused Is the server running on port 5432?
PG::ConnectionBad - could not connect to server: Connection refused
psql: could not connect to server: Connection refused
Now, here is the issue:
I have a Rails app that works like a charm.
With my collaborator, we use GitHub to work together.
We have a master and an mvp branches.
I recently updated my git version with Homebrew (Mac).
We use Foreman to start our app locally.
Now, when I try to launch the app locally, I get the following error:
PG::ConnectionBad at /
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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 tried to reboot my computers several times.
I also checked the content of /usr/local/var/postgres:
PG_VERSION pg_dynshmem pg_multixact pg_snapshots pg_tblspc postgresql.conf
base pg_hba.conf pg_notify pg_stat pg_twophase postmaster.opts
global pg_ident.conf pg_replslot pg_stat_tmp pg_xlog server.log
pg_clog pg_logical pg_serial pg_subtrans postgresql.auto.conf
As you can see, there is no postmaster.pid file in there.
Any idea how I could fix this?
run postgres -D /usr/local/var/postgres and you should see something like:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 379) running in data directory "/usr/local/var/postgres"?
Then run kill -9 PID in HINT
And you should be good to go.
You most likely ran out of battery and your postgresql server didn't shutdown correctly.
The easiest workaround is to download the official postgresql app and launch it: it will force the server to start (http://postgresapp.com/)
Most likely it's because your system shutdown unexpectedly
Try
postgres -D /usr/local/var/postgres
You might see
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 449) running in data directory "/usr/local/var/postgres"?
Then try
kill -9 PID
example
kill -9 419
And it should start postgres normally
The postgresql server might be down and the solution might be as simple as running:
sudo service postgresql start
which fixed the issue for me.
This could be caused by the pid file created for postgres which has not been deleted due to unexpected shutdown. To fix this, remove this pid file.
Find the postgres data directory. On a MAC using homebrew it is /usr/local/var/postgres/, other systems it might be /usr/var/postgres/
Remove pid file by running:
rm postmaster.pid
Restart postgress. On Mac, run:
brew services restart postgresql
I had almost just as same error with my Ruby on Rails application running postgresql(mac). This worked for me:
brew services restart postgresql
This worked in my case:
brew uninstall postgresql
rm -fr /usr/local/var/postgres/
brew install postgresql
In my case PostgreSQL updates from version 13.4 to 14 in background, so it fixes by:
brew postgresql-upgrade-database
In other case the problem fixed by:
rm -rf /usr/local/var/postgres/postmaster.pid
or
rm -rf /opt/homebrew/var/postgres/postmaster.pid
Restart service postgresql:
brew services restart postgresql
PS:
How can you understand what is the problem?
For first see what service is not correct started:
brew services list
For second show file postgres.log, where will be the error:
tail -f /usr/local/var/log/postgres.log
or
tail -f /opt/homebrew/var/log/postgres*
And so find answer by this error's text
I resolved the issue via this command
pg_ctl -D /usr/local/var/postgres start
At times, you might get this error
pg_ctl: another server might be running; trying to start server anyway
So, try running the following command and then run the first command given above.
pg_ctl -D /usr/local/var/postgres stop
Step 1:
cd /etc/postgresql/12/main/
open file named postgresql.conf
sudo nano postgresql.conf
add this line to that file
listen_addresses = '*'
then open file named pg_hba.conf
sudo nano pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart
This is how I solved my problem:
see the status of services
brew services list
and the output was :
Name Status User Plist
postgresql error myuser /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
unbound stopped
I change the file name in this path, you can also remove it
mv /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist_temp
and then reboot the os
sudo reboot
after booting I started the postgresql and it worked.
brew services start postgresql
Find postgresql#10 service directory
$ ls /usr/local/var/postgresql#10
Find file postmaster.pid and delete that file
$ rm -f postmaster.pid
Restart postgres service using
$ brew services restart postgresql#10
This worked for me:
run
sudo lsof -i :<port_number>
after that it will display the PID which is currently attached to the process.
After that run sudo kill -9 <PID>
if that doesn't work,
try the solution offered by user8376606 it would definitely work!
If you want to restart Postgresql on Linux, then you have to use the following command.
/etc/init.d/postgresql restart
In my case
I have changed the port in postgresql.conf file
and restart postgresql services in
Run => service.msc => Restart
now retry
First I tried
lsof -wni tcp:5432
but it doesn't show any PID number.
Second I tried
Postgres -D /usr/local/var/postgres
and it showed that server is listening.
So I just restarted my mac to restore all ports back and it worked for me.
For Docker users: In my case it was caused by excessive docker image size. You can remove unused data using prune command:
docker system prune --all --force --volumes
Warning: as per manual (docker system prune --help):
This will remove:
all stopped containers
all networks not used by at least one container
all dangling images
all dangling build cache
I encountered a similar problem when I was trying to connect my Django application to PostgreSQL database.
I wrote my Dockerfile with instructions to setup the Django project followed by instructions to install PostgreSQL and run Django server in my docker-compose.yml.
I defined two services in my docker-compose-yml.
services:
postgres:
image: "postgres:latest"
environment:
- POSTGRES_DB=abc
- POSTGRES_USER=abc
- POSTGRES_PASSWORD=abc
volumes:
- pg_data:/var/lib/postgresql/data/
django:
build: .
command: python /code/manage.py runserver 0.0.0.0:8004
volumes:
- .:/app
ports:
- 8004:8004
depends_on:
- postgres
Unfortunately whenever I used to run docker-compose up then same err. used to pop up.
And this is how my database was defined in Django settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'abc',
'HOST': '127.0.0.1',
'PORT': '5432',
'OPTIONS': {
'client_encoding': 'UTF8',
},
}
}
So, In the end I made use of docker-compose networking which means if I change the host of my database to postgres which is defined as a service in docker-compose.yml will do the wonders.
So, Replacing 'HOST': '127.0.0.1' => 'HOST': 'postgres' did wonders for me.
After replacement this is how your Database config in settings.py will look like.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'abc',
'HOST': 'postgres',
'PORT': '5432',
'OPTIONS': {
'client_encoding': 'UTF8',
},
}
}
I often encounter this problem on windows,the way I solved the problem is Service - Click PostgreSQL Database Server 8.3 - Click the second tab "log in" - choose the first line "the local system account".
It also gives the same error if you just stop your PostgreSQL app. You just need to start it again. (PostgreSQL 11)
I have faced the same issue and I was unable to start the postgresql server and was unable to access my db even after giving password, and I have been doing all the possible ways.
This solution worked for me,
For the Ubuntu users:
Through command line, type the following commands:
1.service --status-all (which gives list of all services and their status. where "+" refers to running and "-" refers that the service is no longer running)
check for postgresql status, if its "-" then type the following command
2.systemctl start postgresql (starts the server again)
refresh the postgresql page in browser, and it works
For the Windows users:
Search for services, where we can see list of services and the right click on postgresql, click on start and server works perfectly fine.
In my case I forgot to change the database from postgres (on my production) back to sqlite3 which I was using for development.
It's working for me >>Node.Js App
user#MacBook-Pro % sudo lsof -i :5430
Output
COMMAND PID user FD TYPE DEVICE SIZE/OFF NODE NAME
node 7885 user 21u IPv6 0x2e7d89f6118f95b9 0t0 TCP *:radec-corp (LISTEN)
Kill the PID
user#MacBook-Pro % sudo kill -9 7885
One more test
user#MacBook-Pro % sudo lsof -i :5430
user#MacBook-Pro % "No more running PID for the port 5430"
In my case, on a Ruby on Rails project, I removed a .pid file from the folder tmp/pids and restart the system.
Had the same issue. I checked that my database.yml file, (dev mode) host was pointing to 5433. I updated it to 5432 and it worked.
Just in case someone needs this for windows, read on.
On windows hit the Windows button + R
then enter services.msc and look for postgresql-x64-14, Right click it and click start
Then go back to your PgAdmin4 for windows and then enter your master password if asked.
From here, you should be able to proceed as usual with viewing of the db schemas.
Also, for Django, restart your server with CTR+C then python manage.py runserver (assuming you're working inside a virtual env)
Good luck
ps -ef|grep postgres
Then kill the process with PID
sudo kill -9 PID
Then start the postgresql
sudo service postgresql start
In my case I when it happens to me I need to do the following steps:
1º Step
Log in postgres user:
#sudo su postgres
2º Step
Run the following steps: /opt/PostgreSQL/10/bin/postgres -D /opt/PostgreSQL/10/data -r /usr/local/var/postgres/server.log
Explanation:
We access the utility of postgres located at /opt/PostgreSQL/10/bin/ in your case could be another but identify where it's.
After this step we tell to the utility of postgres where the it's data folder is by using the option -D, this data folder contains all necessary configuration of postgres server.
The option -r we tell to postgres where to send stdout and stderr to given file, in my case the file that I used is /usr/local/var/postgres/server.log
Note:
I'm using Postgre 10
Linux Ubuntu

Docker-compose - Redis at 0.0.0.0 instead of 127.0.0.1

I havs migrated my Rails app (local dev machine) to Docker-Compose. All is working except the Worker Rails instance (batch) cannot connect to Redis.
Completed 500 Internal Server Error in 40ms (ActiveRecord: 2.3ms)
Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)):
In my docker-compose.yml
redis:
image: redis
ports:
- "6379:6379"
batch:
build: .
command: bundle exec rake environment resque:work QUEUE=*
volumes:
- .:/app
links:
- db
- redis
environment:
- REDIS_URL=redis://redis:6379
I think the Redis instance is available via the IP of the Docker host.
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.10.0
Accessing via 0.0.0.0 doesn't work
$ curl 0.0.0.0:6379
curl: (7) Failed to connect to 0.0.0.0 port 6379: Connection refused
Accessing via the docker-machine IP I think works:
$ curl http://192.168.99.100:6379
-ERR wrong number of arguments for 'get' command
-ERR unknown command 'Host:'
EDIT
After installing redis-cli in the batch instance, I was able to hit the redis server using the 'redis' hostname. I think the problem is possibly in the Rails configuration itself.
Facepalm!!!
The docker containers were communicating just fine, the problem was I hadn't told Resque (the app using Redis) where to find it. Thank you to "The Real Bill" for pointing out I should be using docker-cli.
For anyone else using Docker and Resque, you need this in your config/initializers/resque.rb file:
Resque.redis = Redis.new(host: 'redis', port: 6379)
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
If you run
docker-compose run --rm batch env | grep REDIS
you will get the env variables that your container has (the link line in the compose will auto-generate some).
Then all you need to do is look for one along the lines of _REDIS_1_PORT... and use the correct one. I have never had luck connecting my rails to another service in any other way. But luckily these env variables are always generated on start so they will be up to date even if the container IP happens to change between startups.
You should use the hostname redis to connect to the service, although you may need to wait for redis to start.

Rails: Address already in use - bind(2) (Errno::EADDRINUSE)

I am trying to deploy Rails app with the Puma web server. When trying to start Puma server with a config file bundle exec puma -C config/puma.rb I get an error that the address is already in use.
Does someone know how to fix this?
bundle exec puma -C config/puma.rb
[23699] Puma starting in cluster mode...
[23699] * Version 2.11.3 (ruby 2.0.0-p353), codename: Intrepid Squirrel
[23699] * Min threads: 5, max threads: 5
[23699] * Environment: development
[23699] * Process workers: 2
[23699] * Preloading application
Jdbc-MySQL is only for use with JRuby
[23699] * Listening on tcp://0.0.0.0:3000
/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `new'
from /Users/lexi87/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `add_tcp_listener'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:96:in `block in parse'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `each'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `parse'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/runner.rb:119:in `load_and_bind'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cluster.rb:302:in `run'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cli.rb:216:in `run'
from /rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/bin/puma:10:in `<top (required)>'
from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `load'
from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `<main>'
from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
You need to use kill -9 59780 with 59780 replaced with found PID number (use lsof -wni tcp:3000 to see which process used 3000 port and get the process PID).
Or you can just modify your puma config change the tcp port tcp://127.0.0.1:3000 from 3000 to 9292 or other port that not been used.
Or you can start your rails app by using:
bundle exec puma -C config/puma.rb -b tcp://127.0.0.1:3001
To kill the puma process first run
lsof -wni tcp:3000
to show what is using port 3000. Then use the PID that comes with the result to run the kill process.
For example after running lsof -wni tcp:3000 you might get something like
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 3366 dummy 8u IPv4 16901 0t0 TCP 127.0.0.1:3000 (LISTEN)
Now run the following to kill the process. (where 3366 is the PID)
kill -9 3366
Should resolve the issue
you can also try this trick:
ps aux | grep puma
sample output:
myname 77921 0.0 0.0 2433828 1972 s000 R+ 11:17AM 0:00.00 grep puma
myname 67661 0.0 2.3 2680504 191204 s002 S+ 11:00AM 0:18.38 puma 3.11.2 (tcp://localhost:3000) [my_proj]
then:
kill -9 67661
Found the script below in this github issue. Works great for me.
#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")
pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?
You can either run it in irb or inside a ruby file.
For the latter, create server_killer.rb then run it with ruby server_killer.rb
SOLUTION FOR GENERAL Address already in use - bind(2) (Errno::EADDRINUSE)
This issue is because we are trying to use the same port which is already is use. so we have to stop the services running on that port so that we can run another services.
we can use kill like kill -9 {PID}where {PID} is the PID of the services running on that port. To know the PID of any services lets say "firefox" we can use commands like pidof firefox, ps aux | grep -i firefox ,pgrep firefox and then use the kill command to stop that service.
sometime we might get into the situation where we don't know the PID or the service name to search for in this case we can use the following little ruby code to do it for us.(in this case port 3000, you can change it according to your need)
#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")
pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?
save it as something.rb and run sudo ruby something.rb
If the above solutions don't work on ubuntu/linux then you can try this
sudo fuser -k -n tcp port
Run it several times to kill processes on your port of choosing. port could be 3000 for example. You would have killed all the processes if you see no output after running the command
You can find and kill the running processes: ps aux | grep puma
Then you can kill it with kill PID
I had this issue on my Macbook Air, running Rails 5.0.3, Puma 5.2.2
Tried running
lsof -wni tcp:3000 but there's no process on this port number.
Managed to fix this by running:
export PORT=3000 on my terminal, then I just added this extra line to my .bash_profile
It might be old but in my case, it was because of docker. Hope it will help others.

Resources