Class: Flydata::QueryBasedSync::ResourceRequester

Inherits:
Object
  • Object
show all
Includes:
FlydataCore::Logger
Defined in:
lib/flydata/query_based_sync/resource_requester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ResourceRequester

Returns a new instance of ResourceRequester.


9
10
11
12
# File 'lib/flydata/query_based_sync/resource_requester.rb', line 9

def initialize(context)
  @context = context
  @resource_client = create_resource_client(context)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context


14
15
16
# File 'lib/flydata/query_based_sync/resource_requester.rb', line 14

def context
  @context
end

Instance Method Details

#create_resource_client(context) ⇒ Object

Override Return a resource client object ex: pg_client, mysql_connection


55
56
57
# File 'lib/flydata/query_based_sync/resource_requester.rb', line 55

def create_resource_client(context)
  raise "Not implemented"
end

#each_response(table_name, interval = 1) ⇒ Object

Fetch resources for a table It may call the callback multiple times in the following cases

- The resource size is bigger than the max size per request
- Resume to fetch resources if resume info exists in per-table position file

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flydata/query_based_sync/resource_requester.rb', line 36

def each_response(table_name, interval = 1)
  latest_src_pos = context.source_pos_class.new(context.table_meta.current_snapshot)

  loop do
    responses = fetch_responses_once(table_name, latest_src_pos)
    break if responses.nil? || responses.empty?
    responses.each do |response|
      yield response
    end
    break if responses.last.new_source_pos >= latest_src_pos
    sleep interval  # to avoid rush
  end

  nil
end

#fetch_responses_once(table_name, latest_src_pos) ⇒ Object

Override Fetch the resource and return a response object Returned object must be kind of Flydata::QueryBasedSync::Response


62
63
64
# File 'lib/flydata/query_based_sync/resource_requester.rb', line 62

def fetch_responses_once(table_name, latest_src_pos)
  raise "Not implemented"
end

#startObject

Start fetching resources by keeping a same connection Caller needs to call methods using the connection with block

ex:

requester.start do |req|
  req.fetch_resources(table_name) do |response|
    handle(response)
  end
end

25
26
27
28
29
30
# File 'lib/flydata/query_based_sync/resource_requester.rb', line 25

def start
  context.table_meta.reload(@resource_client)
  yield self
ensure
  @resource_client.close
end