Hi James

Here's how I'd structure it:

  def index
    @search_results = CustomMenu.search params[:search],
      :include  => :custom_page,
      :populate => true
    flash[:notice] = 'Sorry, no search results found' if @search_results.empty?
  rescue Riddle::ConnectionError
    raise if @retried
    ThinkingSphinx::Configuration.instance.controller.start
    @retried = true
    retry
  end

A couple of things to note:
* :populate forces the search to happen immediately. Thinking Sphinx defaults 
to lazy loading, like ActiveRecord, to allow chaining of scopes without 
unnecessary queries.
* let's only retry once - don't want to end up in an endless loop if something 
goes pearshaped.
* Using the controller from TS's configuration object, you can control Sphinx. 
This is what the rake task uses under the hood anyway, so let's not shell out 
to rake.
* Not sure if blank? is standard Ruby, and while a TS result set emulates an 
Array, I'm not sure if it emulates an Array that has Rails injections. #empty? 
fits better from a language perspective anyway in this case :)

I've not tested this code, but hopefully it's helpful :)

-- 
Pat

On 05/03/2011, at 1:23 AM, jamesw wrote:

> To reduce server load I'm wanting to start sphinx on demand as I have
> over 80 websites all wanting to use sphinx but usually only 10 active
> at any one time.
> Obviously if ts is not running I get a connection refused exception
> from riddle so all I need to do is trap that exception and start the
> server.
> To achieve this I have the following in a searches controller.
>  def index
>    @search_results = CustomMenu.search(params[:search], :include
> => :custom_page)
>    begin
>      if @search_results.blank?
>        flash[:notice] = 'Sorry, no search results found'
>      end
>    rescue Riddle::ConnectionError
>      start_search_engine
>      @search_results = CustomMenu.search(params[:search], :include
> => :custom_page)
>    end
>  end
> 
>  def start_search_engine
>    Search.start_sphinx
>  end
> 
> and the search module looks like this
> class Search
>  def self.start_sphinx
>    "rake thinking_sphinx:rebuild RAILS_ENV=#{Rails.env}"
>    res = "rake thinking_sphinx:start RAILS_ENV=#{Rails.env}"
>    puts("@@@@ Start sphinx #{res}")
>  end
> end
> However the start_sphinx method does not actually work though the
> output returned in the puts statement shows
> @@@@ Start sphinx rake thinking_sphinx:start RAILS_ENV=development but
> the connection is refused in the view.
> 
> Sphinx will start if I run the rake task from the command line.
> 
> Any ideas as to what the best solution for this problem might be would
> be greatly appreciated.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Thinking Sphinx" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/thinking-sphinx?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/thinking-sphinx?hl=en.

Reply via email to