heroku running a daemon process - ruby

I'm trying to run a deamon process running on heroku.
The framework-less app is just a ruby script which after being launched monitors my twitter stream and if there is a tweet with a photo then publish it on a tumblr.
Locally it runs just fine, but when I run it on heroku it just crashes right away.
heroku[worker.1]: Starting process with command `bundle exec ruby twitter-to-tumblr.rb start`
heroku[worker.1]: State changed from starting to up
heroku[worker.1]: Process exited with status 0
heroku[worker.1]: State changed from up to crashed
I'm kind of a newbie with ruby and heroku and I'm missing something here, any help is really appreciated.
Thank you

You need to ensure that that parent process doesn't exit. Your code is most likely forking a daemon with a PID and then the parent process exits with status 0. A status of 0 means the program is finished and everything is OK. Heroku only knows that the process you asked it do finished and If you keep the parent process around it should work. The parent can then also handle signal processing for the child.
Based on your procfile command I'm guessing you are using the 'daemons' gem to handle the daemonization. You can keep the parent running by passing in the ontop option to the run command. This prevents the parent from exiting:
Daemons.run_proc('tweetzilla', {:ontop => true}) do
# => long running code here.
end

Related

Ubuntu run service in foreground

I've made a (docker) container for ddclient.
The problem is that I'm having problems in running that service in the foreground so that the docker container keeps running.
I've managed to keep the docker running by adding a bashat the end of the script but this is hackish, since the actual process it should be whatching is the ddclient.
Another way I found was to tail -f the log file, but if the service stops, the container will keep running instead of stoping.
Q: So is there any (easy) way to keep a service running in the foreground?
The problem with the process (any process) running in a container is signal management: you need to make sure the SIGKILL and other signals are properly communicated to the right process(es) in order to successfully stop/remove a container (and not leave zombie processes: see "PID 1 zombie reaping issue")
One option is at least to make your service at least write in a log file
ENTRYPOINT ["/bin/sh" "-c" ]
CMD yourProcess > log
That should keep it in foreground, as suggested in "How do I bring a daemon process to foreground?".
For a service, try and use as a base image phusion/baseimage-docker which manages other services properly.

Foreman exiting with code 1

I'm a complete heroku and nodejs newbie. I just followed a tutorial to get a GroupMe bot running locally with heroku. After running the command foreman start, I receive the following:
18:10:00 web.1 | started with pid 24985
18:10:00 web.1 | exited with code 1
18:10:00 system | sending SIGTERM to all processes
The procfile reads:
web: node index.js
How do I prevent exiting with code 1 above?
You might see that message if you don't have node installed on your local machine. You can check by typing
node --version
The Getting Started with Node.js on Heroku tutorial at https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction might be helpful. It walks you through deploying a Node.js app step-by-step.
Try running the program manually:
node index.js
This will show you the actual errors you have.

Ruby daemons on Heroku

I'm trying to execute a ruby daemon framework-less app on Heroku, but it crash after trying to execute the worker:
heroku[worker.1]: State changed from crashed to starting
heroku[worker.1]: Starting process with command `bundle exec rake twitter:start`
heroku[worker.1]: State changed from starting to up
heroku[worker.1]: Process exited with status 0
heroku[worker.1]: State changed from up to crashed
The app basically collects data, through a bunch of APIs, and saves it on a remote MongoHQ instance.
The rake tasks are:
dir_path = File.expand_path('../', __FILE__)
namespace :twitter do
desc 'Start Twitter daemon'
task :start do
exec "#{dir_path}/bin/autana start"
end
...
end
The Procfile is very simple: worker: bundle exec rake twitter:start
The code that executes the daemon is the following:
#!/usr/bin/env ruby
require 'daemons'
require File.expand_path('../../lib/autana', __FILE__)
Daemons.run_proc('autana', multiple: true, no_pidfiles: true) do
client = Autana::Client::Twitter.new
client.collect
end
Any ideas?
Can you run it dynamically?
heroku run rake twitter:start
So, it was a MongoDB error. Data was not properly associated. After entering the console, and changed the document, everything worked as expected.

start daemon on remote server via Jenkins SSH shell script exits mysteriously

I have a build job on jenkins that is building my project and after it is done, it opens an ssh shell script on a remote server and transfers files and then stop and starts a daemon.
When I stop and start the daemon from the command line on a RHEL server, it executes just fine. When the job executes in jenkins, there are no errors.
The daemon stops fine and it starts fine. But shortly after starting, the daemon dies suddenly.
sudo service daemonName stop
# transfer files.
sudo service daemonName start
I'm sure that the problem isn't pathing
Does anyone know what could be special about the way Jenkins is executing the ssh shell script that would cause the daemon start to not fully complete?
The problem:
When executing a build through jenkins, the command to start the daemon process was clearly successfully executing, yet after the build job was done, the daemon would suddenly quit.
The solution:
I thought for this whole time that it was jenkins killing the daemon. So I tried many different incarnations and permutations of disabling the ProcessTree module that goes through and cleans up zombie child processes. I tried fooling it by resetting the BUILD_ID environment variable. Nothing worked.
Thanks to this thread I found out that that solution only works for child processes executed on the BUILD machine. I.E. not applicable to my problem.
More searching led me here: Run a persistent process via ssh
The solution? Nohup.
So now the build successfully restarts the daemon by executing the following:
sudo nohup service daemonname start
Jenkins watches for processes spawned by the job and kill them to avoid zombie processes.
See https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller
The workaround is to override the BUILD_ID environment variable:
BUILD_ID=dontKillMe

Heroku scheduler crashing

I'm having trouble with the scheduler add-on on Heroku. I had it running fine for a long time but now it is stuck in a loop of crashing and restarting.
I've tried stopping the process by running heroku ps:stop scheduler and I've removed the scheduler add-on but it still keeps restarting.
The task it was running was rake:cron. I've since emptied out that task so it's not actually doing anything but it still crashes after running it with the following message:
heroku[scheduler.5818]: State changed from up to crashed
heroku[scheduler.5818]: State changed from crashed to down
heroku[scheduler.5818]: State changed from down to starting
heroku[scheduler.5818]: Starting process with command `bundle exec rake cron`
heroku[scheduler.5818]: State changed from starting to up
heroku[scheduler.5818]: Process exited with status 0
heroku[scheduler.5818]: State changed from up to crashed
heroku[scheduler.5818]: State changed from crashed to starting
Is there any other way to force kill a process on Heroku? I'm worried I'm being billed for the constantly running/crashed scheduler.

Resources