'foreman start' is stuck - ruby-on-rails

I have a locally developed Rails application. It is very simple, with no database but only a controller that accesses Facebook data and renders them in view. By the way, gem RestGraph is used.
Before I push this application to heroku, I used foreman start to test it. Since I was using WEBrick before I need to install 'thin' gem and create a Procfile which reads:
web: bundle exec thin start -R config.ru -e $RACK_ENV -p $PORT
an '.env' file seems also required, which reads:
RACK_ENV=development
PORT=3000
The 'config.ru' file is generate by rails, which reads:
require ::File.expand_path('../config/environment', __FILE__)
run Myapp::Application
Now I entered 'foreman start', but all I get is one line:
15:33:18 web.1 | started with pid 27143
The server will not boot. And if I force terminate it, the error is:
/usr/local/foreman/lib/foreman/engine.rb:141: [BUG] rb_sys_fail() - errno == 0
Which is not very helpful.

It may be that instead of being "stuck," your log is just being buffered: foreman only shows line with “started wit pid #” and nothing else
I solved that issue by adding
$stdout.sync = true
to config.ru
Then you might get more helpful feedback from Foreman.

This seems a little over complex. From looking at a couple of apps I have I have the following in my procfile:
bundle exec rails server thin -p $PORT
I also don't have a .env file in these instances.
One thing worth noting is that Foreman will ignore the port you pass in most of the time, it likes to sit in a port range higher than 5000

Related

foreman start doesn't seem to bind puma socket in development

I'm working a Rails application ran into some problems with a 502 error when I tried to go to our staging site. This thread had the answer I needed by adding to puma.rb:
application_path = '/home/deploy/hotel-automata/shared'
bind "unix://#{application_path}/sockets/puma.socket"
I was also able to start the server in development by running foreman start -p 3000. The question I have is how does this not throw an error? The application_path I have definitely doesn't exist in my local computer and running bundle exec puma or bundle exec rails server threw an error on file not found. Is this something that's specific to foreman or am I missing something? I looked around for a while but couldn't find anything related to this.
Also, is this the best way to do this? I feel that using an exact path might not be the best way here but I'm not sure what else I should do. Any suggestions on this point would be much appreciated too.
Also, here's my Procfile:
web: bundle exec rails server
redis: redis-server
sidekiq: bundle exec sidekiq

How can I run a ruby script right after the server starts in Rails 5

I have two ruby scripts that need to be running while the server is.
Currently I am running them separately using detached screens, but I would like to launch them at the same time the rails server is starting.
How can I integrate them so that I can achieve this behavior?
Have you tried Foreman gem? It will allow you to create a simple file (Procfile) where you can specify all the process that should be started simultaneously.
I usually create a file named Procfile.dev in the project's root, that would look like for example:
web: bundle exec rails server thin start -p 4000
mail: mailcatcher -f
your_script: instructions
Then you start your Rails app as:
foreman start -f Procfile.dev
With that command, Foreman will execute all the processes on the file.
You should install the gem locally and not in the Gemfile.
Foreman Gem

foreman development vs production (rails)

what is the "foreman way" for behaving differently in production vs
development? That is we want foreman start to start up a bunch of
stuff in dev, however in heroku production we don't need it to start
(for example) solr.
I follow the convention;
Procfile defines all processes
.foreman set specific foreman variables
Development:
.env sets environment variables for each developer
.env.example sets defaults for development
foreman start starts all processes
Production:
heroku config sets environment variables
heroku ps:scale turns on or off whichever processes are needed for production
Here's an example from a project.
Procfile:
web: bundle exec unicorn_rails -p $PORT -c ./config/unicorn.rb
worker: bundle exec rake jobs:work
search: bundle exec rake sunspot:solr:run
.env.example:
# default S3 bucket
S3_KEY=keykeykeykeykeykey
S3_SECRET=secretsecretsecret
S3_BUCKET=myapp-development
.env
# developer's private S3 bucket
S3_KEY=mememememememememe
S3_SECRET=mysecretmysecret
S3_BUCKET=myapp-development
.foreman:
# development port is 3000
port: 3000
Foreman takes arguments to use a different file (-d) and arguments to specify what to run. It also supports a .foreman file that allows those args to become default. See http://ddollar.github.com/foreman/ for more info
I've used environment-specific Procfiles before, which is pretty simple and works fine.
Basically you have Procfile.development, Procfile.production, etc. In each you can customize the procs you want to start, then run them via foreman like so:
foreman start -f Procfile.development
Another approach is to reference scripts in your Procfile, and within each script start up the appropriate process based on the environment. The creator of Foreman does this and has an example from his Anvil project your reference.
Our solution was to use a separate job type in our Procfile for dev vs. production. It is not the most DRY method, but it works...
sidekiq: bundle exec sidekiq
sidekiq-prod: bundle exec sidekiq -e production
Then we run foreman specifying the prod job on the production system:
foreman start -c sidekiq-prod=4

Heroku/ruby on rails... how can I figure out if I'm running as a worker or a web

I have an app on (ruby/rails) heroku. It's running 1 web and 1 worker (for example)
I want to be able to tell what "type" of dyno the app is running under.
I suspect it's a simple thing to tell, but I can't see anything that tells me how to tell.
I don't know if there's a more elegant way to do this, but you can set an environment variable in your Procfile:
web: bundle exec ... PROC_TYPE=web
worker: bundle exec ... PROC_TYPE=worker
Then in your rails code, you can check ENV['PROC_TYPE']
EDIT: more detailed Procfile example, typical for a rails app:
web: bundle exec rails server -p $PORT PROC_TYPE=web
worker: bundle exec rake jobs:work PROC_TYPE=worker

Rails server hangs when started with foreman

Here's what my Procfile looks like:
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
worker: bundle exec rake jobs:work
I intend to add a worker process because I wish to run some background jobs. I'm following these instructions
This is what I noticed:
No problems encountered if the worker is started separately.
When I keep the second line in the Procfile and don't not change anything else, the rails server serves a couple of requests and hangs after that
As mentioned here , I've added STDOUT.sync = true to config/environments/development.rb and verified the same in the rails console. Did not work.
Tailed log/development.log and compared it against the stuff that foreman outputted to the shell and noticed that both matched for a couple of requests and then foreman stopped printing out stuff to the shell - the next request would then hang
I updated foreman using foreman.pkg as mentioned here and verified the same with [6]
It was mentioned here that this might be caused due to a stray debug statement. I'm not using the debugger and I do not have the pry gem or the ruby-debug gem in my Gemfile.lock
I believe the symptoms are similar to this related unanswered question
Please help!
[6]:
which foreman
/usr/bin/foreman
ls -lah /usr/bin/foreman # checked the updated date
Tracked and resolved here:
https://github.com/ddollar/foreman/issues/244
TL;DR: Install the gem, don't use foreman.pkg
I'll add than if you're using Heroku, the foreman version included with the heroku toolbelt has the same issue.
Use the one you can get with gem install foreman instead.

Resources