Class: YogiBerra::Catcher

Inherits:
Object
  • Object
show all
Extended by:
Facets
Defined in:
lib/yogi_berra/catcher.rb

Class Method Summary collapse

Methods included from Facets

cattr, cattr_accessor, cattr_reader, cattr_writer, mattr, mattr_accessor, mattr_reader, mattr_writer

Class Method Details

.connect(load_settings = false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/yogi_berra/catcher.rb', line 81

def connect(load_settings = false)
  load_db_settings if load_settings

  if @@settings
    fork_database
  else
    YogiBerra::Logger.log("Couldn't load the yogi.yml file.", :error) if load_settings
  end
  @@connection
end

.environmentObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yogi_berra/catcher.rb', line 33

def environment
  if ENV["YOGI_ENV"]
    ENV["YOGI_ENV"]
  elsif defined?(Rails)
    Rails.env
  elsif ENV["RAILS_ENV"]
    ENV["RAILS_ENV"]
  else
    "test"
  end
end

.fork_databaseObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/yogi_berra/catcher.rb', line 45

def fork_database
  host = @@settings["host"]
  port = @@settings["port"]
  database = @@settings["database"]
  username = @@settings["username"]
  password = @@settings["password"]
  replica_set = @@settings["replica_set"]

  # :w => 0 set the default write concern to 0, this allows writes to be non-blocking
  # by not waiting for a response from mongodb
  Thread.new do
    begin
      if replica_set
        @@mongo_client = Mongo::MongoReplicaSetClient.new(replica_set, :w => 0, :connect_timeout => 10)
      else
        @@mongo_client = Mongo::MongoClient.new(host, port, :w => 0, :connect_timeout => 10)
      end
    rescue Timeout::Error => timeout_error
      YogiBerra::Logger.log("Couldn't connect to the mongo database timeout on host: #{host} port: #{port}.\n #{timeout_error.inspect}", :error)
      retry
    rescue => error
      YogiBerra::Logger.log("Couldn't connect to the mongo database on host: #{host} port: #{port}.\n #{error.inspect}", :error)
      retry
    end

    @@connection = @@mongo_client[database]
    if username && password
      begin
        @@connection.authenticate(username, password)
      rescue
        YogiBerra::Logger.log("Couldn't authenticate with user #{username} to mongo database on host: #{host} port: #{port} database: #{database}.", :warn)
      end
    end
  end
end

.load_db_settings(config_file = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/yogi_berra/catcher.rb', line 12

def load_db_settings(config_file = nil)
  if config_file
    database_config = config_file
  elsif defined?(Rails)
    database_config = "#{Rails.root}/config/yogi.yml"
  else
    YogiBerra::Logger.log("No config file specified!", :error)
  end
  if database_config
    begin
      File.open(database_config, 'r') do |f|
        yaml_file = YAML.load(f)
        @@settings = yaml_file["#{environment}"] if yaml_file
      end
    rescue
      YogiBerra::Logger.log("No such file: #{database_config}", :error)
    end
    @@settings
  end
end