-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ActiveRecord::Relation#load_async method. (#6)
- Loading branch information
Showing
8 changed files
with
103 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 23 additions & 2 deletions
25
lib/io_to_response_payload_ratio/adapters/active_record_adapter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
lib/io_to_response_payload_ratio/patches/future_result_patch.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module IoToResponsePayloadRatio | ||
module FutureResultPatch | ||
def result | ||
# @event_buffer is used to send ActiveSupport notifications related to async queries | ||
return super unless @event_buffer | ||
|
||
res = super | ||
ActiveRecordAdapter.aggregate_result rows: res.rows | ||
|
||
res | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Helpers | ||
module AsyncHelper | ||
def wait_for_async_query(relation, timeout: 5) | ||
if !relation.connection.async_enabled? || relation.instance_variable_get(:@records) | ||
raise ArgumentError, "async hasn't been enabled or used" | ||
end | ||
|
||
future_result = relation.instance_variable_get(:@future_result) | ||
(timeout * 100).times do | ||
return relation unless future_result.pending? | ||
sleep 0.01 | ||
end | ||
|
||
raise Timeout::Error, "The async executor wasn't drained after #{timeout} seconds" | ||
end | ||
end | ||
end |