Class: Ruote::Mon::Storage
- Inherits:
-
Object
- Object
- Ruote::Mon::Storage
- Includes:
- StorageBase
- Defined in:
- lib/ruote/mon/storage.rb
Constant Summary collapse
- TYPES =
%w[ msgs schedules expressions workitems errors configurations variables trackers history ]
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#add_type(type) ⇒ Object
Mainly used by ruote’s test/unit/ut_17_storage.rb.
-
#by_field(type, key, value, opts = {}) ⇒ Object
Note: no check on value, MongoDB specific queries can be used…
- #by_participant(type, participant_name, opts = {}) ⇒ Object
-
#close ⇒ Object
Shuts this storage down.
- #delete(doc) ⇒ Object
- #get(type, key) ⇒ Object
- #get_many(type, key = nil, opts = {}) ⇒ Object
- #get_schedules(delta, now) ⇒ Object
- #ids(type) ⇒ Object
-
#initialize(mongo_db, options = {}) ⇒ Storage
constructor
A new instance of Storage.
- #purge! ⇒ Object
-
#purge_type!(type) ⇒ Object
Nukes a db type and reputs it(losing all the documents that were in it).
- #put(doc, opts = {}) ⇒ Object
-
#put_msg(action, options) ⇒ Object
Puts a msg.
- #query_workitems(query) ⇒ Object
-
#reserve(doc) ⇒ Object
Returns true if the doc was successfully deleted.
-
#shutdown ⇒ Object
Shuts this storage down.
Constructor Details
#initialize(mongo_db, options = {}) ⇒ Storage
Returns a new instance of Storage.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruote/mon/storage.rb', line 47 def initialize(mongo_db, ={}) @db = mongo_db @options = #collection('msgs').drop_index('_id_') # can't do that... (TYPES - %w[ msgs schedules ]).each do |t| collection(t).ensure_index('_wfid') collection(t).ensure_index([ [ '_id', 1 ], [ '_rev', 1 ] ]) end collection('schedules').ensure_index('_wfid') collection('schedules').ensure_index('at') replace_engine_configuration() end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
45 46 47 |
# File 'lib/ruote/mon/storage.rb', line 45 def db @db end |
Instance Method Details
#add_type(type) ⇒ Object
Mainly used by ruote’s test/unit/ut_17_storage.rb
198 199 200 201 |
# File 'lib/ruote/mon/storage.rb', line 198 def add_type(type) # nothing to be done end |
#by_field(type, key, value, opts = {}) ⇒ Object
Note: no check on value, MongoDB specific queries can be used…
218 219 220 221 222 223 224 225 |
# File 'lib/ruote/mon/storage.rb', line 218 def by_field(type, key, value, opts={}) value = { '$exists' => true } if value.nil? paginate_workitems( collection(type).find("fields.#{key}" => value), opts) end |
#by_participant(type, participant_name, opts = {}) ⇒ Object
227 228 229 230 231 232 |
# File 'lib/ruote/mon/storage.rb', line 227 def by_participant(type, participant_name, opts={}) paginate_workitems( collection(type).find('participant_name' => participant_name), opts) end |
#close ⇒ Object
Shuts this storage down.
184 185 186 187 |
# File 'lib/ruote/mon/storage.rb', line 184 def close @db.connection.close end |
#delete(doc) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ruote/mon/storage.rb', line 134 def delete(doc) rev = doc['_rev'] raise ArgumentError.new("can't delete doc without _rev") unless rev r = collection(doc).remove( { '_id' => doc['_id'], '_rev' => doc['_rev'] }, :safe => true) if r['n'] == 1 nil else from_mongo(collection(doc).find_one('_id' => doc['_id']) || true) end end |
#get(type, key) ⇒ Object
129 130 131 132 |
# File 'lib/ruote/mon/storage.rb', line 129 def get(type, key) from_mongo(collection(type).find_one('_id' => key)) end |
#get_many(type, key = nil, opts = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ruote/mon/storage.rb', line 151 def get_many(type, key=nil, opts={}) opts = Ruote.keys_to_s(opts) keys = key ? Array(key) : nil cursor = if keys.nil? collection(type).find elsif keys.first.is_a?(Regexp) collection(type).find('_id' => { '$in' => keys }) else # a String collection(type).find('_wfid' => { '$in' => keys }) end paginate(cursor, opts) end |
#get_schedules(delta, now) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/ruote/mon/storage.rb', line 65 def get_schedules(delta, now) from_mongo(collection('schedules').find( 'at' => { '$lte' => Ruote.time_to_utc_s(now) } ).to_a) end |
#ids(type) ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/ruote/mon/storage.rb', line 167 def ids(type) collection(type).find( {}, :fields => [], :sort => [ '_id', Mongo::ASCENDING ] ).collect { |d| d['_id'] } end |
#purge! ⇒ Object
177 178 179 180 |
# File 'lib/ruote/mon/storage.rb', line 177 def purge! TYPES.each { |t| collection(t).remove } end |
#purge_type!(type) ⇒ Object
Nukes a db type and reputs it(losing all the documents that were in it).
205 206 207 208 |
# File 'lib/ruote/mon/storage.rb', line 205 def purge_type!(type) collection(type).remove end |
#put(doc, opts = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruote/mon/storage.rb', line 97 def put(doc, opts={}) original = doc doc = doc.dup doc['_rev'] = (doc['_rev'] || -1) + 1 doc['_wfid'] = doc['_id'].split('!').last doc['put_at'] = Ruote.now_to_utc_s if doc['type'] == 'schedules' doc['_wfid'] = doc['_wfid'].split('-')[0..-2].join('-') end r = begin collection(doc).update( { '_id' => doc['_id'], '_rev' => original['_rev'] }, to_mongo(opts[:update_rev] ? Ruote.fulldup(doc) : doc), :safe => true, :upsert => original['_rev'].nil?) rescue Mongo::OperationFailure false end if r && (r['updatedExisting'] || original['_rev'].nil?) original.merge!( '_rev' => doc['_rev'], 'put_at' => doc['put_at'] ) if opts[:update_rev] nil else from_mongo(collection(doc).find_one('_id' => doc['_id']) || true) end end |
#put_msg(action, options) ⇒ Object
Puts a msg. Doesn’t use :safe => true, it’s always an insert with a new id.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ruote/mon/storage.rb', line 86 def put_msg(action, ) msg = prepare_msg_doc(action, ) msg['put_at'] = Ruote.now_to_utc_s msg['_rev'] = 0 # in case of msg replay collection(msg).insert(to_mongo(msg)) end |
#query_workitems(query) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/ruote/mon/storage.rb', line 234 def query_workitems(query) query = Ruote.keys_to_s(query) opts = {} opts['count'] = query.delete('count') opts['skip'] = query.delete('skip') || query.delete('offset') opts['limit'] = query.delete('limit') opts['descending'] = query.delete('descending') wfid = query.delete('wfid') pname = query.delete('participant') || query.delete('participant_name') query = query.each_with_object({}) { |(k, v), h| h["fields.#{k}"] = v } query['wfid'] = wfid if wfid query['participant_name'] = pname if pname paginate_workitems( collection('workitems').find(query), opts) end |
#reserve(doc) ⇒ Object
Returns true if the doc was successfully deleted.
74 75 76 77 78 79 80 81 |
# File 'lib/ruote/mon/storage.rb', line 74 def reserve(doc) r = collection(doc).remove( { '_id' => doc['_id'] }, :safe => true) r['n'] == 1 end |
#shutdown ⇒ Object
Shuts this storage down.
191 192 193 194 |
# File 'lib/ruote/mon/storage.rb', line 191 def shutdown @db.connection.close end |