algoliasearch-rails icon indicating copy to clipboard operation
algoliasearch-rails copied to clipboard

Rework enqueuing system to ensure records got deleted accordingly

Open chaadow opened this issue 9 years ago • 10 comments

Hi, I tried this snippet of code as suggested in the readme for enqueuing Algolia operations :

class Contact < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch enqueue: :trigger_delayed_job do
    attribute :first_name, :last_name, :email
  end

  def self.trigger_delayed_job(record, remove)
    if remove
      record.delay.remove_from_index!
    else
      record.delay.index!
    end
  end
end

My application is running rails 3.2.22 with ruby 2.2.2 and DelayedJob 4.0.6

Without delayed job, the auto_remove works perfectly fine. However, when I try to remove the project with the enqueuing system set, I get the following error :

ArgumentError:
       job cannot be created for non-persisted record:

after further research, I found this DJ related issue : https://github.com/collectiveidea/delayed_job/issues/820 Is there any workaround to this, since I want to remove my model asynchronously from Algolia only after my project has been deleted

chaadow avatar Aug 12 '15 10:08 chaadow

That's weird, do you have the associated backtrace? Is it raised from the record.delay.{remove_from_,}index! methods?

redox avatar Aug 12 '15 17:08 redox

Yes it is raised from record.delay.remove_from_index! When I destroy my model. For example : project.destroy

chaadow avatar Aug 13 '15 12:08 chaadow

@redox Here is an example of a failing spec backtrace :

 Failure/Error: expect { delete :destroy, :id => @project }.to change(Project, :count).by(-1)
     ArgumentError:
       job cannot be created for non-persisted record: #<Project

chaadow avatar Aug 13 '15 12:08 chaadow

Can you just try to run a record.remove_from_index! and another record.delay.remove_from_index! from the rails console, to check if you're experiencing the same? I'm don't get it right now :/

redox avatar Aug 13 '15 15:08 redox

record.remove_from_index! works whereas record.delay.remove_from_index! doesn't.

chaadow avatar Aug 13 '15 22:08 chaadow

Actually, it doesn't work only after I destroy the record and the self.trigger_delayed_job is triggered. Since it's a "remove" it steps in the if remove condition, and finally the exception I told you about above is raised. When I try without the "delay" it works whereas with the delay it doesn't and it displays this error :

ArgumentError:
       job cannot be created for non-persisted record: #<Project

chaadow avatar Aug 14 '15 07:08 chaadow

Could you just try with another (non-algolia) method? From what I understand, your Project object cannot be serialized but I don't get why it could be related to the remove_from_index!.

redox avatar Aug 14 '15 20:08 redox

I am only using what's suggested in the readme for delayedjob. record.delay.remove_from_index! works outside the self.trigger_delayed_job, if I try it in the console. However when I try it after a destroy, for instance : project.destroy, then as I explained,record.delay.remove_from_index! does not work anymore. Could you try that to see maybe you have to update the example you're suggesting in the readme ?

chaadow avatar Aug 17 '15 11:08 chaadow

+1. I am having the same problem.

chrisyeung1121 avatar Oct 27 '15 09:10 chrisyeung1121

I ran into the same issue. The workaround I've used is to delete the record by ID from the index in the background job. It seems to work fine

   def trigger_delayed_job(record, remove)
      if remove
        index = Algolia::Index.new("#{self.name}_#{Rails.env}")
        index.delete_object(record.uuid)
      else
        obj = self.find(record.id)
        obj.index!
      end
    end

drewtunney avatar Jun 10 '16 14:06 drewtunney