More advanced delay control

This way I use Delayed :: Job (on Heroku) as an after_create callback after the user creates a specific model.

A common use case, however, turns out to be that users create something, then delete it right away (probably because they made a mistake or something like that).

When this happens, workers are activated, but by the time they request a model at hand, it has already been deleted, BUT due to the automatic repeat function, this ill-fated work will repeat 25 times, and certainly never Work.

Is it possible to somehow catch certain errors and, when they occur, to prevent the repetition of a particular task, but if it is not, will it happen again in the future?

+3
source share
2 answers

Test in the function you call with delayed_job. Make appropriate checks so that your desired work can continue or not, and either work on this task or return success.

+3
source

To expand David's answer, instead:

def after_create
   self.send_later :spam_this_user
end

I would do this:

# user.rb

def after_create
   Delayed::Job.enqueue SendWelcomeEmailJob.new(self.id)
end

# send_welcome_email_job.rb

class SendWelcomeEmailJob <  Struct(:user_id)
   def perform
      user = User.find_by_id(self.user_id)
      return if user.nil?  #user must have been deleted

      # do stuff with user
   end
end
+3
source

Source: https://habr.com/ru/post/1758123/


All Articles