Class: ForestAdminRpcAgent::Http::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_rpc_agent/http/router.rb

Class Method Summary collapse

Class Method Details

.cache_disabled?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/forest_admin_rpc_agent/http/router.rb', line 27

def self.cache_disabled?
  config = ForestAdminRpcAgent::Facades::Container.config_from_cache
  config&.dig(:disable_route_cache) == true
rescue StandardError
  # Config not available, default to caching enabled
  false
end

.cached_route_instancesObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/forest_admin_rpc_agent/http/router.rb', line 7

def self.cached_route_instances
  return route_instances.freeze if cache_disabled?

  return @cached_route_instances if @cached_route_instances

  @mutex.synchronize do
    @cached_route_instances ||= begin
      start_time = Time.now
      computed_routes = route_instances
      elapsed = ((Time.now - start_time) * 1000).round(2)

      log_message = "[ForestAdminRpcAgent] Computed #{computed_routes.size} routes " \
                    "in #{elapsed}ms (caching enabled)"
      log_to_available_logger('Info', log_message)

      computed_routes.freeze
    end
  end
end

.log_to_available_logger(level, message) ⇒ Object



41
42
43
44
45
# File 'lib/forest_admin_rpc_agent/http/router.rb', line 41

def self.log_to_available_logger(level, message)
  ForestAdminRpcAgent::Facades::Container.logger.log(level, message)
rescue StandardError
  puts message
end

.reset_cached_route_instances!Object



35
36
37
38
39
# File 'lib/forest_admin_rpc_agent/http/router.rb', line 35

def self.reset_cached_route_instances!
  @mutex.synchronize do
    @cached_route_instances = nil
  end
end

.route_instancesObject



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/forest_admin_rpc_agent/http/router.rb', line 47

def self.route_instances
  route_classes = ForestAdminRpcAgent::Routes.constants.reject { |route| route.to_s == 'BaseRoute' }

  route_instances = []

  route_classes.each do |route_name|
    route_class = ForestAdminRpcAgent::Routes.const_get(route_name)

    # Skip if it's not a class or if it doesn't look like a route
    unless route_class.is_a?(Class)
      log_to_available_logger(
        'Warn',
        "Skipping constant: #{route_name} (not a class)"
      )
      next
    end

    begin
      route_instance = route_class.new

      unless route_instance.respond_to?(:registered)
        log_to_available_logger(
          'Warn',
          "Skipping route: #{route_class} (does not respond to :registered)"
        )
        next
      end

      route_instances << route_instance
    rescue ArgumentError => e
      # Skip classes that require constructor arguments (not routes)
      log_to_available_logger(
        'Warn',
        "Skipping constant: #{route_name} (requires constructor arguments: #{e.message})"
      )
      next
    rescue StandardError => e
      raise e.class, "Failed to instantiate route '#{route_name}': #{e.message}"
    end
  end

  route_instances
end