Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework enqueuing system to ensure records got deleted accordingly #75

Open
chaadow opened this issue Aug 12, 2015 · 10 comments
Open

Rework enqueuing system to ensure records got deleted accordingly #75

chaadow opened this issue Aug 12, 2015 · 10 comments
Labels

Comments

@chaadow
Copy link

chaadow commented Aug 12, 2015

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 : collectiveidea/delayed_job#820
Is there any workaround to this, since I want to remove my model asynchronously from Algolia only after my project has been deleted

@redox
Copy link
Contributor

redox commented Aug 12, 2015

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

@chaadow
Copy link
Author

chaadow commented Aug 13, 2015

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

@chaadow
Copy link
Author

chaadow commented Aug 13, 2015

@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

@redox
Copy link
Contributor

redox commented Aug 13, 2015

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 :/

@chaadow
Copy link
Author

chaadow commented Aug 13, 2015

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

@chaadow
Copy link
Author

chaadow commented Aug 14, 2015

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

@redox
Copy link
Contributor

redox commented Aug 14, 2015

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!.

@chaadow
Copy link
Author

chaadow commented Aug 17, 2015

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 ?

@chrisyeung1121
Copy link

+1. I am having the same problem.

@redox redox changed the title Error encountered while auto_delete with DelayedJob Rework enqueuing system to ensure records got deleted accordingly Apr 24, 2016
@redox redox added the bug label Apr 24, 2016
@drewtunney
Copy link

drewtunney commented Jun 10, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants