lib/pvoutput/client.rb in pvoutput-0.3.0 vs lib/pvoutput/client.rb in pvoutput-0.4.0

- old
+ new

@@ -15,10 +15,28 @@ @batch_size = 100 if donation_mode self.class.headers 'X-Pvoutput-Apikey' => @api_key, 'X-Pvoutput-SystemId' => @system_id end + # Helper method to post batch request to pvout that retries at the moment we get + # a 400 error back with body containing 'Load in progress' + def post_request(path, options = {}, &block) + loop do + response = self.class.post(path, options, &block) + + if response.code == 400 && response.body =~ /Load in progress/ + # We can't send data too fast, when the previous request is still loaded we + # have to wait so sleep 10 seconds and try again + sleep(10) + elsif response.code == 200 + return + else + raise('Bad Post') + end + end + end + # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def add_status(options) time = options[:when] || Time.now params = { @@ -82,12 +100,10 @@ params = { :data => data.chop, } - response = self.class.post('/service/r2/addbatchoutput.jsp', :body => params) - - raise('Bad Post') unless response.code == 200 + post_request('/service/r2/addbatchoutput.jsp', :body => params) end end # rubocop:enable Metrics/AbcSize end end