Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Daemonize a Rails script with bluepill

I've always used bluepill successfully to daemonize simple Ruby scripts. This time however, I have a script that's also loading a Rails environment so I can access my database connection for the Rails app and its respective models. The bluepill config I use is no different than what I normally do:

   Bluepill.application("myapp", :foreground => true, :log_file => "/tmp/bluepill.log") do |app|
          app.process("myapp_process") do |process|
            process.start_command = "/usr/local/rvm/rubies/ruby-1.9.3-p194/bin/ruby /media/apps/myapp/current/lib/async/myscript.rb"
            process.pid_file = "/media/apps/myapp/current/tmp/pids/myscript.pid"
            process.daemonize = true
            process.stdout = "/var/log/myapp/media.log"
            process.stderr = "/var/log/myapp/media_error.log"
            process.working_dir = "/tmp"
            process.stop_command = "kill -QUIT {{PID}}"
            process.start_grace_time = 15.seconds
          end
    end

The main issue is this error:

Failed to signal process 16096 with code 0: No such process

If I do not load the Rails environment using this:

require File.expand_path("/media/apps/myapp/current/config/environment")

This will work as it does with a bunch of my other scripts. This is the first time I'm trying to daemonize a script that loads the Rails environment, however. I know I can use the ruby gem Daemons to get this to work, but that doesn't do monitoring and bluepill is capable of doing both really well.

Am I missing something obvious here?

like image 333
randombits Avatar asked Jan 13 '23 08:01

randombits


1 Answers

Signal code 0 to kill queries to determine if the daemon is receptive to signals. The Bluepill source shows this is done frequently after spawning to check if the daemon process is alive and well.

Since the process is no longer there, it's likely that ruby is dying while loading the environment.

You didn't show your script. I'm guessing that it runs okay from the command line but fails to daemonize. A probable explanation is that there's something in your shell's environment that's missing in the Bluepill process's. Another possibility is access to resources that your interactive shell has, but the headless daemon process does not.

Here's one guess: For the require you gave to work, I believe the RAILS_ENV environment variable must be set. Are you doing that? See for example this note. Perhaps it's better to load using the boot script. See for example the Rails initialization description.

like image 112
Gene Avatar answered Jan 22 '23 00:01

Gene