Search code examples
delayed-jobthinking-sphinx

TS delayed delta - check for finished job


Is there a way to check whether a TS delayed delta job has finished? I have a scenario in which I need to run a new search in an after_save callback and of course I'd like to see the changes to the delta index reflected in the search results.

Here are some details of my example:

I have a model called Feature which has many annotations (Annotation model). The index looks like this:

define_index do
  indexes annotations.value, :as => :annotations

  # other indexes
  set_property :delta => :delayed
end

When the "value" of an annotation changes I update the delta attribute of the associated feature in an Annotation model callback. Setting the delta attribute to true spins off a delayed_job task to update the delta index. In a separate callback I'd like to perform a new search against the updated delta index, but I noticed that the search never reflects the current state of the index. This is no doubt because the delta jobs are not finished yet.

What would be the best strategy to deal with this these timing issues?


Solution

  • I can think of only one. to query the delayed_jobs table

    ActiveRecord::Base.connection.execute("select count(1) from delayed_jobs where handler like '%%'")
    

    If the job has succeeded, the entry is sure to have gone. This is the only way i can think of. Or disable delayed delta for this model alone if its not a big deal.