Class: Updater::ORM::Mongo

Inherits:
Object
  • Object
show all
Defined in:
lib/updater/orm/mongo.rb

Constant Summary collapse

FINDER =
:find_one
ID =
:_id

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Mongo

Returns a new instance of Mongo.



12
13
14
15
16
17
18
19
20
21
# File 'lib/updater/orm/mongo.rb', line 12

def initialize(hash = {})
  @hash = {}
  hash.each do |key, val|
    if respond_to? "#{key}="
      send("#{key}=", val)
    else
      @hash[key] = val
    end
  end
end

Class Attribute Details

.collectionObject

Returns the value of attribute collection.



194
195
196
# File 'lib/updater/orm/mongo.rb', line 194

def collection
  @collection
end

.dbObject

Returns the value of attribute db.



194
195
196
# File 'lib/updater/orm/mongo.rb', line 194

def db
  @db
end

.loggerObject

Returns the value of attribute logger.



194
195
196
# File 'lib/updater/orm/mongo.rb', line 194

def logger
  @logger
end

Class Method Details

.after_forkObject



231
232
233
# File 'lib/updater/orm/mongo.rb', line 231

def after_fork
  
end

.before_forkObject



227
228
229
# File 'lib/updater/orm/mongo.rb', line 227

def before_fork
  @db.connection.close
end

.clear_allObject



287
288
289
# File 'lib/updater/orm/mongo.rb', line 287

def clear_all
  @collection.remove
end

.clear_locks(worker) ⇒ Object



291
292
293
# File 'lib/updater/orm/mongo.rb', line 291

def clear_locks(worker)
  @collection.update({:lock_name=>worker.name},{'$unset'=>{:lock_name=>1}},:multi=>true)
end

.create(hash) ⇒ Object



282
283
284
285
# File 'lib/updater/orm/mongo.rb', line 282

def create(hash)
  ret = new(hash)
  ret.save and ret
end

.currentObject



255
256
257
258
259
260
# File 'lib/updater/orm/mongo.rb', line 255

def current
  # raise NotImplementedError, "Mongo does not support lazy evaluation"
  @collection.find(:time=>{'$lte'=>tnow}).map do |x|
    new(x)
  end
end

.current_loadObject



262
263
264
# File 'lib/updater/orm/mongo.rb', line 262

def current_load
  @collection.find(:time=>{'$lte'=>tnow}, :lock_name=>nil).count
end

.delayedObject



266
267
268
# File 'lib/updater/orm/mongo.rb', line 266

def delayed
  @collection.find(:time=>{'$gt'=>tnow}).count
end

.for(target, finder, args, name = nil) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/updater/orm/mongo.rb', line 295

def for(target,finder,args,name=nil)
  if name
    @collection.find(
      :target=>target.to_s, 
      :finder_args=>args, 
      :finder=>finder, 
      :lock_name=>nil,
      :name=>name
    )
  else
    @collection.find(
      :target=>target.to_s, 
      :finder_args=>args, 
      :finder=>finder, 
      :lock_name=>nil,
    )
  end.map {|x| new(x) }
end

.future(start, finish) ⇒ Object



270
271
272
# File 'lib/updater/orm/mongo.rb', line 270

def future(start, finish)
  @collection.find(:time=>{'$gt'=>start+tnow,'$lt'=>finish+tnow}).count
end

.get(id) ⇒ Object



249
250
251
252
253
# File 'lib/updater/orm/mongo.rb', line 249

def get(id)
  id = BSON::ObjectId.from_string(id) if id.kind_of? String
  inst = @collection.find_one(id)
  inst && new(inst)
end

.lock_next(worker) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/updater/orm/mongo.rb', line 235

def lock_next(worker)
  hash = Hash.new
  hash['findandmodify'] =@collection.name
  hash['query'] = {:time=>{'$lte'=>tnow},:lock_name=>nil}
  hash['sort'] =[[:time,'ascending']] #oldest first
  hash['update'] = {'$set'=>{:lock_name=>worker.name}}
  hash['new'] = true
  
  
  ret = @db.command hash, :check_response=>false
  return nil unless ret['ok'] == 1
  return new(ret['value'])
end

.queue_timeObject



274
275
276
277
278
279
280
# File 'lib/updater/orm/mongo.rb', line 274

def queue_time
  nxt = @collection.find_one({:lock_name=>nil, :time=>{'$ne'=>nil}}, :sort=>[[:time, :asc]], :fields=>[:time])
  # logger.debug {"  the queue is empty"} unless nxt
  return nil unless nxt
  return 0 if nxt['time'] <= tnow
  return nxt['time'] - tnow
end

.setup(options) ⇒ Object

Availible options:

  • :database - required either an established Mongo::DB database OR the name of the database

  • :collection - which collection to store jobs in. Default: “updater”

If a connection to the database must be established (ie :database is not a Mongo::DB) these options may be used to establish that connection.

  • :host - the host to connect to. Default: “localhost”

  • :port - the port to connect to. Default: 27017

  • :username/:password - if these are present, they will be used to authenticate against the database

Raises:

  • (ArgumentError)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/updater/orm/mongo.rb', line 205

def setup(options)
  logger ||= options[:logger] || Update.logger
  raise ArgumentError, "Must spesify the name of a database when setting up Mongo driver" unless options[:database]
  if options[:database].kind_of? ::Mongo::DB
    @db = options[:database]
    options[:database] = @db.name
    logger.info "Updater is using already established connection to #{@db.name}"
  else
    logger.info "Attempting to connect to mongodb at #{[options[:host] || "localhost", options[:port] || 27017].join(':')} database: \"#{options[:database]}\""
    @db = ::Mongo::Connection.new(options[:host] || "localhost", options[:port] || 27017).db(options[:database].to_s)
    if options[:username] && options[:password]
      success = db.authenticate(options[:username] , options[:password])
      raise RunTimeError, "Could not Authenticate with MongoDb \"#{options[:database]}\" Please check the username and password."
    end
  end
  collection_name = options[:collection] || 'updater'
  unless db.collection_names.include? collection_name
    logger.warn "Updater MongoDB Driver is creating a new collection, \"#{collection_name}\" in \"#{options[:database]}\""
  end
  @collection = db.collection(collection_name)
end

Instance Method Details

#==(other) ⇒ Object



120
121
122
# File 'lib/updater/orm/mongo.rb', line 120

def ==(other)
  other.class == self.class && id == other.id
end

#[](arg) ⇒ Object

this allows easy mapping for time when a value coud either be U::ORM::Mongo or an ordered hash



69
70
71
# File 'lib/updater/orm/mongo.rb', line 69

def [](arg)  #this allows easy mapping for time when a value coud either be U::ORM::Mongo or an ordered hash
  @hash[arg]
end

#_idObject Also known as: id



30
31
32
# File 'lib/updater/orm/mongo.rb', line 30

def _id
  @hash['_id'] || @hash[:_id]
end

#_id=(val) ⇒ Object Also known as: id=



34
35
36
37
# File 'lib/updater/orm/mongo.rb', line 34

def _id=(val)
  val = BSON::ObjectId.from_string(val.to_s) unless val.kind_of? BSON::ObjectId
  @hash[:_id] = val
end

#collectionObject

Non API Standard. This method returns the collection used by this instance. This is used to create Job Chains



88
89
90
# File 'lib/updater/orm/mongo.rb', line 88

def collection
  self.class.instance_variable_get(:@collection)
end

#destroyObject



65
66
67
# File 'lib/updater/orm/mongo.rb', line 65

def destroy
  self.class.collection.remove({:_id=>id})
end

#lock(worker) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/updater/orm/mongo.rb', line 73

def lock(worker)
    return true if @lock_name && @lock_name == worker.name
    hash = Hash.new
    hash['findandmodify'] = self.class.collection.name
    hash['query'] = {:_id=>id,:lock_name=>nil}
    hash['update'] = {'$set'=>{:lock_name=>worker.name}}
    hash['new'] = true

    ret = self.class.db.command hash, :check_response=>false
    @lock_name = worker.name if ret['ok'] == 1
    return ret['ok'] == 1
end

#saveObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/updater/orm/mongo.rb', line 50

def save
  #todo validation
  [:failure,:success,:ensure].each do |mode|
    next unless @hash[mode]
    @hash[mode] = @hash[mode].map do |job|
      if job.kind_of? Updater::Update
        job.save unless job.id
        job = job.id
      end
      job
    end
  end
  _id = self.class.collection.save(@hash, :safe=>true)
end

#targetObject



42
43
44
# File 'lib/updater/orm/mongo.rb', line 42

def target
  @hash['target'].try :constantize
end

#target=(value) ⇒ Object



46
47
48
# File 'lib/updater/orm/mongo.rb', line 46

def target=(value)
  @hash['target'] = value.to_s
end