2

I am pretty new to rails and my team recently moved to use sidekiq

Calling a worker with this instruction within model

CoolClassJob.perform_async(...)

I'm using a worker with a code similar to this:

class CoolClassJob
  include Sidekiq::Worker
  sidekiq_options queue: "payment", retry: 5

  sidekiq_retry_in do |count|
    10
  end

  def perform()
    ...
        whatever = {...}

        if whatever.status == 'successful'
          thisCoolFunction                  # successfully ends job
        elsif whatever.status == 'failed'
          anotherCoolFunction               # successfully ends job
        elsif whatever.pending?             # I want to retry if it falls in this condition since it is "waiting" for another task to complete.
          raise 'We are trying again'
        end
    ...
  end
  ...
end

I tried with

          begin
            raise 'We are trying again!'
          rescue
             nil
          end

But when I run my tests, I get this error:

     Failure/Error: raise 'We are trying again!'
     
     RuntimeError:
       'We are trying again!'
     ...

Which of course, makes sense to me, since I'm raising the error, I tried searching but wasn't able to come up with a solution. I'm wondering whether its able to a) retry again without raising an error or b) tell Capybara (rspec) to keep trying without throwing an error.

Mike Alvarado
  • 109
  • 10

1 Answers1

1

One way would be to reschedule your worker :

def perform()
    ...
        whatever = {...}

        if whatever.status == 'successful'
          thisCoolFunction                  # successfully ends job
        elsif whatever.status == 'failed'
          anotherCoolFunction               # successfully ends job
        elsif whatever.pending?             # I want to retry if it falls in this condition since it is "waiting" for another task to complete.
          self.class.perform_in(1.minute)
        end
    ...
end

Or maybe you can check this SO answer : Sidekiq/Airbrake only post exception when retries extinguished

BTL
  • 4,311
  • 3
  • 24
  • 29
  • I guess with this I no longer need the `sidekiq_retry_in` part. Is this still affected by the number of retries I set? `sidekiq_options queue: "queue", retry: 5` – Mike Alvarado Jul 24 '20 at 15:58
  • Also, for some reason I'm getting a `NoMethodError: undefined method 'perform_in' for CoolClassJob` – Mike Alvarado Jul 24 '20 at 16:15
  • 1
    Yes you are right I updated to : `self.class.perform_in(1.minute)` – BTL Jul 25 '20 at 12:19