Class: ForestAdminAgent::Builder::AgentFactory

Inherits:
Object
  • Object
show all
Includes:
Http::Exceptions, Utils::Schema, ForestAdminDatasourceCustomizer::DSL::DatasourceHelpers, ForestAdminDatasourceToolkit::Exceptions, Singleton
Defined in:
lib/forest_admin_agent/builder/agent_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



13
14
15
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 13

def container
  @container
end

#customizerObject (readonly)

Returns the value of attribute customizer.



13
14
15
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 13

def customizer
  @customizer
end

#has_env_secretObject (readonly)

Returns the value of attribute has_env_secret.



13
14
15
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 13

def has_env_secret
  @has_env_secret
end

Instance Method Details

#add_chart(name, &definition) ⇒ Object



34
35
36
37
38
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 34

def add_chart(name, &definition)
  @customizer.add_chart(name, &definition)

  self
end

#add_datasource(datasource, options = {}) ⇒ Object



24
25
26
27
28
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 24

def add_datasource(datasource, options = {})
  @customizer.add_datasource(datasource, options)

  self
end

#buildObject



52
53
54
55
56
57
58
59
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 52

def build
  @container.register(:datasource, @customizer.datasource(@logger))

  # Reset route cache to ensure routes are computed with all customizations
  ForestAdminAgent::Http::Router.reset_cached_routes!

  send_schema
end

#build_schema(datasource) ⇒ Hash

Builds the schema hash from the datasource

Parameters:

  • datasource (Object)

    The datasource to generate schema from

Returns:

  • (Hash)

    The schema hash with :meta and :collections keys



126
127
128
129
130
131
132
133
134
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 126

def build_schema(datasource)
  generated = SchemaEmitter.generate(datasource)
  meta = SchemaEmitter.meta

  {
    meta: meta,
    collections: generated
  }
end

#customize_collection(name, &handle) ⇒ Object



40
41
42
43
44
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 40

def customize_collection(name, &handle)
  @customizer.customize_collection(name, &handle)

  self
end

#generate_schema_fileHash

Generates or loads the schema and writes it to file (in development mode). This method can be overridden by subclasses that need to customize schema handling.

Returns:

  • (Hash)

    The schema hash with :meta and :collections keys



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 103

def generate_schema_file
  schema_path = Facades::Container.cache(:schema_path)

  if 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

    JSON.parse(File.read(schema_path), symbolize_names: true)
  else
    datasource = @container.resolve(:datasource)
    schema = build_schema(datasource)
    write_schema_file(schema_path, schema)
    schema
  end
end

#reload!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 61

def reload!
  begin
    @customizer.reload!(logger: @logger)
  rescue StandardError => e
    @logger.log('Error', "Error reloading agent: #{e.message}")
    return
  end

  @container.register(:datasource, @customizer.datasource(@logger), replace: true)

  # Reset route cache before sending schema to ensure routes are recomputed with all customizations
  ForestAdminAgent::Http::Router.reset_cached_routes!
  @logger.log('Info', 'route cache cleared due to agent reload')

  send_schema
end

#remove_collection(names) ⇒ Object



30
31
32
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 30

def remove_collection(names)
  @customizer.remove_collection(names)
end

#send_schema(force: false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 78

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

  return unless @has_env_secret

  schema = generate_schema_file

  if (append_schema_path = Facades::Container.cache(:append_schema_path))
    begin
      append_schema_file = JSON.parse(File.read(append_schema_path), symbolize_names: true)
      schema[:collections] = schema[:collections] + append_schema_file[:collections]
    rescue StandardError => e
      raise "Can't load additional schema #{append_schema_path}: #{e.message}"
    end
  end

  post_schema(schema, force)
end

#setup(options) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 15

def setup(options)
  @options = options
  @has_env_secret = options.to_h.key?(:env_secret)
  @customizer = ForestAdminDatasourceCustomizer::DatasourceCustomizer.new
  build_container
  build_cache
  build_logger
end

#use(plugin, options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 46

def use(plugin, options = {})
  @customizer.use(plugin, options)

  self
end

#write_schema_file(schema_path, schema) ⇒ Object

Writes the schema to a file

Parameters:

  • schema_path (String)

    Path to write the schema file

  • schema (Hash)

    The schema to write



139
140
141
# File 'lib/forest_admin_agent/builder/agent_factory.rb', line 139

def write_schema_file(schema_path, schema)
  File.write(schema_path, format_schema_json(schema))
end