class EasyE::Plugin::MongoPlugin

Constants

WIRED_TIGER_KEY

Attributes

client[RW]

Public Instance Methods

after() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 63
def after
  return logger.error "Refusing to operate" if carefully('check whether this node is a primary') { primary? }.nil?
  return logger.error "This appears to be a primary member, refusing to operate" if primary?
  
  if wired_tiger?
    carefully('start mongo') { start_mongo } if options.shutdown
  else
    carefully('unlock mongo') { unlock_mongo }
  end

  if carefully('check that mongo is still accessible') { client.command(serverStatus: 1).first }
    logger.info "Received status from mongo, everything appears to be ok"
  end
end
before() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 48
def before
  require 'mongo'
  Mongo::Logger.logger = logger
  return logger.error "Refusing to operate" if carefully('check whether this node is a primary') { primary? }.nil?
  return logger.error "This appears to be a primary member, refusing to operate" if primary?

  if wired_tiger?
    logger.info "Wired Tiger storage engine detected"
    carefully('shutdown mongo') { shutdown_mongo } if options.shutdown
  else
    logger.info "MMAPv1 storage engine detected"
    carefully('lock mongo') { lock_mongo }
  end
end
client_options() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 37
def client_options
  {
    user: options.user,
    password: options.password,
    server_selection_timeout: options.server_selection_timeout.to_i,
    wait_queue_timeout: options.wait_queue_timeout.to_i,
    connection_timeout: options.connection_timeout.to_i,
    socket_timeout: options.socket_timeout.to_i
  }
end
default_options() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 20
def default_options
  {
    service: 'mongodb',
    port: '27017',
    shutdown: false,
    host: 'localhost',
    server_selection_timeout: 30,
    wait_queue_timeout: 1,
    connection_timeout: 5,
    socket_timeout: 5
  }
end
defined_options() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 5
def defined_options
  {
    service: 'Service to start after shutting down server',
    shutdown: 'Shutdown mongodb server (this is required if your data and journal are on different volumes',
    user: 'Mongo user',
    password: 'Mongo password',
    port: 'Mongo port',
    host: 'Mongo host',
    server_selection_timeout: 'Timeout in seconds while choosing a server to connect to (default 30)',
    wait_queue_timeout: 'Timeout in seconds while waiting for a connection in the pool (default 1)',
    connection_timeout: 'Timeout in seconds to wait for a socket to connect (default 5)',
    socket_timeout: 'Timeout in seconds to wait for an operation to execute on a socket (default 5)'
  }
end
name() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 78
def name
  "Mongo"
end

Private Instance Methods

lock_mongo() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 116
def lock_mongo
  logger.info "Locking mongo"
  client.command(fsync: 1, lock: true)
end
primary?() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 91
def primary?
  if @primary.nil?
    @primary = client.command(isMaster: 1).first['ismaster']
  end
  @primary
end
shutdown_mongo() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 98
def shutdown_mongo
  logger.info 'Shutting down mongodb'
  begin
    # this will always raise an exception after it completes
    client.command shutdown: 1
  rescue Mongo::Error::SocketError => e
    logger.debug "Received expected socket error after shutting down"
  end

  # we need a new connection now since the server has shut down
  @client = nil
end
start_mongo() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 111
def start_mongo
  logger.info "Starting mongodb via 'service #{options[:service]} start'"
  system "service #{options[:service]} start"
end
unlock_mongo() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 121
def unlock_mongo
  logger.info "Unlocking mongo"
  client.database['$cmd.sys.unlock'].find().first
end
wired_tiger?() click to toggle source
# File lib/plugins/mongo_plugin.rb, line 84
def wired_tiger?
  if @wired_tiger.nil?
    @wired_tiger = client.command(serverStatus: 1).first.has_key? WIRED_TIGER_KEY
  end
  @wired_tiger
end