daemon_controller + Thinking Sphinx

by Michał Kuklis on 22/07/2009

I’ve created simple rails initiator in order to start Sphinx through daemon_controller based on the Thinking Sphinx configuration. I hope it will help somebody.

require 'daemon_controller'
 
def before_start
  if not ThinkingSphinx.define_indexes?
    config = ThinkingSphinx::Configuration.instance
    cmd = "#{config.bin_path}#{config.indexer_binary_name} --config \"#{config.config_file}\" --all"
    cmd << " --rotate" if ThinkingSphinx.sphinx_running?
    system cmd
  end
end
 
if defined?(ThinkingSphinx)
  if not ThinkingSphinx.sphinx_running? 
    conf_instance = ThinkingSphinx::Configuration.instance
    @controller = DaemonController.new(
      :identifier => 'Sphinx search server',
      :start_command => "#{conf_instance.bin_path}#{conf_instance.searchd_binary_name} --pidfile --config \"#{conf_instance.config_file}\"",
      :before_start => method(:before_start),
      :ping_command => lambda { TCPSocket.new(conf_instance.configuration.searchd.address, conf_instance.configuration.searchd.port) },
      :pid_file => conf_instance.configuration.searchd.pid_file,
      :log_file => conf_instance.configuration.searchd.log)
      @controller.start
  end
end

There are 4 comments in this article:

  1. 1/09/2009Yves says:

    Hi,

    thanks for the code. I love it. Much better than those examples from Phusion as you utilize the configuration object. Keep it up and thanks again :-)

  2. 2/09/2009Michał Kuklis says:

    Hi Yves, thanks for your comment. I’m glad you found it useful.

  3. 28/09/2009Jonathan says:

    Hi,

    Thanks a lot for this piece of code Michal, I was dreading having to figure it out by myself :)

  4. 25/04/2011Onno Faber says:

    Thanks for sharing the snippet.

    Add this line in the snipped after conf_instance gets declared, in order to build the configuration file in production mode automatically:
    conf_instance.build

    I’ve tried a lot, but this is not possible with capistrano, once you’ve set deamon_controller to handle starting the sphinx server.

Write a comment: