Module: ForestAdminDatasourceRpc

Defined in:
lib/forest_admin_datasource_rpc.rb,
lib/forest_admin_datasource_rpc/version.rb,
lib/forest_admin_datasource_rpc/collection.rb,
lib/forest_admin_datasource_rpc/datasource.rb,
lib/forest_admin_datasource_rpc/Utils/rpc_client.rb,
lib/forest_admin_datasource_rpc/Utils/sse_client.rb

Defined Under Namespace

Modules: Utils Classes: Collection, Datasource, Error

Constant Summary collapse

VERSION =
"1.6.0"

Class Method Summary collapse

Class Method Details

.build(options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/forest_admin_datasource_rpc.rb', line 11

def self.build(options)
  uri = options[:uri]
  auth_secret = ForestAdminRpcAgent::Facades::Container.cache(:auth_secret)
  ForestAdminRpcAgent::Facades::Container.logger.log('Info', "Getting schema from RPC agent on #{uri}.")

  begin
    rpc_client = Utils::RpcClient.new(uri, auth_secret)
    schema = rpc_client.call_rpc('/forest/rpc-schema', method: :get, symbolize_keys: true)
    last_hash_schema = Digest::SHA1.hexdigest(schema.to_h.to_s)
  rescue StandardError
    ForestAdminRpcAgent::Facades::Container.logger.log(
      'Error',
      'Failed to get schema from RPC agent. Please check the RPC agent is running.'
    )
  end

  if schema.nil?
    # return empty datasource for not breaking stack
    ForestAdminDatasourceToolkit::Datasource.new
  else
    sse = Utils::SseClient.new("#{uri}/forest/rpc/sse", auth_secret) do
      ForestAdminRpcAgent::Facades::Container.logger.log('Info', 'RPC server stopped, checking schema...')
      new_schema = rpc_client.call_rpc('/forest/rpc-schema', method: :get, symbolize_keys: true)

      if last_hash_schema == Digest::SHA1.hexdigest(new_schema.to_h.to_s)
        ForestAdminRpcAgent::Facades::Container.logger.log('Debug', '[RPCDatasource] Schema has not changed')
      else
        ForestAdminAgent::Builder::AgentFactory.instance.reload!
      end
    end
    sse.start

    ForestAdminDatasourceRpc::Datasource.new(options, schema)
  end
end