Class: Rhoconnect::Handler::Query::Engine

Inherits:
Object
  • Object
show all
Includes:
Helpers::AuthMethod, Helpers::Binding, Helpers::SourceJob
Defined in:
lib/rhoconnect/handler/query/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Binding

#bind_handler

Methods included from Helpers::SourceJob

#async

Methods included from Helpers::AuthMethod

#auth_method

Constructor Details

#initialize(model, route_handler, params = {}) ⇒ Engine

Returns a new instance of Engine.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rhoconnect/handler/query/engine.rb', line 11

def initialize(model, route_handler, params = {})
  raise ArgumentError.new(UNKNOWN_SOURCE) unless (model and model.source)
  raise ArgumentError.new('Invalid app for source') unless model.source.app
  raise ArgumentError.new('Invalid sync handler') unless route_handler
  # if handler is not bound - bind it to self
# normally it should be bound to a Controller's instance

  @source = model.source
  @route_handler = bind_handler(:sync_handler_method, route_handler)  
  @params = params
  @model = model
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/rhoconnect/handler/query/engine.rb', line 5

def model
  @model
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/rhoconnect/handler/query/engine.rb', line 5

def params
  @params
end

#route_handlerObject

Returns the value of attribute route_handler.



5
6
7
# File 'lib/rhoconnect/handler/query/engine.rb', line 5

def route_handler
  @route_handler
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/rhoconnect/handler/query/engine.rb', line 5

def source
  @source
end

Instance Method Details

#_execute_queryObject



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
# File 'lib/rhoconnect/handler/query/engine.rb', line 50

def _execute_query
  errordoc = nil
  docobj = nil
  result = nil
  begin
    errordoc = :errors
    docobj = @source
    [:metadata,:schema].each do |method|
      _get_model_data(method)
    end
    @model.before_query
    @route_handler.call
    result = @model.after_query
    # operation,sync succeeded, remove errors
    docobj.lock(errordoc) do
      docobj.flush_data(errordoc)
    end
  rescue Exception => e
    # store sync,operation exceptions to be sent to all clients for this source/user
    log "Model raised query exception: #{e}"
    log e.backtrace.join("\n")
    docobj.lock(errordoc) do
      docobj.put_data(errordoc,{"query-error"=>{'message'=>e.message}},true)
    end
  end
  # pass through expects result hash
  @source.is_pass_through? ? result : true
end

#_get_model_data(method) ⇒ Object

Metadata Operation; source model returns json



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rhoconnect/handler/query/engine.rb', line 80

def _get_model_data(method)
  if @model.respond_to?(method)
    data = @model.send(method)        
    if data
      @source.put_value(method,data)
      if method == :schema
        parsed = JSON.parse(data)
        schema_version = parsed['version']
        raise "Mandatory version key is not defined in model schema method" if schema_version.nil?
        @source.put_value("#{method}_sha1",Digest::SHA1.hexdigest(schema_version))
      else
        @source.put_value("#{method}_sha1",Digest::SHA1.hexdigest(data))
      end          
    end
  end
end

#do_syncObject



24
25
26
27
28
29
30
31
32
# File 'lib/rhoconnect/handler/query/engine.rb', line 24

def do_sync
  query_res = nil
  if source.query_queue or source.queue
    query_res = async(:query, source.query_queue || source.queue, params[:query])
  else
    query_res = run_query
  end
  query_res
end

#run_queryObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rhoconnect/handler/query/engine.rb', line 34

def run_query
  query_res = nil
  source.if_need_refresh do
    Rhoconnect::Stats::Record.update("source:query:#{source.name}") do
      if auth_method('login')
        query_res = _execute_query
        auth_method('logoff')
      end
      # re-wind refresh time in case of error
      query_failure = source.exists?(:errors)
      source.rewind_refresh_time(query_failure)
    end
  end
  query_res
end