mirror of
https://github.com/cyberarm/w3d_hub_linux_launcher.git
synced 2026-05-06 09:28:18 +00:00
Gems, workers, networking, oh my!
This commit is contained in:
64
lib/worker/request.rb
Normal file
64
lib/worker/request.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
module W3DHubLauncher
|
||||
class Worker
|
||||
class Request
|
||||
Query = Data.define(:type, :request_id, :data)
|
||||
|
||||
FETCH_URL = 0
|
||||
DOWNLOAD_URL = 1
|
||||
W3DHUB_API_CALL = 10
|
||||
|
||||
STATUS_ERROR = -1 # request has failed
|
||||
STATUS_PENDING = 0 # request has not yet started
|
||||
STATUS_OK = 1 # request completed successfully
|
||||
STATUS_COMPLETE = STATUS_OK
|
||||
STATUS_IN_PROGRESS = 2 # request is in progress
|
||||
STATUS_BUSY = STATUS_IN_PROGRESS
|
||||
|
||||
# NOT "Thread"/Ractor safe
|
||||
@request_id = 0
|
||||
@requests = []
|
||||
|
||||
# NOT "Thread"/Ractor safe. Only call from main ractor
|
||||
# returns next available request id, and auto increments by 1
|
||||
def self.request_id
|
||||
@request_id += 1
|
||||
end
|
||||
|
||||
# NOT "Thread"/Ractor safe.
|
||||
# returns an array of pending requests
|
||||
def self.requests
|
||||
@requests
|
||||
end
|
||||
|
||||
attr_reader :type, :data, :request_id
|
||||
|
||||
def initialize(type, data, request_id: Request.request_id, &block)
|
||||
@type = type.freeze
|
||||
@data = data.freeze
|
||||
@status = STATUS_PENDING
|
||||
|
||||
@request_id = request_id
|
||||
@callback = block # only called on error or success
|
||||
|
||||
enqueue(@type, @request_id, @data)
|
||||
end
|
||||
|
||||
def enqueue(type, id, data)
|
||||
Request.requests << self
|
||||
W3DHubLauncher::WORKER.send(Query.new(type, id, data))
|
||||
end
|
||||
|
||||
# event from Worker received
|
||||
def handle_event(event, data)
|
||||
pp [event, data]
|
||||
|
||||
case event
|
||||
when STATUS_COMPLETE
|
||||
Request.requests.delete(self)
|
||||
when STATUS_ERROR
|
||||
Request.requests.delete(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user