8

Using Ruby v1.8.7 and Daemons v1.1.8 on Mac OS X Lion, I am attempting to write a consumer process and get it run as a dameon:

# config[:name] => 'idx_my_delete_consumer'
# config[:daemon] => {:multiple => false,
#                    :backtrace => true, 
#                    :dir_mode => :normal, 
#                    :log_dir => '/Users/pprakash/consumer.log',
#                    :monitor => true,
#                    :dir => '/Users/pprakash/pids'}

 Daemons.run_proc(config[:name], config[:daemon]) do
    consumer = MyConsumer.new(config)  
    consumer.subscribe
  end

However, it does not start and instead throws a long traceback, which goes something like this:

E, [2012-05-28T19:34:16.199770 #29357] ERROR -- : Bad file descriptor (Errno::EBADF)
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/daemonize.rb:134:in `for_fd'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/daemonize.rb:134:in `close_io'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/daemonize.rb:134:in `initialize'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/daemonize.rb:134:in `new'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/daemonize.rb:134:in `close_io'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/daemonize.rb:75:in `call_as_daemon'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:258:in `start_proc'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:295:in `start'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:51:in `watch'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:51:in `fork'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:51:in `watch'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:45:in `each'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:45:in `watch'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:44:in `loop'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:44:in `watch'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:84:in `start_with_pidfile'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:64:in `fork'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:64:in `start_with_pidfile'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/monitor.rb:111:in `start'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/application_group.rb:149:in `create_monitor'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:284:in `start'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/controller.rb:70:in `run'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:197:in `run_proc'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `call'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `catch_exceptions'
/opt/local/lib/ruby/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:196:in `run_proc'
users/delete_consumer.rb:40

I am not sure what is causing this issue? The directory name, log file name are all valid. I am able to create an instance of MyConsumer with these config and able to execute its #subscribe properly from a standalone program/console.

pr4n
  • 2,918
  • 3
  • 30
  • 42
  • 3
    So, after a gaining some experience with Ruby Daemons, I have came to realize that any such error implies that the underlying block (which is demonized) has errors. Fixing all the errors in the underlying block fixes this error as well. – pr4n Jun 19 '12 at 06:31
  • 2
    To help others out, answer your own question, so that it has a formal answer. – Jordan Dea-Mattson Feb 26 '13 at 05:05
  • To be able to get better support and have a better environment, I strongly advise that you upgrade your local Ruby to 1.9.3 vs. the much older 1.8.7 distributed with Mac OS X. – Jordan Dea-Mattson Feb 26 '13 at 05:06

2 Answers2

4

Based on my experiences with Ruby Daemons, I found that such errors indicate that the underlying block (which is daemonized) contains errors. Fixing those errors fixes this error as well.

pr4n
  • 2,918
  • 3
  • 30
  • 42
  • In my case, the problem was that the library I was calling in the daemonized loop wrote to stdout by default, in addition to what I had explicitly told it to do. But a daemon has no stdout! Thus, it raised the bad file descriptor exception. – Al Chou Feb 05 '14 at 19:26
0

You do have typos in your example that may be causing the error. Check the spelling for MyConsumer, which you transposed the s and n...

consumer = MyCosnumer.new(config) 
Michael Lang
  • 1,028
  • 12
  • 21
  • Thanks for pointing the typo. It may have arrived here while I was posting the question. Edited and removed it from the question – pr4n Mar 28 '13 at 08:07