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

README enqueuing instructions are... a bit confusing #102

Closed
vincentwoo opened this issue Feb 14, 2016 · 5 comments
Closed

README enqueuing instructions are... a bit confusing #102

vincentwoo opened this issue Feb 14, 2016 · 5 comments

Comments

@vincentwoo
Copy link

For example, read the snippet for explaining how to trigger sidekiq queueing:

class Contact < ActiveRecord::Base
  include AlgoliaSearch

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

  def self.trigger_sidekiq_worker(record, remove)
    MySidekiqWorker.perform_async(record.id, remove)
  end
end

class MySidekiqWorker
  def perform(id, remove)
    c = Contact.find(id)
    remove ? c.remove_from_index! : c.index!
  end
end

If a record is deleted, presumably the worker will be enqueued with the tuple id, true. At some later point, the worker will attempt to run Contact.find id and immediately throw an exception because that id will not be present in the table. Even if it didn't, somehow, what would be returned? Probably nil, since the record is gone. How then can you call remove_from_index! on c?

@vincentwoo
Copy link
Author

As an aside, I know you have a more complete example above that handles deleting better, mostly I am confused as to why a sidekiq example that could never work is included.

There should also probably be a helper to get the computed Algolia index name from the model class!

@vincentwoo
Copy link
Author

Found it!

This might be a better snippet for a general purpose algolia worker:

  # don't actually need removed parameter here, now
  def self.trigger_algolia_update record, removed
    AlgoliaSyncWorker.perform_async 'Contact', record.id
  end
class AlgoliaSyncWorker
    include Sidekiq::Worker

  def perform model_name, id
    klass = model_name.constantize
    record = klass.find_by_id id
    if record
      record.index!
    else
      Algolia::Index.new(klass.index_name).delete_object(id)
    end
  end
end

@redox
Copy link
Contributor

redox commented Feb 14, 2016

Yes OK, that makes sense. Do you want to submit a PR @vincentwoo?

@vincentwoo
Copy link
Author

no

@redox
Copy link
Contributor

redox commented Apr 24, 2016

We'll tackle it while fixing #75

@redox redox closed this as completed Apr 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants