How to run auto restart in heroku for ruby scripts - ruby

In my Dev box on Nitrous, I am able to run God -c scripts.god -D to restart the two .rb files if they die.
I just run that and the processes for the most part stay alive.
But I cannot do the same in heroku. It seems when I run the god command the .god file does not open and generates an error in heroku.
Question:
How can I run God to restart failed processes in heroku as I do on my development Nitrous environment?
Or is there a recommended alternative way to watch heroku processes and restart them automatically when they fail?

On Heroku you shouldn't need to use a process supervisor like god. If all you need is to ensure your process is restarted if it crashes, Heroku can manage that fine.
It should be as simple as adding two entries in your procfile as workers. https://devcenter.heroku.com/articles/background-jobs-queueing
worker: bundle exec sidekiq
clock: bundle exec clockwork lib/clock.rb
slack_listener: bundle exec ruby lib/slack_bot.rb
You could possibly have issues, if your processing are crashing quite often. Dyno Crash Restart Policy

Your processes should start automatically when you access your website.
However, Heroku does provide commands to manage your processes, check out https://devcenter.heroku.com/articles/dynos for the complete list. E.g., to restart all processes, use the toolbelt command:
heroku ps:restart --app yourappname

Related

How can I run as root on Heroku?

I tried using sudo to run a command on Heroku. Then I get a message that I cannot use the sudo command on Heroku. My real question is: How can I run a command with root privilege on Heroku, because it is required for some of the commands I'm trying to run?
You cannot run as root on a Heroku Dyno, as Dynos are effectively containers, isolated from the host system. However, you should be able to install most packages via Buildpacks, either via first party supported buildpacks, thirdparty buildpacks, or via heroku-buildpack-apt. Attempting to alter any system files, will likely either not work, or will have unintended consequences.

Keep a Ruby project alive using Foreman

I have experience with Ruby on Rails but I'm trying to run Stringer the self-hosted RSS reader which is only Ruby. They suggest using Foreman to run the project which is fine, I can get the project up and running. However, for the life of me I cannot figure out how to keep the project running when I close the terminal.
I believe the Procfile is important in this process?? So here is mine.
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
console: bundle exec racksh
Please forgive my ignorance. I'm mostly an iOS developer.
Sounds like you just want to daemonize the process, in which case just run:
foreman start &

Best way to run rails background jobs with Openshift

I have a rails 3.2.13 application on OPENSHIFT. I need the best way to manage background jobs, for example daily, weekly or monthly search alerts (when users can subscribe to the search they made and get email alerts with new products).
I have installed REDIS cartridge according to these instructions.
There are OPENSHIFT cartridges available for Resque and Sidekiq, but installation commands do not work.
I managed to install Resque as a gem according to these instructions and it is working when I start it with the rake command:
RAILS_ENV=production bundle exec rake resque:work QUEUE='*'
But the worker is stopped as soon as I close command line tool. When I use action-hooks for the same rake task, it works only for some period of time and then I have to restart the worker again. How can I start a worker only once on production?
I think you would be best off creating rake tasks for what you need done and then using cron to fire them off when you need them to run.

heroku cedar console

I've been trying to get a heroku console session going on cedar and am having no joy.
The old way was:
heroku console
I understand the new way involves 2 steps:
heroku run bash
then
$ rails console
but at the heroku run bash stage i keep geting:
heroku run bash
Running `bash` attached to terminal... up, run.1
!
! Timeout awaiting process
so i checked there isn't a port blocking issue and
telnet rendezvous.heroku.com 5000
gives
Trying 50.19.103.36...
Connected to ec2-50-19-103-36.compute-1.amazonaws.com.
Escape character is '^]'.
which is as expected i think
what am i doing wrong and what do i need to do to get this console up and running?
Thanks!
PS I have tried - heroku run console - also, and that times out too.
In cedar every command that needs to be attached to terminal uses run.
ex:
heroku run console
heroku run rake db:migrate
When its not necessary to be attached don't use the run:
ex: heroku logs
The new way is heroku run console
Ok - it was obvious after a few hours of sleep. Simply had to update the heroku gem that had got corrupted somehow on my mac. If you have something like this problem, then do that first...

How do I create a resque worker automatically at bootup?

Ok, I'm making my first ruby app. Who know moving everything over to 'production' is so fugging complicated. So far I've struggled my way through configuring passenger, getting it to run on startup, then getting redis to run on startup.
My last task is on startup to add 1 worker. Right now, I have to ssh in and run my rake command rake workers:start. Obviously this is no good when I want to close ssh.. so I just dont really know how or what the next step is.
I tried copying resque default config to config.ru and it just blows up Passenger with errors. I also looked into resque-pool which some people mentioned but that is over my head.
all i have to do is add 1 worker on bootup. This isnt that serious of an app so simpler would be best at this point.
I don't use the god gem because (1) I've seen a project that was very thrashed by the complexity of setup it introduced, and (2) I'm personally really comfortable with the standard Linux (Ubuntu) tools that handle this kind of thing.
To start the Resque workers on bootup
I have this code in my /etc/rc.local file. I have a deploy user on the system:
# Start Resque
su -l deploy -c "/home/deploy/start-resque-workers"
su -l deploy -c "/home/deploy/start-resque-webui"
Then, in those scripts I set up the ruby environment and run the rake task:
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else
printf "ERROR: An RVM installation was not found.\n"
fi
# Use rvm to switch to the default ruby.
rvm use default
# Now launch the app
cd /home/deploy/app-name-here/current
nohup rake QUEUE=* RAILS_ENV=production environment resque:work &
I've been using this kind of set up for years, and it's solid. The servers don't crash. I don't yet need the overhead of installing another system (like the god gem) to watch over these other servers.
Additionally, I use a capistrano gem to handle restarting the workers on deploy.
In production you should be using god to watch your processes. Even if this project is a small one, I strongly recommend investing your time and setting it up.
Another big a must is Capistrano.
So, if you were using god, here's a config file that would help you.
You could also try scheduling rake resque:work at system startup, using a proper script in /etc/init.d/ or /etc/init/ or another (depends on what system you use). I tried this some time ago and I gave up (don't remember why).
I understand that this my answer isn't exactly what you're looking for right now. But imagine this: if everything is set up, then deploying next version is as easy as running rake deploy on your development machine. And it will take care of pulling your code from repository, running migrations, restarting workers and webservers and what not.

Resources