Class: LanGrove::Adaptor::QueryAdaptor

Inherits:
EventMachineAdaptor show all
Defined in:
lib/langrove/adaptor/query_adaptor.rb

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from EventMachineAdaptor

#path, #validate_tls_options

Methods inherited from Base

#listen, #validate_config_adaptor

Methods inherited from Base

#config_exception, #type

Constructor Details

#initialize(root, config, deamon_name) ⇒ QueryAdaptor

Returns a new instance of QueryAdaptor.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/langrove/adaptor/query_adaptor.rb', line 105

def initialize( root, config, deamon_name )
  
  super
  
  #
  # :run_interval: 
  # 
  #  A value in seconds between calling the query. 
  #           
  #  If the previous run is not finished a new run 
  #  will not start if the running?() method is 
  #  implemented.
  #  
  #  The run will be skipped and another :interval: 
  #  period will pass before the next run is 
  #  attempted.
  # 
  @run_interval = @config[:run_interval]
  
  
  #
  # :run_once: 
  # 
  #  If true will run the query only once and then 
  #  exit the Daemon.
  # 
  @run_once = false
  @run_once = @config[:run_once]
  @done_once = false
  
  #
  # :run_immediately: 
  # 
  #  If true (the default) will run the query at Daemon 
  #  start. Otherwise the run will wait :interval: seconds
  #  after daemon start.
  # 
  @run_immediately = @config[:run_immediately]
  @run_immediately = true if @run_immediately.nil?
  
  
  config_exception(
  
    "#{self.class} requires subconfig with :run_interval:"
  
  ) if (
    
    @run_interval.nil? or
    not @run_interval.is_a?( Integer )
    
  )
  
  @query = @config[:query]
  
  config_exception(
  
    "#{self.class} requires :query: subconfig"
  
  ) if @query.nil?
  
  validate_config
  
  connect

end

Instance Method Details

#connectObject



43
44
45
46
47
48
49
50
51
# File 'lib/langrove/adaptor/query_adaptor.rb', line 43

def connect
  
  #
  # OVERRIDE
  # 
  # To connect to the DataSource
  #
  
end

#connector(params, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/langrove/adaptor/query_adaptor.rb', line 171

def connector( params, &block )
  
  #
  # Override Handler type if it
  # is the default (Handler::Socket)
  # 
  # Does not override if the Handler 
  # is user specified.
  #
  if params[:handler][:class] == LanGrove::Handler::Socket
    
    params[:handler][:class] = LanGrove::Handler::Base
    
  end
  
  if @run_immediately
    
    do_query( params, &block )
    
  end
  
  EM.add_periodic_timer( @run_interval ) do
    
    do_query( params, &block )
    
  end
  
end

#do_query(params, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/langrove/adaptor/query_adaptor.rb', line 200

def do_query( params, &block )
    
  if running?
    
    @logger.info "#{self.class} already running query"
    
    return
    
  end
    
  if @run_once and @done_once
    
    @logger.warn "#{self.class} set to :run_once:"
    Process.kill( 'TERM', Process.pid )
    return
    
  end
  
  #
  # The query may be long blocking
  # so defer to allow the reactor
  # to proceed
  #
  operation = proc do
    
    query do |record|
      
      #
      # Initialize a new Handler
      #
      handler = params[:handler][:class].new
      
      #
      # Yield the handler into the daemon,
      # this will traverse the assignment 
      # pipeline resulting in the handler
      # interacting with all configured
      # server behaviours
      #
      yield handler if block
      
      #
      # Pass the record in along the 'reveive'
      # along with associated event triggers.
      # 
      # (For behaviours)
      #
      @root.trigger( handler, :handler, :receive )
      
      handler.receive( record )
      
      @root.trigger( handler, :handler, :after_receive )
  
    end
    
  end
  
  callback = proc do |result|
    
    @done_once = true
    
  end
  
  EM.defer( operation, callback )
  
end

#query(&block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/langrove/adaptor/query_adaptor.rb', line 84

def query( &block )
  
  #
  # OVERRIDE
  #
  # To perform the query and yield resulting 'rows'
  # 
  # - Should yield one hash of key/value pairs
  #   per 'row'
  # 
  
  raise LanGrove::PluginException.new( 
  
    "#{self.class}.query( ... ) - missing implementation."  
    
  )
  
end

#reloadObject



23
24
25
26
27
28
29
30
31
# File 'lib/langrove/adaptor/query_adaptor.rb', line 23

def reload

  #
  # OVERRIDE
  # 
  # Daemon has been called to HUP
  #

end

#reload_adaptorObject



78
79
80
81
82
# File 'lib/langrove/adaptor/query_adaptor.rb', line 78

def reload_adaptor

  reload

end

#running?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/langrove/adaptor/query_adaptor.rb', line 53

def running?
  
  #
  # OVERRIDE
  # 
  # (optional)
  # 
  # Because the query may run asyncronously and 
  # emply callbacks, this flag should be set so
  # that it can be determined if a query is active
  # in the background.
  # 
  # Only required if it is necessary to ensure that
  # never more than 1 concurrent query is running.
  # 
  false
  
end

#stopObject

Used to periodically query a datasource, Yields rows as handlers into the daemon’s event pipeline



13
14
15
16
17
18
19
20
21
# File 'lib/langrove/adaptor/query_adaptor.rb', line 13

def stop
  
  #
  # OVERRIDE
  #
  # Daemon has been called to stop
  #

end

#stop_adaptorObject



72
73
74
75
76
# File 'lib/langrove/adaptor/query_adaptor.rb', line 72

def stop_adaptor

  stop

end

#validate_configObject



33
34
35
36
37
38
39
40
41
# File 'lib/langrove/adaptor/query_adaptor.rb', line 33

def validate_config
  
  #
  # OVERRIDE
  # 
  # To validate implementation specific config
  #
  
end