T
T
The Whiz2015-09-16 14:01:12
Ruby on Rails
The Whiz, 2015-09-16 14:01:12

Why is the object not saved when executing a deferred method through Sidekiq?

When creating an object, I want a few more methods to be executed deferred:
Object:

after_create :process_offer

  protected

      def process_offer
        BodyWorker.perform_async(self.id)
      end

Worker:
class BodyWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(offer_id)
    offer = Offer.find(offer_id)

    remove_html(offer)
    offer.publish!
  end

    protected

      def remove_html(offer)
        offer.body = ''
        offer.save
      end

In this case, I indicated to clear the body of the object so as not to bother the question. However, this worker does not execute the remove_html method, or executes it, but the result is not saved, I do not understand why.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Zaporozhchenko Oleg, 2015-09-16
@c3gdlk

It is strange that the worker does not crash, because to call the worker after saving, you need to use after_commit and not after_create. At the moment when after_create is called, the transaction has not yet ended and the id is available only in the model.

T
thepry, 2015-09-16
@thepry

However, this worker does not execute the remove_html method, or executes it, but the result is not saved, I do not understand why.

Log what the save method returns. Maybe some validations don't allow saving it*

J
jarosluv, 2015-09-17
@jarosluv

Post the latest entries from the Sidekiq log (log/sidekiq.log).
Perhaps the object in the method remove_htmldoes not pass validation. Try using the bang method save!.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question