Search code examples
ruby-on-railswebcapybarasidekiq

Rails | How to Ignore raise error sidekiq worker | Capybara


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.


Solution

  • 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