Class: OpenC3::Store
Direct Known Subclasses
Constant Summary collapse
- @@instance_mutex =
Mutex used to ensure that only one instance is created
Mutex.new
Instance Attribute Summary collapse
-
#redis_pool ⇒ Object
readonly
Returns the value of attribute redis_pool.
-
#redis_url ⇒ Object
readonly
Returns the value of attribute redis_url.
Class Method Summary collapse
-
.instance(pool_size = 100) ⇒ Object
Get the singleton instance.
-
.method_missing(message, *args, **kwargs, &block) ⇒ Object
Delegate all unknown class methods to delegate to the instance.
Instance Method Summary collapse
- #build_redis ⇒ Object
- #get_last_offset(topic) ⇒ Object
- #get_newest_message(topic) ⇒ Object
- #get_oldest_message(topic) ⇒ Object
-
#initialize(pool_size = 10) ⇒ Store
constructor
A new instance of Store.
-
#initialize_streams(topics) ⇒ Object
Stream APIs.
-
#method_missing(message, *args, **kwargs, &block) ⇒ Object
Delegate all unknown methods to redis through the @redis_pool.
- #read_topics(topics, offsets = nil, timeout_ms = 1000, count = nil) ⇒ Object
-
#trim_topic(topic, minid, approximate = 'true', limit: 0) ⇒ Integer
Trims older entries of the redis stream if needed.
- #update_topic_offsets(topics) ⇒ Object
-
#write_topic(topic, msg_hash, id = '*', maxlen = nil, approximate = 'true') ⇒ String
Add new entry to the redis stream.
Constructor Details
#initialize(pool_size = 10) ⇒ Store
Returns a new instance of Store.
78 79 80 81 82 83 |
# File 'lib/openc3/utilities/store_autoload.rb', line 78 def initialize(pool_size = 10) @redis_username = ENV['OPENC3_REDIS_USERNAME'] @redis_key = ENV['OPENC3_REDIS_PASSWORD'] @redis_url = "redis://#{ENV['OPENC3_REDIS_HOSTNAME']}:#{ENV['OPENC3_REDIS_PORT']}" @redis_pool = ConnectionPool.new(size: pool_size) { build_redis() } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(message, *args, **kwargs, &block) ⇒ Object
Delegate all unknown methods to redis through the @redis_pool
63 64 65 |
# File 'lib/openc3/utilities/store_autoload.rb', line 63 def method_missing(, *args, &block) @redis_pool.with { |redis| redis.public_send(, *args, &block) } end |
Instance Attribute Details
#redis_pool ⇒ Object (readonly)
Returns the value of attribute redis_pool.
43 44 45 |
# File 'lib/openc3/utilities/store_autoload.rb', line 43 def redis_pool @redis_pool end |
#redis_url ⇒ Object (readonly)
Returns the value of attribute redis_url.
42 43 44 |
# File 'lib/openc3/utilities/store_autoload.rb', line 42 def redis_url @redis_url end |
Class Method Details
.instance(pool_size = 100) ⇒ Object
Get the singleton instance
46 47 48 49 50 51 52 53 54 |
# File 'lib/openc3/utilities/store_autoload.rb', line 46 def self.instance(pool_size = 100) # Logger.level = Logger::DEBUG return @instance if @instance @@instance_mutex.synchronize do @instance ||= self.new(pool_size) return @instance end end |
.method_missing(message, *args, **kwargs, &block) ⇒ Object
Delegate all unknown class methods to delegate to the instance
58 59 60 |
# File 'lib/openc3/utilities/store_autoload.rb', line 58 def self.method_missing(, *args, &block) self.instance.public_send(, *args, &block) end |
Instance Method Details
#build_redis ⇒ Object
86 87 88 |
# File 'lib/openc3/utilities/store_autoload.rb', line 86 def build_redis return Redis.new(url: @redis_url, username: @redis_username, password: @redis_key) end |
#get_last_offset(topic) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/openc3/utilities/store_autoload.rb', line 129 def get_last_offset(topic) @redis_pool.with do |redis| result = redis.xrevrange(topic, count: 1) if result and result[0] and result[0][0] result[0][0] else "0-0" end end end |
#get_newest_message(topic) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/openc3/utilities/store_autoload.rb', line 115 def (topic) @redis_pool.with do |redis| # Default in xrevrange is range end '+', start '-' which means get all # elements from higher ID to lower ID and since we're limiting to 1 # we get the last element. See https://redis.io/commands/xrevrange. result = redis.xrevrange(topic, count: 1) if result and result.length > 0 return result[0] else return nil end end end |
#get_oldest_message(topic) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/openc3/utilities/store_autoload.rb', line 104 def (topic) @redis_pool.with do |redis| result = redis.xrange(topic, count: 1) if result and result.length > 0 return result[0] else return nil end end end |
#initialize_streams(topics) ⇒ Object
Stream APIs
95 96 97 98 99 100 101 102 |
# File 'lib/openc3/utilities/store_autoload.rb', line 95 def initialize_streams(topics) @redis_pool.with do |redis| topics.each do |topic| # Create empty stream with maxlen 0 redis.xadd(topic, { a: 'b' }, maxlen: 0) end end end |
#read_topics(topics, offsets = nil, timeout_ms = 1000, count = nil) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/openc3/utilities/store_autoload.rb', line 161 def read_topics(topics, offsets = nil, timeout_ms = 1000, count = nil) Thread.current[:topic_offsets] ||= {} topic_offsets = Thread.current[:topic_offsets] begin # Logger.debug "read_topics: #{topics}, #{offsets} pool:#{@redis_pool}" @redis_pool.with do |redis| offsets = update_topic_offsets(topics) unless offsets result = redis.xread(topics, offsets, block: timeout_ms, count: count) if result and result.length > 0 result.each do |topic, | .each do |msg_id, msg_hash| topic_offsets[topic] = msg_id yield topic, msg_id, msg_hash, redis if block_given? end end end # Logger.debug "result:#{result}" if result and result.length > 0 return result end rescue Redis::TimeoutError return {} # Should return an empty hash not array - xread returns a hash end end |
#trim_topic(topic, minid, approximate = 'true', limit: 0) ⇒ Integer
Trims older entries of the redis stream if needed. > www.rubydoc.info/github/redis/redis-rb/Redis:xtrim
224 225 226 227 228 |
# File 'lib/openc3/utilities/store_autoload.rb', line 224 def trim_topic(topic, minid, approximate = 'true', limit: 0) @redis_pool.with do |redis| return redis.xtrim_minid(topic, minid, approximate: approximate, limit: limit) end end |
#update_topic_offsets(topics) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/openc3/utilities/store_autoload.rb', line 140 def update_topic_offsets(topics) offsets = [] topics.each do |topic| # Normally we will just be grabbing the topic offset # this allows xread to get everything past this point Thread.current[:topic_offsets] ||= {} topic_offsets = Thread.current[:topic_offsets] last_id = topic_offsets[topic] if last_id offsets << last_id else # If there is no topic offset this is the first call. # Get the last offset ID so we'll start getting everything from now on offsets << get_last_offset(topic) topic_offsets[topic] = offsets[-1] end end return offsets end |
#write_topic(topic, msg_hash, id = '*', maxlen = nil, approximate = 'true') ⇒ String
Add new entry to the redis stream. > www.rubydoc.info/github/redis/redis-rb/Redis:xadd
203 204 205 206 207 208 |
# File 'lib/openc3/utilities/store_autoload.rb', line 203 def write_topic(topic, msg_hash, id = '*', maxlen = nil, approximate = 'true') id = '*' if id.nil? @redis_pool.with do |redis| return redis.xadd(topic, msg_hash, id: id, maxlen: maxlen, approximate: approximate) end end |