Class: SplitIoClient::SplitFactory

Inherits:
Object
  • Object
show all
Includes:
Cache::Fetchers, Cache::Repositories, Cache::Senders, Cache::Stores
Defined in:
lib/splitclient-rb/split_factory.rb

Constant Summary collapse

ROOT_PROCESS_ID =
Process.pid
SINGLETON_WARN =
'We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application'
LOCALHOST_API_KEY =
'localhost'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config_hash = {}) ⇒ SplitFactory

Returns a new instance of SplitFactory.



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/splitclient-rb/split_factory.rb', line 16

def initialize(api_key, config_hash = {})
  at_exit do
    unless ENV['SPLITCLIENT_ENV'] == 'test'
      if (Process.pid == ROOT_PROCESS_ID)
        @config.logger.info('Split SDK shutdown started...')
        @client.destroy if @client
        stop!
        @config.logger.info('Split SDK shutdown complete')
      end
    end
  end

  @api_key = api_key

  store_flag_sets = config_hash.key?(:flag_sets_filter) ? config_hash[:flag_sets_filter] : []
  store_flag_sets = [] if !store_flag_sets.is_a?(Array)
  flag_sets_count = 0
  flag_sets_invalid = 0

  @config = SplitConfig.new(config_hash.merge(localhost_mode: @api_key == LOCALHOST_API_KEY ))

  if config_hash.key?(:flag_sets_filter)
    flag_sets_count = store_flag_sets.length()
    flag_sets_invalid = flag_sets_count - @config.flag_sets_filter.length()
  end

  raise 'Invalid SDK mode' unless valid_mode

  validate_api_key

  register_factory

  build_telemetry_components
  build_flag_sets_filter
  build_repositories
  build_telemetry_synchronizer(flag_sets_count, flag_sets_invalid)
  build_impressions_sender_adapter
  build_unique_keys_tracker
  build_impressions_components

  @status_manager = Engine::StatusManager.new(@config)
  @split_validator = SplitIoClient::Validators.new(@config)
  @evaluator = Engine::Parser::Evaluator.new(@segments_repository, @splits_repository, @config)

  start!

  @client = SplitClient.new(@api_key, repositories, @status_manager, @config, @impressions_manager, @evaluation_producer, @evaluator, @split_validator)
  @manager = SplitManager.new(@splits_repository, @status_manager, @config)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



14
15
16
# File 'lib/splitclient-rb/split_factory.rb', line 14

def adapter
  @adapter
end

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/splitclient-rb/split_factory.rb', line 14

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/splitclient-rb/split_factory.rb', line 14

def config
  @config
end

#managerObject (readonly)

Returns the value of attribute manager.



14
15
16
# File 'lib/splitclient-rb/split_factory.rb', line 14

def manager
  @manager
end

Instance Method Details

#register_factoryObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/splitclient-rb/split_factory.rb', line 89

def register_factory
  SplitIoClient.load_factory_registry

  number_of_factories = SplitIoClient.split_factory_registry.number_of_factories_for(@api_key)

  if(number_of_factories > 0)
    @config.logger.warn("Factory instantiation: You already have #{number_of_factories} factories with this API Key. #{SINGLETON_WARN}")
  elsif(SplitIoClient.split_factory_registry.other_factories)
    @config.logger.warn('Factory instantiation: You already have an instance of the Split factory.' \
      " Make sure you definitely want this additional instance. #{SINGLETON_WARN}")
  end

  SplitIoClient.split_factory_registry.add(@api_key)
end

#start!Object Also known as: resume!



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/splitclient-rb/split_factory.rb', line 66

def start!
  return start_localhost_components if @config.localhost_mode

  if @config.consumer?
    build_synchronizer
    build_sync_manager

    @sync_manager.start_consumer
    return
  end

  build_fetchers
  build_synchronizer
  build_streaming_components
  build_sync_manager

  @sync_manager.start
end

#stop!Object



85
86
87
# File 'lib/splitclient-rb/split_factory.rb', line 85

def stop!
  @config.threads.each { |_, t| t.exit }
end

#valid_modeObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/splitclient-rb/split_factory.rb', line 104

def valid_mode
  valid_startup_mode = false
  case @config.mode
  when :consumer
    if @config.cache_adapter.is_a? SplitIoClient::Cache::Adapters::RedisAdapter
      if !@config.localhost_mode
        valid_startup_mode = true
      else
        @config.logger.error('Localhost mode cannot be used with Redis. ' \
          'Use standalone mode and Memory adapter instead.')
      end
    else
      @config.logger.error('Consumer mode cannot be used with Memory adapter. ' \
        'Use Redis adapter instead.')
    end
  when :standalone
    if @config.cache_adapter.is_a? SplitIoClient::Cache::Adapters::MemoryAdapter
      valid_startup_mode = true
    else
      @config.logger.error('Standalone mode cannot be used with Redis adapter. ' \
        'Use Memory adapter instead.')
    end
  when :producer
    @config.logger.error('Producer mode is no longer supported. Use Split Synchronizer. ' \
      'See: https://github.com/splitio/split-synchronizer')
  else
    @config.logger.error('Invalid SDK mode selected. ' \
      "Valid modes are 'standalone with memory adapter' and 'consumer with redis adapter'")
  end

  valid_startup_mode
end