update thinking sphinx index in ruby code
Asked Answered
R

2

7

I have app, which run's on ubuntu 12.04, with nginx+passenger And in my method i try to rebuild sphinx index so:

  def update_sphinx_index
    Rails.application.load_tasks
    Rake::Task['ts:rebuild'].invoke
    redirect_to admin_mainpage_path
  end

also i try:

`rake ts:index`

but nothing happend, sphinx index doesn't rebuild. What i do wrong? Maybe i need to do this with some privilegies, or something else, becouse when i do rake ts:rebuild in command terminal all is fine, and index rebuild.

Raeleneraf answered 14/11, 2013 at 12:42 Comment(14)
may be you need set RAILS_ENV?Educatory
@Monk_Code what do you mean? code? Rake::Task['ts:rebuild RAILS_ENV=production'].invokeRaeleneraf
I indexation during limited hours following command bundle exec rake ts:index RAILS_ENV=staging on stagingEducatory
@Monk_Code write all command code, which is in ruby fileRaeleneraf
but i think this bad practice reindex on controller.Educatory
@Monk_Code and which is good?Raeleneraf
let us continue this discussion in chatEducatory
Why do you need to update the index so often?Indic
What happens if you run `rake ts:index` from rails console?Custommade
@np_ pastebin.com/3huSNetQRaeleneraf
What is the output of `rake ts:index` when you run it from within your app? For example, in a view, do <%=`rake ts:index`%>.Custommade
@np_ i get pastebin.com/gXGDTHVa with =cd /home/prog/OnlineAuto/Shop rake ts:rebuild i get empty with rebuild pastebin.com/9scaKzgWRaeleneraf
@brabertaser1992 I did not -1 your question. I +1'd because someone else gave you -1 for no good reason. But for that extreme reaction don't expect any help.Coburn
@Coburn really sorry...Raeleneraf
C
4

First of all, I don't really like the idea of calling sphinx reindex from a controller. The best practice here would be to use deltas for partial indexing (deltas with resque are a great solution for almost real time indexing) and then an occasional reindex once in a while to compact the files.

But if for any reason you really need to run this from the controller, I would say there are two things to consider. One is you are probably not on the right directory, so you can try to issue the system call changing to the directory then executing rake, as in

`cd /path/to/your/app rake ts:index`

Apart from this, if you are following best practices, the user executing your web server will be an underprivileged user, something like a user belonging to the www-data group. It's very likely this user won't have permissions to execute rake or to write the index files, so make sure the user starting your web server has the right permissions.

Correspondent answered 10/12, 2013 at 1:20 Comment(2)
and how can i change thid permission's? i use nginx + passengerRaeleneraf
first you need to identify which permission issues you have. If your problem is you can't write the files, you can just make the index files available for any user in your system, as they are not really a security risk. If your problem is you can't execute rake, then you should add group permissions with chmod and make sure the user starting your web server is a part of the same group.Correspondent
P
3

ts:rebuild stops sphinx, rebuilds the index and then restarts, and should only be run if you've changed the structure of your index.

Instead, you should be running ts:index if you're only adding new data.

As for why the rake task is not executing, it may be that your search daemon is still running.

Price answered 16/11, 2013 at 13:8 Comment(10)
As for why the rake task is not executing, it may be that your search daemon is still running. how to be thanRaeleneraf
You could create another task to kill the searchd process, and run it before calling ts:rebuild. See here for an example of such a task.Price
how about Rails.application.load_tasks Rake::Task['ts:stop'].invoke Rake::Task['ts:index'].invoke Rake::Task['ts:start'].invoke ?Raeleneraf
maybe there are any ideas?Raeleneraf
Did you try using ts:index instead? Or killing the searchd process before running ts:rebuild?Price
first yes, killing noRaeleneraf
so first i need to create task with code: task :kill_searchd do run 'ps -ef | grep "searchd.*<application/folder>" | grep -v grep | awk \'{print $2}\' | xargs kill' end and then run it in controller before "thinking_sphinx:rebuild", "kill_searchd" ?Raeleneraf
Yeah something similar to that.Price
i try with killing process, and it didn't helpRaeleneraf
what else could you propose?Raeleneraf

© 2022 - 2024 — McMap. All rights reserved.