Updated gems, fixed error on Ruby 3.2 (Thread block no longer passes self as argument), fixed API calls not timing out properly, fixed BackgroundWorker not halting properly.

This commit is contained in:
2023-02-03 12:19:18 -06:00
parent db12e56623
commit c73bd2d88b
3 changed files with 63 additions and 43 deletions

View File

@@ -3,28 +3,26 @@ GEM
specs: specs:
addressable (2.8.1) addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0) public_suffix (>= 2.0.2, < 6.0)
concurrent-ruby (1.1.10) concurrent-ruby (1.2.0)
cyberarm_engine (0.22.0) cyberarm_engine (0.23.0)
excon (~> 0.88) excon (~> 0.88)
gosu (~> 1.1) gosu (~> 1.1)
gosu_more_drawables (~> 0.3) gosu_more_drawables (~> 0.3)
digest-crc (0.6.4) digest-crc (0.6.4)
rake (>= 12.0.0, < 14.0.0) rake (>= 12.0.0, < 14.0.0)
event_emitter (0.2.6) event_emitter (0.2.6)
excon (0.94.0) excon (0.99.0)
ffi (1.15.5) ffi (1.15.5)
ffi (1.15.5-x64-mingw-ucrt)
ffi (1.15.5-x64-mingw32)
ffi-win32-extensions (1.0.4) ffi-win32-extensions (1.0.4)
ffi ffi
gosu (1.4.3) gosu (1.4.5)
gosu_more_drawables (0.3.1) gosu_more_drawables (0.3.1)
i18n (1.12.0) i18n (1.12.0)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
ircparser (1.0.0) ircparser (1.0.0)
launchy (2.5.0) launchy (2.5.2)
addressable (~> 2.7) addressable (~> 2.8)
public_suffix (5.0.0) public_suffix (5.0.1)
rake (13.0.6) rake (13.0.6)
rexml (3.2.5) rexml (3.2.5)
rubyzip (2.3.2) rubyzip (2.3.2)
@@ -59,4 +57,4 @@ DEPENDENCIES
win32-security win32-security
BUNDLED WITH BUNDLED WITH
2.3.17 2.4.3

View File

@@ -15,10 +15,14 @@ class W3DHub
}.freeze }.freeze
def self.on_thread(method, *args, &callback) def self.on_thread(method, *args, &callback)
BackgroundWorker.job(-> { Api.send(method, *args) }, callback) BackgroundWorker.foreground_job(-> { Api.send(method, *args) }, callback)
end end
class DummyResponse class DummyResponse
def initialize(error)
@error = error
end
def success? def success?
false false
end end
@@ -26,16 +30,22 @@ class W3DHub
def status def status
-1 -1
end end
def body
""
end
def error
@error
end
end end
#! === W3D Hub API === !# #! === W3D Hub API === !#
ENDPOINT = "https://secure.w3dhub.com".freeze ENDPOINT = "https://secure.w3dhub.com".freeze
def self.post(url, headers = DEFAULT_HEADERS, body = nil) def self.excon(method, 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 #{method.to_s.upcase} \"#{url}\"..." }
logger.debug(LOG_TAG) { "Fetching POST \"#{url}\"..." }
# Inject Authorization header if account data is populated # Inject Authorization header if account data is populated
if Store.account if Store.account
@@ -45,28 +55,37 @@ class W3DHub
end end
begin begin
Excon.post( Excon.send(
method,
url, url,
headers: headers, headers: headers,
body: body, body: body,
nonblock: true,
tcp_nodelay: true, tcp_nodelay: true,
write_timeout: API_TIMEOUT, write_timeout: API_TIMEOUT,
read_timeout: API_TIMEOUT, read_timeout: API_TIMEOUT,
connection_timeout: API_TIMEOUT, connect_timeout: API_TIMEOUT,
idempotent: true, idempotent: true,
retry_limit: 6, retry_limit: 3,
retry_interval: 5 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" } 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) { "Connection to \"#{url}\" errored:" }
logger.error(LOG_TAG) { e } logger.error(LOG_TAG) { e }
DummyResponse.new
DummyResponse.new(e)
end end
end end
def self.post(url, headers = DEFAULT_HEADERS, body = nil)
excon(:post, url, headers, body)
end
# Method: POST # Method: POST
# FORMAT: JSON # FORMAT: JSON
@@ -240,11 +259,7 @@ class W3DHub
SERVER_LIST_ENDPOINT = "https://gsh.w3dhub.com".freeze SERVER_LIST_ENDPOINT = "https://gsh.w3dhub.com".freeze
def self.get(url, headers = DEFAULT_HEADERS, body = nil) def self.get(url, headers = DEFAULT_HEADERS, body = nil)
@client ||= Excon.new(SERVER_LIST_ENDPOINT, persistent: true) excon(:get, url, headers, body)
logger.debug(LOG_TAG) { "Fetching GET \"#{url}\"..." }
Excon.get(url, headers: headers, body: body, persistent: true, idempotent: true, retry_limit: 6, retry_interval: 5)
end end
# Method: GET # Method: GET

View File

@@ -71,12 +71,15 @@ class W3DHub
def kill! def kill!
@thread_pool.each(&:kill) @thread_pool.each(&:kill)
logger.info(LOG_TAG) { "Forcefully killed background job worker." }
@@alive = false
end end
def handle_jobs def handle_jobs
8.times do |i| 8.times do |i|
Thread.new do |thread| Thread.new do
@thread_pool << thread @thread_pool << Thread.current
while BackgroundWorker.run? while BackgroundWorker.run?
job = @parallel_jobs.shift job = @parallel_jobs.shift
@@ -96,24 +99,28 @@ class W3DHub
end end
end end
while BackgroundWorker.run? Thread.new do
job = @jobs.shift @thread_pool << Thread.current
@busy = true while BackgroundWorker.run?
job = @jobs.shift
begin @busy = true
job&.do
rescue => e begin
job&.raise_error(e) job&.do
rescue => e
job&.raise_error(e)
end
@busy = !@jobs.empty?
sleep 0.1
end end
@busy = !@jobs.empty? logger.info(LOG_TAG) { "Stopped background job worker." }
@@alive = false
sleep 0.1
end end
logger.info(LOG_TAG) { "Stopped background job worker." }
@@alive = false
end end
def add_job(job) def add_job(job)