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

Ability to reset retries count #683

Closed
andrusha opened this issue Feb 6, 2013 · 3 comments
Closed

Ability to reset retries count #683

andrusha opened this issue Feb 6, 2013 · 3 comments

Comments

@andrusha
Copy link

andrusha commented Feb 6, 2013

If you have, say 1000 records you want to import through remote API, it might take significant amount of time and sometimes API might be unresponsive and because it might be inaccessible for unknown period time, job throws an exception and restarts until it can proceed, so thread is not occupied by stale job.
Here is the catch, if API is unresponsive for one hour there would be ~7 retries and if after that we import some records successfully and API is unresponsive again retry_count would be equal 8 and job will have to wait ~1 hour before next try and so on, which is unoptimal.

What I want to have is ability to reset retry_count inside Worker, currently I monkey-patch RetryJob middleware in rather ugly manner:

Sidekiq::Middleware::Server::RetryJobs.class_eval do
  include Storage

  alias_method :old_call, :call

  def call(worker, msg, queue, &block)
    msg.delete 'retry_count'  if storage.get(worker.jid, :reset_retries) == 'true'

    old_call worker, msg, queue, &block
  end
end

class Importer
  include Storage

  def perform(items)
    completed = 0
    items.each do |item|
      api.import item
      completed += 1
    end
  ensure
    storage.set @jid, reset_retries: (completed > 0)
  end
end

Ideally I would like to have something like this:

class Importer
  include RetryJob::Worker

  def perform(items)
    completed = 0
    items.each do |item|
      api.import itme
      completed += 1
    end
  ensure
    reset_retries  if completed > 0
  end
end

Any suggestions on how to implement this?

@mperham
Copy link
Collaborator

mperham commented Feb 6, 2013

Implement your own retry system using scheduling and server middleware.

@andrusha
Copy link
Author

andrusha commented Feb 6, 2013

All right. May ask another question, is there a way to pass variable from worker to middleware without saving/loading it to/from db?

@mperham
Copy link
Collaborator

mperham commented Feb 6, 2013

Not easily but there are ways if you are crafty and don't mind a hack. Middleware is designed to be orthogonal to workers.

@mperham mperham closed this as completed Feb 6, 2013
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