Class: Flydata::QueryBasedSync::ResourceRequester
- Inherits:
-
Object
- Object
- Flydata::QueryBasedSync::ResourceRequester
- Includes:
- FlydataCore::Logger
- Defined in:
- lib/flydata/query_based_sync/resource_requester.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
-
#create_resource_client(context) ⇒ Object
Override Return a resource client object ex: pg_client, mysql_connection.
-
#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.
-
#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.
-
#initialize(context) ⇒ ResourceRequester
constructor
A new instance of ResourceRequester.
-
#start ⇒ Object
Start fetching resources by keeping a same connection Caller needs to call methods using the connection with block.
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
#context ⇒ Object (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..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 |
#start ⇒ Object
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..reload(@resource_client) yield self ensure @resource_client.close end |