1919# limitations under the License.
2020
2121require 'rubygems'
22+ require 'daemons'
2223require 'optparse'
2324require 'chef'
2425require 'facter'
2526
27+ trap ( "INT" ) { Chef ::Log . info ( "SIGINT received, stopping" ) ; exit }
28+
2629config = {
2730 :config_file => "/etc/chef/server.rb" ,
28- :log_level => :info
31+ :log_level => :info ,
2932}
3033opts = OptionParser . new do |opts |
3134 opts . banner = "Usage: #{ $0} [-d DIR|-r FILE] (options)"
3235 opts . on ( "-c CONFIG" , "--config CONFIG" , "The Chef Config file to use" ) do |c |
3336 config [ :config_file ] = c
3437 end
38+ opts . on_tail ( "-d" , "--daemonize" , "Daemonize the process" ) do
39+ config [ :daemonize ] = true
40+ end
3541 opts . on_tail ( "-l LEVEL" , "--loglevel LEVEL" , "Set the log level (debug, info, warn, error, fatal)" ) do |l |
3642 config [ :log_level ] = l . to_sym
3743 end
@@ -54,32 +60,28 @@ if config[:log_level]
5460 Chef ::Log . level ( config [ :log_level ] . to_sym )
5561end
5662
63+ # daemonotron!
64+ Daemons . daemonize ( :ontop => !config [ :daemonize ] )
65+
5766# Get a Chef::SearchIndex object
5867indexer = Chef ::SearchIndex . new
5968Chef ::Queue . connect
6069Chef ::Queue . subscribe ( :queue , "index" )
6170Chef ::Queue . subscribe ( :queue , "remove" )
62- while 1
63- begin
64- object , headers = Chef ::Queue . receive_msg
65- Chef ::Log . info ( "Headers #{ headers . inspect } " )
66- if headers [ "destination" ] == "/queue/chef/index"
67- start_timer = Time . new
68- indexer . add ( object )
69- indexer . commit
70- final_timer = Time . new
71- Chef ::Log . info ( "Indexed object from #{ headers [ 'destination' ] } in #{ final_timer - start_timer } seconds" )
72- elsif headers [ "destination" ] == "/queue/chef/remove"
73- start_timer = Time . new
74- indexer . delete ( object )
75- indexer . commit
76- final_timer = Time . new
77- Chef ::Log . info ( "Removed object from #{ headers [ 'destination' ] } in #{ final_timer - start_timer } seconds" )
78- end
79- rescue Exception => e
80- if e . kind_of? ( Interrupt )
81- raise e
82- end
83- Chef ::Log . error ( "Received Exception: #{ e } \n #{ e . backtrace . join ( "\n " ) } continuing" )
71+ loop do
72+ object , headers = Chef ::Queue . receive_msg
73+ Chef ::Log . info ( "Headers #{ headers . inspect } " )
74+ if headers [ "destination" ] == "/queue/chef/index"
75+ start_timer = Time . new
76+ indexer . add ( object )
77+ indexer . commit
78+ final_timer = Time . new
79+ Chef ::Log . info ( "Indexed object from #{ headers [ 'destination' ] } in #{ final_timer - start_timer } seconds" )
80+ elsif headers [ "destination" ] == "/queue/chef/remove"
81+ start_timer = Time . new
82+ indexer . delete ( object )
83+ indexer . commit
84+ final_timer = Time . new
85+ Chef ::Log . info ( "Removed object from #{ headers [ 'destination' ] } in #{ final_timer - start_timer } seconds" )
8486 end
8587end
0 commit comments