Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract ActiveSupport::ExecutionContext out of ActiveRecord::QueryLogs
I'm working on a standardized error reporting interface (#43472) and it has the same need for a `context` than Active Record's query logs. Just like query logs, when reporting an error you want to know what the current controller or job is, etc. So by extracting it we can allow both API to use the same store.
- Loading branch information
Showing
16 changed files
with
151 additions
and
141 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
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveSupport | ||
module ExecutionContext # :nodoc: | ||
@after_change_callbacks = [] | ||
class << self | ||
def after_change(&block) | ||
@after_change_callbacks << block | ||
end | ||
|
||
# Updates the execution context. If a block is given, it resets the provided keys to their | ||
# previous value once the block exits. | ||
def set(**options) | ||
options.symbolize_keys! | ||
keys = options.keys | ||
|
||
store = self.store | ||
|
||
previous_context = keys.zip(store.values_at(*keys)).to_h | ||
|
||
store.merge!(options) | ||
@after_change_callbacks.each(&:call) | ||
|
||
if block_given? | ||
begin | ||
yield | ||
ensure | ||
store.merge!(previous_context) | ||
@after_change_callbacks.each(&:call) | ||
end | ||
end | ||
end | ||
|
||
def []=(key, value) | ||
store[key.to_sym] = value | ||
@after_change_callbacks.each(&:call) | ||
end | ||
|
||
def to_h | ||
store.dup | ||
end | ||
|
||
def clear | ||
store.clear | ||
end | ||
|
||
private | ||
def store | ||
Thread.current[:active_support_execution_context] ||= {} | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.