Class: Plugin::MongoDB

Inherits:
LanGrove::Plugin::Persistor
  • Object
show all
Defined in:
lib/plugin/mongo_db.rb

Instance Method Summary collapse

Instance Method Details

#all_capsules(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/plugin/mongo_db.rb', line 30

def all_capsules( &block )
  
  #
  # raise Exception on failed (should bring down the daemon)
  # 
  @collection.find().to_a.each do |capsule|
  
    yield capsule['_id'], capsule
  
  end
  
end

#connectObject



20
21
22
23
24
25
26
27
# File 'lib/plugin/mongo_db.rb', line 20

def connect
  
  @logger.info "Connecting to mongo #{@database}.#{@table}"
  @connection = Mongo::Connection.new
  @db = @connection.db( @database )
  @collection = @db.collection( @table )
  
end

#fetch(handler) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/plugin/mongo_db.rb', line 77

def fetch( handler )
  
  gen_key( handler ) if handler.key.nil?
  
  id = handler.key['key']['_id']
  
  begin 
  
    capsule = @collection.find_one('_id' => id )
    
    if capsule.nil?
      
      unless @allow_learn.nil?

        handler.capsule = {}
        
        @logger.warn( "WARN: Assumed NEW capsule for #{@table}._id: #{id}" )

        return true
        
      else
        
        @logger.error "ERROR: Ignored #{@table}._id: #{id}. Enable :allow_learn:"
        
        return false
        
      end

    end
  
  rescue Mongo::ConnectionFailure => e
  
    @logger.error "ERROR: on store() #{e}"
  
    connect
    
    retry
    
  end
  
  handler.capsule = capsule
  
  return true
  
end

#store(handler) ⇒ Object



44
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
# File 'lib/plugin/mongo_db.rb', line 44

def store( handler )
  
  gen_key( handler ) if handler.key.nil?

  begin
    
    id = handler.key['key']['_id']
    
    @collection.update(
    
      {'_id' => id }, 
      handler.capsule, 
      :upsert  => true
      
    )
    
    @logger.debug "Wrote #{handler.class} to mongo:#{@database}.#{@table} at _id: #{id}"
  
  rescue Mongo::ConnectionFailure => e
  
    @logger.error "ERROR: on fetch() #{e}"
  
    connect
    
    retry
    
  end
  
  true
  
end

#validate_configObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/plugin/mongo_db.rb', line 7

def validate_config
  
  super
  
  connect
  
  #
  # Polulate this from :db:username: and :db:password
  #
  # auth = db.authenticate(username, password)
  
end