Module: Octopus
- Defined in:
- lib/octopus/log_subscriber.rb,
lib/octopus.rb,
lib/octopus/model.rb,
lib/octopus/proxy.rb,
lib/octopus/version.rb,
lib/octopus/exception.rb,
lib/octopus/migration.rb,
lib/octopus/migration.rb,
lib/octopus/migration.rb,
lib/octopus/migration.rb,
lib/octopus/association.rb,
lib/octopus/persistence.rb,
lib/octopus/scope_proxy.rb,
lib/octopus/slave_group.rb,
lib/octopus/proxy_config.rb,
lib/octopus/load_balancing.rb,
lib/octopus/relation_proxy.rb,
lib/octopus/shard_tracking.rb,
lib/octopus/abstract_adapter.rb,
lib/octopus/collection_proxy.rb,
lib/octopus/singular_association.rb,
lib/octopus/collection_association.rb,
lib/octopus/query_cache_for_shards.rb,
lib/octopus/shard_tracking/dynamic.rb,
lib/octopus/shard_tracking/attribute.rb,
lib/octopus/association_shard_tracking.rb,
lib/octopus/load_balancing/round_robin.rb
Overview
The round-robin load balancing of slaves belonging to the same shard. It is a pool that contains slaves which queries are distributed to.
Defined Under Namespace
Modules: AbstractAdapter, Association, AssociationShardTracking, CollectionAssociation, CollectionProxy, ConnectionPool, LoadBalancing, LogSubscriber, Migration, MigrationProxy, Migrator, Model, Persistence, ResultPatch, ShardTracking, SingularAssociation, UnknownMigrationVersionError
Classes: Exception, Proxy, ProxyConfig, RelationProxy, ScopeProxy, SlaveGroup
Constant Summary
collapse
- VERSION =
'0.11.3'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Attribute Details
#logger=(value) ⇒ Object
Sets the attribute logger
130
131
132
|
# File 'lib/octopus.rb', line 130
def logger=(value)
@logger = value
end
|
Class Method Details
.atleast_rails50? ⇒ Boolean
106
107
108
|
# File 'lib/octopus.rb', line 106
def self.atleast_rails50?
ActiveRecord::VERSION::MAJOR >= 5
end
|
.atleast_rails51? ⇒ Boolean
122
123
124
|
# File 'lib/octopus.rb', line 122
def self.atleast_rails51?
ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
end
|
.atleast_rails52? ⇒ Boolean
126
127
128
|
# File 'lib/octopus.rb', line 126
def self.atleast_rails52?
ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 1)
end
|
.config ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/octopus.rb', line 18
def self.config
@config ||= begin
file_name = File.join(Octopus.directory, 'config/shards.yml').to_s
if File.exist?(file_name) || File.symlink?(file_name)
config ||= HashWithIndifferentAccess.new(YAML.load(ERB.new(File.read(file_name)).result))[Octopus.env]
else
config ||= HashWithIndifferentAccess.new
end
config
end
end
|
.directory ⇒ Object
Returns the Rails.root_to_s when you are using rails Running the current directory in a generic Ruby process
61
62
63
|
# File 'lib/octopus.rb', line 61
def self.directory
@directory ||= defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd
end
|
.enabled? ⇒ Boolean
Public: Whether or not Octopus is configured and should hook into the current environment. Checks the environments config option for the Rails environment by default.
Returns a boolean
49
50
51
52
53
54
55
56
57
|
# File 'lib/octopus.rb', line 49
def self.enabled?
if defined?(::Rails.env)
Octopus.environments.include?(Rails.env.to_s)
else
Octopus.config
end
end
|
.env ⇒ Object
10
11
12
|
# File 'lib/octopus.rb', line 10
def self.env
@env ||= 'octopus'
end
|
.environments ⇒ Object
76
77
78
|
# File 'lib/octopus.rb', line 76
def self.environments
@environments ||= config['environments'] || ['production']
end
|
.environments=(environments) ⇒ Object
72
73
74
|
# File 'lib/octopus.rb', line 72
def self.environments=(environments)
@environments = environments.map(&:to_s)
end
|
.fully_replicated(&_block) ⇒ Object
.load_balancer=(balancer) ⇒ Object
32
33
34
|
# File 'lib/octopus.rb', line 32
def self.load_balancer=(balancer)
@load_balancer = balancer
end
|
.logger ⇒ Object
132
133
134
135
136
137
138
|
# File 'lib/octopus.rb', line 132
def self.logger
if defined?(Rails.logger)
@logger ||= Rails.logger
else
@logger ||= Logger.new($stderr)
end
end
|
.master_shard ⇒ Object
40
41
42
|
# File 'lib/octopus.rb', line 40
def self.master_shard
((config && config[:master_shard]) || :master).to_sym
end
|
.rails42? ⇒ Boolean
98
99
100
|
# File 'lib/octopus.rb', line 98
def self.rails42?
rails4? && ActiveRecord::VERSION::MINOR == 2
end
|
.rails4? ⇒ Boolean
94
95
96
|
# File 'lib/octopus.rb', line 94
def self.rails4?
ActiveRecord::VERSION::MAJOR == 4
end
|
.rails50? ⇒ Boolean
102
103
104
|
# File 'lib/octopus.rb', line 102
def self.rails50?
ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
end
|
.rails51? ⇒ Boolean
110
111
112
|
# File 'lib/octopus.rb', line 110
def self.rails51?
ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 1
end
|
.rails52? ⇒ Boolean
114
115
116
|
# File 'lib/octopus.rb', line 114
def self.rails52?
ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 2
end
|
.rails60? ⇒ Boolean
118
119
120
|
# File 'lib/octopus.rb', line 118
def self.rails60?
ActiveRecord::VERSION::MAJOR > 6 || ActiveRecord::VERSION::MAJOR == 6
end
|
.rails_env ⇒ Object
14
15
16
|
# File 'lib/octopus.rb', line 14
def self.rails_env
@rails_env ||= defined?(::Rails.env) ? Rails.env.to_s : 'shards'
end
|
.robust_environment? ⇒ Boolean
90
91
92
|
# File 'lib/octopus.rb', line 90
def self.robust_environment?
robust_environments.include? rails_env
end
|
.robust_environments ⇒ Object
Environments in which to swallow failures from a single shard when iterating through all.
86
87
88
|
# File 'lib/octopus.rb', line 86
def self.robust_environments
@robust_environments ||= config['robust_environments'] || ['production']
end
|
.robust_environments=(environments) ⇒ Object
80
81
82
|
# File 'lib/octopus.rb', line 80
def self.robust_environments=(environments)
@robust_environments = environments.map(&:to_s)
end
|
.setup {|_self| ... } ⇒ Object
This is the default way to do Octopus Setup Available variables: :enviroments => the enviroments that octopus will run. default: ‘production’
68
69
70
|
# File 'lib/octopus.rb', line 68
def self.setup
yield self
end
|
.shards=(shards) ⇒ Object
140
141
142
143
|
# File 'lib/octopus.rb', line 140
def self.shards=(shards)
config[rails_env] = HashWithIndifferentAccess.new(shards)
ActiveRecord::Base.connection.initialize_shards(@config)
end
|
.using(shard, &block) ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File 'lib/octopus.rb', line 145
def self.using(shard, &block)
conn = ActiveRecord::Base.connection
if conn.is_a?(Octopus::Proxy)
conn.run_queries_on_shard(shard, &block)
else
yield
end
end
|
.using_all(&block) ⇒ Object
165
166
167
168
169
170
171
172
173
|
# File 'lib/octopus.rb', line 165
def self.using_all(&block)
conn = ActiveRecord::Base.connection
if conn.is_a?(Octopus::Proxy)
conn.send_queries_to_all_shards(&block)
else
yield
end
end
|
.using_group(group, &block) ⇒ Object
155
156
157
158
159
160
161
162
163
|
# File 'lib/octopus.rb', line 155
def self.using_group(group, &block)
conn = ActiveRecord::Base.connection
if conn.is_a?(Octopus::Proxy)
conn.send_queries_to_group(group, &block)
else
yield
end
end
|