Skip to content

Commit

Permalink
refactor: remove waitgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis committed Nov 28, 2024
1 parent c79a057 commit d8005f6
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/placeos-rest-api/controllers/drivers.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "wait_group"
require "./application"

module PlaceOS::Api
Expand Down Expand Up @@ -132,26 +131,27 @@ module PlaceOS::Api

def self.driver_reload(driver : ::PlaceOS::Model::Driver, request_id : String) : Tuple(Int32, String)
cores = RemoteDriver.default_discovery.node_hash
wg = WaitGroup.new
channel = Channel(Tuple(Int32, String)).new(cores.size)
cores.each do |core_id, uri|
wg.spawn do
client = PlaceOS::Core::Client.new(uri: uri, request_id: request_id)
resp = client.driver_reload(driver.id.as(String))
channel.send(resp)
rescue error
Log.error(exception: error) { {
message: "failure to request a driver reload on core node",
core_uri: uri.to_s,
core_id: core_id,
driver: driver.id.as(String),
request_id: request_id,
} }
channel.send({500, "failed to request a driver reload on core #{uri}: error: #{error.message}"})
end
cores.each do |cid, core_uri|
->(core_id : String, uri : URI) do
spawn do
client = PlaceOS::Core::Client.new(uri: uri, request_id: request_id)
resp = client.driver_reload(driver.id.as(String))
channel.send(resp)
rescue error
Log.error(exception: error) { {
message: "failure to request a driver reload on core node",
core_uri: uri.to_s,
core_id: core_id,
driver: driver.id.as(String),
request_id: request_id,
} }
channel.send({500, "failed to request a driver reload on core #{uri}: error: #{error.message}"})
end
end.call(cid, core_uri)
end

wg.wait
Fiber.yield

resps = cores.map do |_, _|
channel.receive
Expand Down

0 comments on commit d8005f6

Please sign in to comment.