How to Daemonize Rails script with bluepill

I have always successfully used bluepill to demonize simple Ruby scripts. However, this time I have a script that also loads the Rails environment so that I can access my database connection for the Rails application and its corresponding models. The bluepill configuration that I use is no different from what I usually 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 problem 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 the same as with a bunch of my other scripts. However, this is the first time I am trying to demonize a script loading a Rails environment. I know that I can use the demons of ruby ​​gems to make it work, but it does not monitor, and bluepill is able to perform both of these actions very well.

Am I missing something obvious here?

+4
source share
4 answers

Signal code from 0 to kill requests to determine if the daemon is susceptible to signals. A Bluepill source shows that this happens often after spawning to check if the demon process is alive and well.

Since the process no longer exists, it is likely that the ruby ​​dies when the environment boots.

You did not specify your script. I assume that it works fine from the command line, but cannot be demonized. The likely explanation is that there is something missing from the Bluepill process in your shell environment. Another possibility is access to the resources that your interactive shell has, but the process without heads does not work.

Here is one hunch: for require , which you gave to work, I believe that the RAILS_ENV environment RAILS_ENV should be set. You do it? See for example this note . It might be better to boot using the download script. See, for example, Rails initialization .

+4
source

Did you know that you can run a script in a rails environment using rails runner ? You can try this.

I was not lucky with bluepill, I had much more success with the eye: https://github.com/kostya/eye

You can check this, it has the same syntax as bluepill.

+3
source

Hm. We tried a little bluepill, but in many cases it was not very useful when the processes had complex startups. Recently, we were happy with "runit", which has several related components (for example, chpset for user installation, environment, etc.), which are very useful. We use devops utilities, such as a chef, to install machines with standard services, and runit is better suited. But with any of them, you need the pid of the current process, which you need to receive signals from the monitoring system - this is similar to the PID that you get from your command:

/media/apps/myapp/current/lib/async/myscript.rb

not the one that continues to work - maybe if you post more of this script, we will see what happens, but I guess somewhere it postpones another process?

+2
source

on this site https://github.com/arya/bluepill/issues/164 I found this information

After some experimentation, I found that if you run commands with the -no-privileged flag, as well as the basic dir and logfile flags, this works. They are also included in the configuration, so it is a bit redundant: /

Command example: bundle exec bluepill load bluepill / monitorbs.pill --no-privileged --base-dir / xxx / xxx / xxx --logfile / xxx / xxx / xxx / bluepill / bs.log "

0
source

Source: https://habr.com/ru/post/1502697/


All Articles