byCoder
byCoder

Reputation: 9184

start thinking sphinx on rails server startup

I have a chain of nginx + passenger for my rails app.

Now after each server restart i need to write in terminal in project folder

rake ts:start

but how can i automatize it?

So that after each server restart thinking sphinx is automatically started without my command in terminal?

I use rails 3.2.8 and ubuntu 12.04.

I can not imagine what can i try ever, please help me.

How can i do this, give some advices?

Upvotes: 1

Views: 1317

Answers (4)

smoyth
smoyth

Reputation: 709

If you are using the excellent whenever gem to manage your crontab, you can just put

every :reboot do
  rake "ts:start"
end

in your schedule.rb and it seems to work great. I just tested on an EC2 instance running Ubuntu 14.04.

Upvotes: 2

declan
declan

Reputation: 5635

I followed @pat's suggestion and wrote a script to start ThinkingSphinx whenever the server boots up. You can see it as a gist - https://gist.github.com/declan/4b7cc4fb4926df16f54c

We're using Capistrano for deployment to Ubuntu 14.04, and you may need to modify the path and user name to match your server setup. Otherwise, all you need to do is

  • Put this script into /etc/init.d/thinking_sphinx
  • Confirm that the script works: calling /etc/init.d/thinking_sphinx start on the command line should start ThinkingSphinx for your app, and /etc/init.d/thinking_sphinx stop should stop it
  • Tell Ubuntu to run this script automatically on startup: update-rc.d thinking_sphinx defaults

There's a good post on debian-administration.org called making scripts run at boot time that has more details.

Upvotes: 1

troelskn
troelskn

Reputation: 117567

What I did to solve the same problem:

In config/application.rb, add:

module Rails
  def self.rake?
    !!@rake
  end

  def self.rake=(value)
    @rake = !!value
  end
end

In Rakefile, add this line:

Rails.rake = true

Finally, in config/initializers/start_thinking_sphinx.rb put:

unless Rails.rake?
  begin
    # Prope ts connection
    ThinkingSphinx.search "test", :populate => true
  rescue Mysql2::Error => err
    puts ">>> ThinkingSphinx is unavailable. Trying to start .."
    MyApp::Application.load_tasks
    Rake::Task['ts:start'].invoke
  end
end

(Replace MyApp above with your app's name)

Seems to work so far, but if I encounter any issues I'll post back here.

Obviously, the above doesn't take care of monitoring that the server stays up. You might want to do that separately. Or an alternative could be to manage the service with Upstart.

Upvotes: 3

pat
pat

Reputation: 16226

There's two options I can think of.

  • You could look at how Ubuntu manages start-up scripts and add one for this (perhaps in /etc/init?).
  • You could set up monit or another monitoring tool and have it keep Sphinx running. Monit should boot automatically when your server restarts, and so it should ensure Sphinx (and anything else it's tracking) is running.

The catch with Monit and other such tools is that when you deliberately stop Sphinx (say, to update configuration structure and corresponding index changes), it might start it up again before it's appropriate. So I think you should start with the first of these two options - I just don't know a great deal about the finer points of that approach.

Upvotes: 1

Related Questions