Skip to content

Instantly share code, notes, and snippets.

@rogercampos
Created November 23, 2011 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rogercampos/1389066 to your computer and use it in GitHub Desktop.
Save rogercampos/1389066 to your computer and use it in GitHub Desktop.
Retrying Hominid api calls due to EOFError
# Example of a DelayedJob that syncs info with mailchimp
class SyncMailchimp < Struct.new(:opts)
include Dobexer::ExceptionNotifier
def run_hominid(attempts = 0, &block)
attempts += 1
block.call
rescue EOFError => e
if attempts < 3
Rails.logger.error("Hominid EOFError, retrying: #{e.message}")
run_hominid(attempts, &block)
else
Rails.logger.error("Give up on EOFError due to more than 3 attempts failed. Opts: #{opts.inspect}")
end
end
def get_h
@h ||= Hominid::API.new(APP_CONFIG['mailchimp_key'], {:timeout => 60})
end
def perform
return unless opts[:action]
@user = User.find(opts[:user])
case opts[:action]
# ...
when :update
run_hominid do
list_id = get_h.find_list_id_by_name("list_name")
vars = {}
vars['FNAME'] = @user.full_name
# other vars...
get_h.list_update_member(list_id, @user.email, vars)
end
end
rescue Hominid::APIError => e
Rails.logger.error "Hominid API error: #{e.message}"
end
end
require 'spec_helper'
describe SyncMailchimp do
it "should be EOFError proof" do
user = Factory :user
job = SyncMailchimp.new :action => :update, :user => user.id
hominid_mock = mock("An hominid API instance")
hominid_mock.stub!(:find_list_id_by_name).and_return("xxxxx")
hominid_mock.should_receive(:list_update_member).at_most(1).times.and_raise(EOFError)
hominid_mock.should_receive(:list_update_member).once.and_return(true)
job.stub!(:get_h).and_return(hominid_mock)
job.perform
end
it "should not try more than 3 times the API call" do
user = Factory :user
job = SyncMailchimp.new :action => :update, :user => user.id
hominid_mock = mock("An hominid API instance")
hominid_mock.stub!(:find_list_id_by_name).and_return("xxxxxx")
hominid_mock.should_receive(:list_update_member).at_most(3).times.and_raise(EOFError)
job.stub!(:get_h).and_return(hominid_mock)
job.perform
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment