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

Returns a new instance of Client.



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

def initialize(*opts)
  Unleash.configuration ||= Unleash::Configuration.new(*opts)
  Unleash.configuration.validate!

  Unleash.logger = Unleash.configuration.logger.clone
  Unleash.logger.level = Unleash.configuration.log_level

  unless Unleash.configuration.disable_client
    Unleash.toggle_fetcher = Unleash::ToggleFetcher.new

    register

    self.fetcher_scheduled_executor = Unleash::ScheduledExecutor.new('ToggleFetcher', Unleash.configuration.refresh_interval)
    self.fetcher_scheduled_executor.run do
      Unleash.toggle_fetcher.fetch
    end

    unless Unleash.configuration.disable_metrics
      Unleash.toggle_metrics = Unleash::Metrics.new
      Unleash.reporter = Unleash::MetricsReporter.new
      self.metrics_scheduled_executor = Unleash::ScheduledExecutor.new('MetricsReporter', Unleash.configuration.metrics_interval)
      self.metrics_scheduled_executor.run do
        Unleash.reporter.send
      end
    end
  else
    Unleash.logger.warn "Unleash::Client is disabled! Will only return default results!"
  end
end

Instance Attribute Details

#fetcher_scheduled_executorObject

Returns the value of attribute fetcher_scheduled_executor.



12
13
14
# File 'lib/unleash/client.rb', line 12

def fetcher_scheduled_executor
  @fetcher_scheduled_executor
end

#metrics_scheduled_executorObject

Returns the value of attribute metrics_scheduled_executor.



12
13
14
# File 'lib/unleash/client.rb', line 12

def metrics_scheduled_executor
  @metrics_scheduled_executor
end

Instance Method Details

#get_variant(feature, context = nil, fallback_variant = false) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/unleash/client.rb', line 74

def get_variant(feature, context = nil, fallback_variant = false)
  Unleash.logger.debug "Unleash::Client.get_variant for feature: #{feature} with context #{context}"

  if Unleash.configuration.disable_client
    Unleash.logger.warn "unleash_client is disabled! Always returning #{default_variant} for feature #{feature}!"
    return fallback_variant || Unleash::FeatureToggle.disabled_variant
  end

  toggle_as_hash = Unleash.toggles.select{ |toggle| toggle['name'] == feature }.first if Unleash.toggles

  if toggle_as_hash.nil?
    Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
    return fallback_variant || Unleash::FeatureToggle.disabled_variant
  end

  toggle = Unleash::FeatureToggle.new(toggle_as_hash)
  variant = toggle.get_variant(context, fallback_variant)

  if variant.nil?
    Unleash.logger.debug "Unleash::Client.get_variant variants for feature: #{feature} not found"
    return fallback_variant || Unleash::FeatureToggle.disabled_variant
  end

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

  return variant
end

#if_enabled(feature, context = nil, default_value = false, &blk) ⇒ Object

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



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

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

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

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/unleash/client.rb', line 44

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

    if Unleash.configuration.disable_client
      Unleash.logger.warn "unleash_client is disabled! Always returning #{default_value} for feature #{feature}!"
      return default_value
    end

    toggle_as_hash = Unleash.toggles.select{ |toggle| toggle['name'] == feature }.first if Unleash.toggles

    if toggle_as_hash.nil?
      Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
      return default_value
    end

    toggle = Unleash::FeatureToggle.new(toggle_as_hash)
    toggle_result = toggle.is_enabled?(context, default_value)

    return toggle_result
end

#shutdownObject

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



103
104
105
106
107
108
109
# File 'lib/unleash/client.rb', line 103

def shutdown
  unless Unleash.configuration.disable_client
    Unleash.toggle_fetcher.save!
    Unleash.reporter.send unless Unleash.configuration.disable_metrics
    shutdown!
  end
end

#shutdown!Object

quick shutdown: just kill running threads



112
113
114
115
116
117
# File 'lib/unleash/client.rb', line 112

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