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/reconciliate_rpc.rb,
lib/forest_admin_datasource_rpc/Utils/schema_polling_pool.rb,
lib/forest_admin_datasource_rpc/Utils/schema_polling_client.rb

Defined Under Namespace

Modules: Utils Classes: Collection, Datasource, Error, ReconciliateRpc

Constant Summary collapse

KNOWN_BUILD_OPTIONS =
%i[uri auth_secret introspection schema_polling_interval_sec].freeze
VERSION =
"1.43.0"

Class Method Summary collapse

Class Method Details

.build(options) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/forest_admin_datasource_rpc.rb', line 17

def self.build(options)
  unknown_keys = options.keys - KNOWN_BUILD_OPTIONS
  if unknown_keys.any?
    ForestAdminAgent::Facades::Container.logger.log(
      'Warn',
      "[RPCDatasource] Unknown option(s) passed to build: #{unknown_keys.join(", ")}. " \
      "Known options are: #{KNOWN_BUILD_OPTIONS.join(", ")}"
    )
  end

  uri = options[:uri]
  auth_secret = options[:auth_secret] || ForestAdminAgent::Facades::Container.cache(:auth_secret)
  provided_introspection = options[:introspection]

  polling_interval = options[:schema_polling_interval_sec] ||
                     ENV['SCHEMA_POLLING_INTERVAL_SEC']&.to_i ||
                     600

  # Auto-configure pool with default settings if not already configured
  ensure_pool_configured

  datasource_ref = nil

  schema_polling = Utils::SchemaPollingClient.new(
    uri,
    auth_secret,
    polling_interval: polling_interval,
    introspection: provided_introspection
  ) do |new_schema|
    Thread.new do
      logger = ForestAdminAgent::Facades::Container.logger
      logger.log('Info', '[RPCDatasource] Schema change detected, reloading agent in background...')
      begin
        datasource_ref&.refresh!(new_schema)
        ForestAdminAgent::Builder::AgentFactory.instance.reload!
        logger.log('Info', '[RPCDatasource] Agent reload completed successfully')
      rescue StandardError => e
        logger.log('Error', "[RPCDatasource] Agent reload failed: #{e.class} - #{e.message}")
      end
    end
  end

  # Start polling (includes initial synchronous schema fetch)
  # - Without introspection: crashes if RPC is unreachable
  # - With introspection: falls back to introspection if RPC is unreachable
  schema_polling.start?

  schema = schema_polling.current_schema
  if schema.nil?
    raise ForestAdminDatasourceToolkit::Exceptions::ForestException,
          'Fatal: Unable to build RPC datasource - no introspection schema was provided and schema fetch failed'
  end

  options.delete(:introspection)
  datasource_ref = ForestAdminDatasourceRpc::Datasource.new(options, schema, schema_polling)
end

.configure_polling_pool(max_threads:) ⇒ Object



11
12
13
# File 'lib/forest_admin_datasource_rpc.rb', line 11

def self.configure_polling_pool(max_threads:)
  Utils::SchemaPollingPool.instance.configure(max_threads: max_threads)
end