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:
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

View File

@@ -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

View File

@@ -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,6 +99,9 @@ class W3DHub
end
end
Thread.new do
@thread_pool << Thread.current
while BackgroundWorker.run?
job = @jobs.shift
@@ -115,6 +121,7 @@ class W3DHub
logger.info(LOG_TAG) { "Stopped background job worker." }
@@alive = false
end
end
def add_job(job)
@jobs << job