Class: Janus::Context
- Inherits:
-
Object
- Object
- Janus::Context
- Defined in:
- lib/janus-ar/context.rb
Overview
Per-execution state that records whether the current unit of work has been pinned to the primary (e.g. after a write, so subsequent reads stay consistent). State is stored in ActiveSupport::IsolatedExecutionState so it follows the application’s configured isolation level (thread or fiber), matching ActiveRecord itself.
Because pooled threads/fibers are reused across requests and jobs, the context MUST be released between units of work or a thread that performed a single write would keep routing every later read to the primary. The Rails integration (see Janus::Railtie) does this automatically; outside Rails, call Janus::Context.release_all yourself (e.g. in a Sidekiq middleware).
Constant Summary collapse
- STATE_KEY =
:janus_ar_context
Instance Attribute Summary collapse
-
#last_used_connection ⇒ Object
readonly
Returns the value of attribute last_used_connection.
Class Method Summary collapse
-
.install_reset_hook(executor) ⇒ Object
Release the context at the start of every unit of work wrapped by the given ActiveSupport executor (web requests, ActiveJob and Sidekiq-on-Rails jobs all run inside it).
- .last_used_connection ⇒ Object
- .release_all ⇒ Object
- .stick_to_primary ⇒ Object
- .use_primary? ⇒ Boolean
- .used_connection(connection) ⇒ Object
Instance Method Summary collapse
-
#initialize(primary: false) ⇒ Context
constructor
A new instance of Context.
- #release_all ⇒ Object
- #stick_to_primary ⇒ Object
- #use_primary? ⇒ Boolean
- #used_connection(connection) ⇒ Object
Constructor Details
#initialize(primary: false) ⇒ Context
Returns a new instance of Context.
20 21 22 23 |
# File 'lib/janus-ar/context.rb', line 20 def initialize(primary: false) @primary = primary @last_used_connection = :primary end |
Instance Attribute Details
#last_used_connection ⇒ Object (readonly)
Returns the value of attribute last_used_connection.
42 43 44 |
# File 'lib/janus-ar/context.rb', line 42 def last_used_connection @last_used_connection end |
Class Method Details
.install_reset_hook(executor) ⇒ Object
Release the context at the start of every unit of work wrapped by the given ActiveSupport executor (web requests, ActiveJob and Sidekiq-on-Rails jobs all run inside it).
68 69 70 |
# File 'lib/janus-ar/context.rb', line 68 def install_reset_hook(executor) executor.to_run { Janus::Context.release_all } end |
.last_used_connection ⇒ Object
61 62 63 |
# File 'lib/janus-ar/context.rb', line 61 def last_used_connection current.last_used_connection end |
.release_all ⇒ Object
49 50 51 |
# File 'lib/janus-ar/context.rb', line 49 def release_all current.release_all end |
.stick_to_primary ⇒ Object
45 46 47 |
# File 'lib/janus-ar/context.rb', line 45 def stick_to_primary current.stick_to_primary end |
.use_primary? ⇒ Boolean
57 58 59 |
# File 'lib/janus-ar/context.rb', line 57 def use_primary? current.use_primary? end |
.used_connection(connection) ⇒ Object
53 54 55 |
# File 'lib/janus-ar/context.rb', line 53 def used_connection(connection) current.used_connection(connection) end |
Instance Method Details
#release_all ⇒ Object
29 30 31 32 |
# File 'lib/janus-ar/context.rb', line 29 def release_all @primary = false @last_used_connection = nil end |
#stick_to_primary ⇒ Object
25 26 27 |
# File 'lib/janus-ar/context.rb', line 25 def stick_to_primary @primary = true end |
#use_primary? ⇒ Boolean
34 35 36 |
# File 'lib/janus-ar/context.rb', line 34 def use_primary? @primary end |
#used_connection(connection) ⇒ Object
38 39 40 |
# File 'lib/janus-ar/context.rb', line 38 def used_connection(connection) @last_used_connection = connection end |