zachfeldman / rubypress

Ruby interface for the WordPress XMLRPC API. Follows standard XML-RPC Documentation closely.
201 stars 55 forks source link

retrying requests that time out #13

Closed pseudomuto closed 10 years ago

pseudomuto commented 10 years ago

@zachfeldman for review

Problem

Time outs can happen for all kinds of reasons. On occasion this happens with shared hosting sites that need to "wake up" your application before serving requests. When this happens, the solution is to simply make the call again.

Writing this retry code everywhere kinda sucks...

Solution

Wrap XMLRPC::Client#call in a retryable block. I've done this by using the equivalent to alias_method_chain so that the retry is completely transparent to calling code (no changes required).

Now all calls to #connection.call in Rubypress::Client will be retried if a Timeout::Error or Net::ReadTimeout occurs.

Allow callers to pass :retry_timeouts => true when initializing a client. This will extend the specific instance of XMLRPC::Client being used by that client with retry functionality without affecting other clients.

borc commented 10 years ago

This PR appears to make automatic retry default behaviour. While I personally don't mind, it may be a bad idea to change this. It may break retry functionality that people have built into their applications, and introduce unexpected behaviour when people update from a previous version.

Maybe this should be a client init option?

zachfeldman commented 10 years ago

Hey @pseudomuto and @borc I agree that this is a good feature to have but we should allow people to enable it when instantiating a client rather than having it be the default behavior. If we can change that then I'll test and merge =)

pseudomuto commented 10 years ago

Great point! Someone might not even know about it and that's definitely a problem. I'll find a way to make this optional

zachfeldman commented 10 years ago

Thanks @pseudomuto !

pseudomuto commented 10 years ago

The more I think about this the more I think maybe this should not be added here. It would add a gem dependency for something people might not even use.

Perhaps a gist along with a link in the README would be better. What do you guys think?

zachfeldman commented 10 years ago

No, I do think it would be a useful part of the library, just that it should be optional. But if you'd rather not do that, then we can totally just go with the latter option.

pseudomuto commented 10 years ago

Hey guys, I've updated the code to only retry timeouts if :retry_timeouts => true is passed into the client initializer.

The way it works is by extending a specific instance of XMLRPC::Client with the XMLRPCRetryable module included here rather than monkey patching the class itself.

I've also updated the README file. What do you think?

pseudomuto commented 10 years ago

That makes sense. Removed!

pseudomuto commented 10 years ago

@zachfeldman would you be able verify CI is passing?

zachfeldman commented 10 years ago

Sure @pseudomuto , sorry for the delay! I will definitely be taking a look at this this weekend.

pseudomuto commented 10 years ago

Sweet!

zachfeldman commented 10 years ago

@pseudomuto I'm having some issues when I try to run tests, will once again take a look this weekend. Sorry for the delay!

pseudomuto commented 10 years ago

Cool...thanks man!

zachfeldman commented 10 years ago

@pseudomuto test suite passes! Just make these changes

1) Remove Pry require line from spec_helper 2) Remove line 6 from users_spec (

:update_content_length_header => true, :preserve_exact_body_bytes => true, :serialize_with => :json)

3) Merge all commits into one final patch

Then we're good to go! Can't wait, sorry once again for the delay.

pseudomuto commented 10 years ago

Hey @zachfeldman, I removed the pry line and squashed the commits. I didn't change user_spec though...did you want me to delete that line anyway?

zachfeldman commented 10 years ago

Yeah @pseudomuto , I'd appreciate that!

pseudomuto commented 10 years ago

And done!