2

I am trying to create a custom daemon to run every 60 seconds and start / stop and monitor it with bluepill.

Can someone show me how to do this easier or please can you tell me what I am doing wrong?

Currently bluepill is starting the daemon so that it runs but seems to then not know its running as keep trying to stop and start it.

Here is my code from my .pill file

app.process("get_dropboxes") do |process|

   process.working_dir = RAILS_ROOT
   process.pid_file = File.join(RAILS_ROOT, "tmp", "pids", "get_dropbox.pid")  
   process.start_command = "/usr/bin/env RAILS_ENV=#{RAILS_ENV} bundle exec lib/daemons/get_dropbox_ctl start"
   process.stop_command = "/usr/bin/env RAILS_ENV=#{RAILS_ENV} bundle exec lib/daemons/get_dropbox_ctl stop"

   process.start_grace_time = 10.seconds
   process.stop_grace_time = 10.seconds
   process.restart_grace_time = 10.seconds

   process.checks :cpu_usage, :every => 30.seconds, :below => 20, :times => [3,5]
   process.checks :mem_usage, :every => 30.seconds, :below => 350.megabytes, :times => [3,5]

 end

here is my get_dropbox_ctl file

#!/usr/bin/env ruby
require 'rubygems'
require "daemons"
require 'yaml'
require 'erb'

gem 'activesupport', '>=3.0.0'
require 'active_support'

# For some reason, ActiveSupport 3.0.0 doesn't load.
# Load needed extension directly for now.
require "active_support/core_ext/object"
require "active_support/core_ext/hash"

options = YAML.load(
  ERB.new(
    IO.read(
      File.dirname(__FILE__) + "/../../config/daemons.yml"
  )).result).with_indifferent_access

options[:dir_mode] = options[:dir_mode].to_sym

Daemons.run File.dirname(__FILE__) + "/get_dropbox.rb", options

And here is my get_dropbox.rb file

#!/usr/bin/env ruby

# You might want to change this
ENV["RAILS_ENV"] ||= "production"

require 'net/pop'
require File.dirname(__FILE__) + "/../../config/application"
Rails.application.require_environment!

$running = true
Signal.trap("TERM") do 
  $running = false
end

while($running) do


  # do stuff .......

  sleep 60
end

Logs I get

[2011-07-13T16:55:00.464202 #32257]  WARN -- : [domain.com:get_dropboxes] pid_file /var/www/domain.com/current/tmp/pids/get_dropbox.pid does not exist or cannot be read
W, [2011-07-13T16:55:00.464315 #32257]  WARN -- : [domain.com:get_dropboxes] pid_file /var/www/domain.com/current/tmp/pids/get_dropbox.pid does not exist or cannot be read
W, [2011-07-13T16:55:00.464505 #32257]  WARN -- : [domain.com:get_dropboxes] Executing start command: /usr/bin/env RAILS_ENV=production bundle exec lib/daemons/get_dropbox_ctl start
I, [2011-07-13T16:55:01.602210 #32257]  INFO -- : [domain.com:get_dropboxes] Going from down => starting

Surely there is an easier way than this ?

halfer
  • 19,824
  • 17
  • 99
  • 186
rick
  • 463
  • 5
  • 23
  • Is there a reason why you don't do this with a cron job? – moritz Jul 13 '11 at 17:58
  • i need to monitor it and auto restart it if it goes down and i am using bluepill to monitor other things so just thought it was best option ??? or am i wrong ?? – rick Jul 13 '11 at 23:20
  • That depends, I think: When you want a job done every 60 seconds, cron is a good option, because it will not "drift" if your actual job takes a long tike (say 5 seconds). The 60 second sleep suggests that this is closer to your scenario than a real daemonized process? – moritz Jul 14 '11 at 12:07
  • its pulling email in so just need to keep going and as i have bluepill setup already seems like easier option than to also have to monitor cron. also if cron takes longer than 60 seconds and triues to start again things can go wrong!! do you know how to do this via a deaemon ? – rick Jul 14 '11 at 13:36
  • I understand and I agree. Could there be some kind of permissions problem with the pid file? – moritz Jul 14 '11 at 15:51
  • is there a specific place the pid file should go / be ? – rick Jul 15 '11 at 08:34
  • No, you can configure that and it seems like you did. But the place should be readable by bluepill and writable by the control script. – moritz Jul 15 '11 at 09:49

1 Answers1

0

I am not sure if this is what you are looking for, but as far as I know, you can skip the daemon file and use bluepill for that. As long as you specify a pidfile you can set process.daemonize = true and it will create a daemon for you.

namxam
  • 96
  • 5
  • thanks so what would be the process.start_command command to start a ruby script with the rails environment ? or would this just be a rake task ? – rick Jul 15 '11 at 13:41
  • Just use the rake task and it should work. If you do not specify the stop task, it ends the process by using process signals (usually just works). – namxam Aug 15 '11 at 17:10
  • Yup, for any process that runs in the foreground, specify `process.daemonize` to bluepill and things will be sweet. If the process self-daemonizes, the process *must* generate it's own PID file and you *must* tell bluepill where that pid file can be found after the `grace_start_time` has passed. – d11wtq Sep 16 '11 at 11:43