Class: Unleash::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Client

rubocop:disable Metrics/AbcSize



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
# File 'lib/unleash/client.rb', line 20

def initialize(*opts)
  Unleash.configuration = Unleash::Configuration.new(*opts) unless opts.empty?
  Unleash.configuration.validate!

  Unleash.logger = Unleash.configuration.logger.clone
  Unleash.logger.level = Unleash.configuration.log_level
  Unleash.engine = YggdrasilEngine.new
  Unleash.engine.register_custom_strategies(Unleash.configuration.strategies.custom_strategies)

  @impact_metrics = ImpactMetrics.new(Unleash.engine, Unleash.configuration.app_name)

  Unleash.toggle_fetcher = Unleash::ToggleFetcher.new Unleash.engine unless Unleash.configuration.streaming_mode?

  if Unleash.configuration.disable_client
    Unleash.logger.warn "Unleash::Client is disabled! Will only return default (or bootstrapped if available) results!"
    Unleash.logger.warn "Unleash::Client is disabled! Metrics and MetricsReporter are also disabled!"
    Unleash.configuration.disable_metrics = true
    return
  end

  register

  initialize_client_mode

  start_metrics unless Unleash.configuration.disable_metrics
end

Instance Attribute Details

#fetcher_scheduled_executorObject

Returns the value of attribute fetcher_scheduled_executor.



16
17
18
# File 'lib/unleash/client.rb', line 16

def fetcher_scheduled_executor
  @fetcher_scheduled_executor
end

#impact_metricsObject (readonly)

Returns the value of attribute impact_metrics.



17
18
19
# File 'lib/unleash/client.rb', line 17

def impact_metrics
  @impact_metrics
end

#metrics_scheduled_executorObject

Returns the value of attribute metrics_scheduled_executor.



16
17
18
# File 'lib/unleash/client.rb', line 16

def metrics_scheduled_executor
  @metrics_scheduled_executor
end

Instance Method Details

#get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/unleash/client.rb', line 88

def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant)
  variant = Unleash.engine.get_variant(feature, context)

  if variant.nil?
    Unleash.logger.debug "Unleash::Client.get_variant variants for feature: #{feature} not found"
    Unleash.engine.count_toggle(feature, false)
    return fallback_variant
  end

  variant = Variant.new(variant)

  Unleash.engine.count_variant(feature, variant.name)
  Unleash.engine.count_toggle(feature, variant.feature_enabled)

  # TODO: Add to README: name, payload, enabled (bool)

  variant
end

#if_disabled(feature, context = nil, default_value = true) {|blk| ... } ⇒ Object

execute a code block (passed as a parameter), if is_disabled? is true.

Yields:

  • (blk)


84
85
86
# File 'lib/unleash/client.rb', line 84

def if_disabled(feature, context = nil, default_value = true, &blk)
  yield(blk) if is_disabled?(feature, context, default_value)
end

#if_enabled(feature, context = nil, default_value = false) {|blk| ... } ⇒ Object

execute a code block (passed as a parameter), if is_enabled? is true.

Yields:

  • (blk)


79
80
81
# File 'lib/unleash/client.rb', line 79

def if_enabled(feature, context = nil, default_value = false, &blk)
  yield(blk) if is_enabled?(feature, context, default_value)
end

#is_disabled?(feature, context = nil, default_value_param = true, &fallback_blk) ⇒ Boolean Also known as: disabled?

Returns:

  • (Boolean)


69
70
71
# File 'lib/unleash/client.rb', line 69

def is_disabled?(feature, context = nil, default_value_param = true, &fallback_blk)
  !is_enabled?(feature, context, !default_value_param, &fallback_blk)
end

#is_enabled?(feature, context = nil, default_value_param = false, &fallback_blk) ⇒ Boolean Also known as: enabled?

rubocop:enable Metrics/AbcSize

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/unleash/client.rb', line 48

def is_enabled?(feature, context = nil, default_value_param = false, &fallback_blk)
  Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"

  default_value = if block_given?
                    default_value_param || !!fallback_blk.call(feature, context)
                  else
                    default_value_param
                  end

  toggle_enabled = Unleash.engine.enabled?(feature, context)
  if toggle_enabled.nil?
    Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
    Unleash.engine.count_toggle(feature, false)
    return default_value
  end

  Unleash.engine.count_toggle(feature, toggle_enabled)

  toggle_enabled
end

#shutdownObject

safe shutdown: also flush metrics to server and toggles to disk



108
109
110
111
112
113
# File 'lib/unleash/client.rb', line 108

def shutdown
  unless Unleash.configuration.disable_client
    Unleash.reporter.post unless Unleash.configuration.disable_metrics
    shutdown!
  end
end

#shutdown!Object

quick shutdown: just kill running threads



116
117
118
119
120
121
# File 'lib/unleash/client.rb', line 116

def shutdown!
  unless Unleash.configuration.disable_client
    self.fetcher_scheduled_executor&.exit
    self.metrics_scheduled_executor.exit unless Unleash.configuration.disable_metrics
  end
end