diff --git a/Gemfile.lock b/Gemfile.lock index 31cd0e5..1291e6d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,28 +3,26 @@ GEM specs: addressable (2.8.1) public_suffix (>= 2.0.2, < 6.0) - concurrent-ruby (1.1.10) - cyberarm_engine (0.22.0) + concurrent-ruby (1.2.0) + cyberarm_engine (0.23.0) excon (~> 0.88) gosu (~> 1.1) gosu_more_drawables (~> 0.3) digest-crc (0.6.4) rake (>= 12.0.0, < 14.0.0) event_emitter (0.2.6) - excon (0.94.0) + excon (0.99.0) ffi (1.15.5) - ffi (1.15.5-x64-mingw-ucrt) - ffi (1.15.5-x64-mingw32) ffi-win32-extensions (1.0.4) ffi - gosu (1.4.3) + gosu (1.4.5) gosu_more_drawables (0.3.1) i18n (1.12.0) concurrent-ruby (~> 1.0) ircparser (1.0.0) - launchy (2.5.0) - addressable (~> 2.7) - public_suffix (5.0.0) + launchy (2.5.2) + addressable (~> 2.8) + public_suffix (5.0.1) rake (13.0.6) rexml (3.2.5) rubyzip (2.3.2) @@ -59,4 +57,4 @@ DEPENDENCIES win32-security BUNDLED WITH - 2.3.17 + 2.4.3 diff --git a/lib/api.rb b/lib/api.rb index d9d1b2e..a5e8d54 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -15,10 +15,14 @@ class W3DHub }.freeze def self.on_thread(method, *args, &callback) - BackgroundWorker.job(-> { Api.send(method, *args) }, callback) + BackgroundWorker.foreground_job(-> { Api.send(method, *args) }, callback) end class DummyResponse + def initialize(error) + @error = error + end + def success? false end @@ -26,16 +30,22 @@ class W3DHub def status -1 end + + def body + "" + end + + def error + @error + end end #! === W3D Hub API === !# ENDPOINT = "https://secure.w3dhub.com".freeze - def self.post(url, headers = DEFAULT_HEADERS, body = nil) - # TODO: Check if session has expired and attempt to refresh session before submitting request - - logger.debug(LOG_TAG) { "Fetching POST \"#{url}\"..." } + def self.excon(method, url, headers = DEFAULT_HEADERS, body = nil) + logger.debug(LOG_TAG) { "Fetching #{method.to_s.upcase} \"#{url}\"..." } # Inject Authorization header if account data is populated if Store.account @@ -45,28 +55,37 @@ class W3DHub end begin - Excon.post( + Excon.send( + method, url, headers: headers, body: body, + nonblock: true, tcp_nodelay: true, write_timeout: API_TIMEOUT, read_timeout: API_TIMEOUT, - connection_timeout: API_TIMEOUT, + connect_timeout: API_TIMEOUT, idempotent: true, - retry_limit: 6, - retry_interval: 5 + retry_limit: 3, + retry_interval: 1, + retry_errors: [Excon::Error::Socket, Excon::Error::HTTPStatus] # Don't retry on timeout ) - rescue Excon::Errors::Timeout + rescue Excon::Errors::Timeout => e logger.error(LOG_TAG) { "Connection to \"#{url}\" timed out after: #{API_TIMEOUT} seconds" } - DummyResponse.new - rescue Excon::Socket::Error => e + + DummyResponse.new(e) + rescue Excon::Error => e logger.error(LOG_TAG) { "Connection to \"#{url}\" errored:" } logger.error(LOG_TAG) { e } - DummyResponse.new + + DummyResponse.new(e) end end + def self.post(url, headers = DEFAULT_HEADERS, body = nil) + excon(:post, url, headers, body) + end + # Method: POST # FORMAT: JSON @@ -240,11 +259,7 @@ class W3DHub SERVER_LIST_ENDPOINT = "https://gsh.w3dhub.com".freeze def self.get(url, headers = DEFAULT_HEADERS, body = nil) - @client ||= Excon.new(SERVER_LIST_ENDPOINT, persistent: true) - - logger.debug(LOG_TAG) { "Fetching GET \"#{url}\"..." } - - Excon.get(url, headers: headers, body: body, persistent: true, idempotent: true, retry_limit: 6, retry_interval: 5) + excon(:get, url, headers, body) end # Method: GET diff --git a/lib/background_worker.rb b/lib/background_worker.rb index fc54748..5628fd9 100644 --- a/lib/background_worker.rb +++ b/lib/background_worker.rb @@ -71,12 +71,15 @@ class W3DHub def kill! @thread_pool.each(&:kill) + + logger.info(LOG_TAG) { "Forcefully killed background job worker." } + @@alive = false end def handle_jobs 8.times do |i| - Thread.new do |thread| - @thread_pool << thread + Thread.new do + @thread_pool << Thread.current while BackgroundWorker.run? job = @parallel_jobs.shift @@ -96,24 +99,28 @@ class W3DHub end end - while BackgroundWorker.run? - job = @jobs.shift + Thread.new do + @thread_pool << Thread.current - @busy = true + while BackgroundWorker.run? + job = @jobs.shift - begin - job&.do - rescue => e - job&.raise_error(e) + @busy = true + + begin + job&.do + rescue => e + job&.raise_error(e) + end + + @busy = !@jobs.empty? + + sleep 0.1 end - @busy = !@jobs.empty? - - sleep 0.1 + logger.info(LOG_TAG) { "Stopped background job worker." } + @@alive = false end - - logger.info(LOG_TAG) { "Stopped background job worker." } - @@alive = false end def add_job(job)