Module: Tengine::Core::SafeUpdatable

Extended by:
ActiveSupport::Concern
Included in:
Mutex::Mutex, OptimisticLock, Scheduler
Defined in:
lib/tengine/core/safe_updatable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.safemode(collection, wtimeout = 61440) ⇒ Object



30
31
32
33
34
35
# File 'lib/tengine/core/safe_updatable.rb', line 30

def safemode(collection, wtimeout=61440)
  collection.database.session.cluster.with_primary do |n|
    return n.peers.size.zero? ||
      { :w => "majority", :wtimeout => wtimeout, }
  end
end

Instance Method Details

#update_in_safe_mode(collection, selector, document, opts = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tengine/core/safe_updatable.rb', line 6

def update_in_safe_mode(collection, selector, document, opts=nil)
  options = { :upsert => false, :multiple => false }
  options.update(opts) if opts

  options = options.merge({ :safe => safemode(collection, 10240) })

  max_retries = 60
  retries = 0
  begin
    # Return a Hash containing the last error object if running safe mode.
    # Otherwise, returns true
    self.class.with(options).where(selector).update(document)
  rescue Moped::Errors::ConnectionFailure, Moped::Errors::OperationFailure => ex
    case ex when Moped::Errors::OperationFailure then
      raise ex unless ex.message =~ /wtimeout/
    end
    retries += 1
    raise ex if retries > max_retries
    Tengine.logger.debug "retrying due to mongodb error #{ex.inspect}, #{retries} times."
    sleep 0.5
    retry
  end
end