Class: ForestAdminRpcAgent::Agent

Inherits:
ForestAdminAgent::Builder::AgentFactory
  • Object
show all
Includes:
ForestAdminAgent::Http::Exceptions
Defined in:
lib/forest_admin_rpc_agent/agent.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cached_schemaObject (readonly)

Returns the value of attribute cached_schema.



8
9
10
# File 'lib/forest_admin_rpc_agent/agent.rb', line 8

def cached_schema
  @cached_schema
end

#cached_schema_hashObject (readonly)

Returns the value of attribute cached_schema_hash.



8
9
10
# File 'lib/forest_admin_rpc_agent/agent.rb', line 8

def cached_schema_hash
  @cached_schema_hash
end

#rpc_collectionsObject (readonly)

Returns the value of attribute rpc_collections.



8
9
10
# File 'lib/forest_admin_rpc_agent/agent.rb', line 8

def rpc_collections
  @rpc_collections
end

Instance Method Details

#mark_collections_as_rpc(*names) ⇒ Object



55
56
57
58
# File 'lib/forest_admin_rpc_agent/agent.rb', line 55

def mark_collections_as_rpc(*names)
  @rpc_collections.push(*names)
  self
end

#rpc_schemaObject

Returns the cached schema for the /rpc-schema route Falls back to building schema from datasource if not cached



62
63
64
65
66
67
# File 'lib/forest_admin_rpc_agent/agent.rb', line 62

def rpc_schema
  return @cached_schema if @cached_schema

  build_and_cache_schema_from_datasource
  @cached_schema
end

#schema_hash_matches?(provided_hash) ⇒ Boolean

Check if provided hash matches the cached schema hash

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/forest_admin_rpc_agent/agent.rb', line 70

def schema_hash_matches?(provided_hash)
  return false unless @cached_schema_hash && provided_hash

  @cached_schema_hash == provided_hash
end

#send_schema(force: false) ⇒ 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
# File 'lib/forest_admin_rpc_agent/agent.rb', line 17

def send_schema(force: false)
  if should_skip_schema_update? && !force
    log_schema_skip
    load_and_cache_schema
    return
  end

  schema_path = ForestAdminRpcAgent::Facades::Container.cache(:schema_path)

  if ForestAdminRpcAgent::Facades::Container.cache(:is_production)
    unless schema_path && File.exist?(schema_path)
      raise InternalServerError.new(
        'Schema file not found in production',
        details: { schema_path: schema_path }
      )
    end

    load_and_cache_schema_from_file(schema_path)

    ForestAdminRpcAgent::Facades::Container.logger.log(
      'Info',
      'RPC agent running in production mode, using existing schema file.'
    )
  else
    generate_and_cache_schema(schema_path)

    ForestAdminRpcAgent::Facades::Container.logger.log(
      'Info',
      "RPC agent schema generated and saved to #{schema_path}"
    )
  end

  ForestAdminRpcAgent::Facades::Container.logger.log(
    'Info',
    'RPC agent does not send schema to Forest Admin servers.'
  )
end

#setup(options) ⇒ Object



10
11
12
13
14
15
# File 'lib/forest_admin_rpc_agent/agent.rb', line 10

def setup(options)
  super
  @rpc_collections = []
  @cached_schema = nil
  @cached_schema_hash = nil
end