Skip to content

Instantly share code, notes, and snippets.

@ono
Created July 8, 2011 10:55
Show Gist options
  • Save ono/1071586 to your computer and use it in GitHub Desktop.
Save ono/1071586 to your computer and use it in GitHub Desktop.
retry_sample.rb
def perform(member_course_id, ls_db, retry_count=0)
member_course = nil
begin
member_course = MemberCourse.find(member_course_id)
rescue ActiveRecord::RecordNotFound => e
if retry_count<3
# Chances are that the job is enqueued before the transaction is
# committed. #6511
Resque.enqueue_at(30.seconds.from_now, self.class, member_course_id, ls_db, retry_count+1)
return
else
# The member_course presumably is deleted. There is enough time waiting. So
# Just ignore here.
return
end
end
Member.current_with_block(member_course.member) do
begin
# reloads member_course with ls_db
member_course = MemberCourse.find(member_course_id)
rescue ActiveRecord::RecordNotFound => e
if retry_count<5
# When memebr_course is not slonyed, that query gets erros but it's ignorable
Resque.enqueue_at(15.minutes.from_now, self.class, member_course_id, ls_db, retry_count+1)
return
else
raise e
end
end
MemberCourse.transaction do
member_course.lock!
ActiveRecord::Base.transaction(ls_db) { perform_product(member_course) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment