How to change the name Procfile - gem foreman - ruby-on-rails

I use on heroku unicorn , Procfile is already used by Heroku for unicorn server. I would like on development use gem foreman , is it possible to configure gem use different file than Procfile??

foreman start --procfile myfile
or
foreman start -f myfile
That being said, the whole idea of foreman and Procfiles is to use the same one everywhere so you can keep your development environment as close as possible to your production environment.
I would recommend looking at the docs here and making sure you really need a whole separate file for development.

Related

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

Unicorn web server questions

I recently discovered this line in my Gemfile:
# Use unicorn as the app server
# gem 'unicorn'
I have 2 questions.
Why would I want to use unicorn over the default WEBrick?
How do I get it to work? I uncommented that line, ran bundle install and then rails server and it still booted up WEBrick
Reasons why you would use Unicorn instead of WEBrick?
Unicorn is supposed to be faster than WEBrick
You can spawn multiple processes
If you are using unicorn in production. You would want your development env to be as close as production.
How to run unicorn locally?
Uncomment gem 'unicorn' in Gemfile
Create unicorn.rb file in config/ and add the following line. You can increase the number of processes if you want to
worker_processes 1
start unicorn using the following command
unicorn -c config/unicorn.rb
While this is mostly an opinion answer, Unicorn supports multiple "worker" processes to handle concurrent web requests by executing one instance of Unicorn. The number of worker processes you can run depends on the specs of the hardware, but generally 3-4 workers is safe for small servers and even development machines. You'd need multiple WEBrick processes for concurrent requests. I've also found Unicorn to be faster than WEBrick, especially in production applications and apps running on Heroku. Heroku actually has some really good documentation on this that is applicable outside of Heroku as well.
Take a look at the Unicorn gem documentation as well as the Heroku docs above. TL;DR - you'll use the command unicorn instead of rails server to run your app using Unicorn.
You can also use the unicorn_rails gem which will override default webrick and unicorn instead
https://github.com/samuelkadolph/unicorn-rails

Delay Job Worker keeps turning off?

New to Rails and very new to Delayed Jobs.
Got one that's supposed to be triggered after 5 minutes. I finally got it to work so that if I run
rake jobs:work
in my terminal, the job starts up and works correctly. If I CTRL-C and exit that action in my terminal, the delayed job stops working correctly. This is one thing on my local server and another on Heroku, where I have to start up the delayed job using
heroku run rake jobs:work
I looked into the new Heroku toolbelt and downloaded the gem they suggest for worker maintenance, foreman, but when I run "foreman start", I get this error
ERROR: procfile does not exist
I don't know what a procfile is, I'm afraid of breaking things after spending pretty much a day debugging my delayed_jobs actions, and I want to do this right to make sure it works instead of figuring out some hacky fix that breaks down later -- so I figured I should ask this question, however obnoxiously vague it may be.
Should I be using foreman for this? Or workless? (Saw that in another SO question). Where's my procfile? Should I do anything with it?
Thanks,
Sasha
You should be using a procfile to set up your Heroku processes, this is the standard method that Heroku uses to define and control the processes.
If you haven't utilised a procfile to this point everything will probably still work as Heroku adds some default processes when you push a Rails app, including both the web and worker processes. The default worker process is set to delayed job.
Foreman was developed in order to set up your local machine to use the same approach but, unlike the Heroku service, Foreman actually requires a procfile to be present to control the services that are started when Foreman is started as it doesn't know how to setup defaults.
I would suggest creating a procfile, placed in the root directory of your project, to ensure that your processes are set up and operating in the same manner on your local machine as on Heroku. If you want to mimic what Heroku sets up automatically you add the following to the procfile depending on whether you are using the Thin web server (which Heroku recommends) or not.
With Thin in your gemfile:
web: bundle exec thin start -R config.ru -e $RACK_ENV -p $PORT
worker: bundle exec rake jobs:work
Without a special web server (eg you are using webrick, the rails default):
web: bundle exec rails server -p $PORT
worker: bundle exec rake jobs:work
Once this file is in place you can run foreman on your local machine and it will start your web server and delayed_job workers automatically.
Running through this process will only impact starting delayed_job on the local machine. As you are running the exact same command bundle exec rake jobs:work as you are currently using there should be no impact on your dj actions in either locally or on Heroku. Obviously some testing is required to make suer this is actually the case.
Workless is designed to scale workers on Heroku so that you don't have to pay for them when there is no work available. It has no bearing on the procfile or defining how to actually start a dj worker process.
as far as I know, there are 2 version of delayed_job:
original(tobi's) https://github.com/tobi/delayed_job
collectiveidea's fork: https://github.com/collectiveidea/delayed_job
when using the collectiveidea version, you should start it as below:
# Runs two workers in separate processes.
$ RAILS_ENV=production script/delayed_job -n 2 start
I am not familiar with delayed_job on Heroku, please follow its instructions.

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.

How to create a Heroku rails dev environment in actual development mode?

I'm attempting to mimic, as close as possible, the Heroku deployment environment but in actual development mode for a Rails app. In other words, I'd like more verbose output from the thin console logs web and worker processes and I'd like for the assets pipeline to be refreshed appropriately vs having to run a command to refresh them.
The reason I have to do this is due to some testing of additional workers that need to function during development and testing phases.
Currently I have foreman running a procfile locally which spawns thin. Here are the commands that it steps through:
First I start it via Foreman with RACK_ENV=development PORT=3000 foreman start --port $PORT
Second, in my Procfile I have:
`web: bundle exec thin start -p $PORT -e $RACK_ENV`
`worker: bundle exec ruby worker.rb`
These execute just fine, however I have two seeming problems that I'd like to overcome:
A) I have to run bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile so it seems as though thin does not honor a development mode that does not require asset precompilation. I tried to add config.assets.compile = true into my config/environments/development.rb, but that did not seemingly help the situation. The real problem seems to be that actual images in the assets folder are not refreshed without this manual preocompile step.
B) I am not seeing any more verbose development level logging in the output console. I would like to see a verbose request log as well as the debug print statements that I have in my worker script. None of these propagate back up to the console log where the foreman command is initially run.
The thought has come to mind that perhaps I should just have a Procfile.development and in there have webrick instead of thin, however that only resolves point A and leaves the question of point B above.
Thus my question, how can I accomplish my original designs using foreman + thin?
I ended up approaching this at a little different slant and got through the issue. The core problem I was having was the the logging facility was being overridden by one of gem's we've been using and therefore had to force logging to use the Rails logger with more verbosity:
How to increase Heroku log drain verbosity to include all Rails app details?

Resources