• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Unleash / unleash-client-ruby / 11031869423

25 Sep 2024 11:14AM UTC coverage: 95.57% (-1.7%) from 97.25%
11031869423

Pull #204

github

web-flow
Merge 4cf325450 into d890ae8d3
Pull Request #204: docs: migration guide for v6

73 of 74 new or added lines in 8 files covered. (98.65%)

6 existing lines in 3 files now uncovered.

453 of 474 relevant lines covered (95.57%)

1377.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.22
/lib/unleash/toggle_fetcher.rb
1
require 'unleash/configuration'
16✔
2
require 'unleash/bootstrap/handler'
16✔
3
require 'net/http'
16✔
4
require 'json'
16✔
5
require 'yggdrasil_engine'
16✔
6

7
module Unleash
16✔
8
  class ToggleFetcher
16✔
9
    attr_accessor :toggle_engine, :toggle_lock, :toggle_resource, :etag, :retry_count
16✔
10

11
    def initialize(engine)
16✔
12
      self.toggle_engine = engine
3,824✔
13
      self.etag = nil
3,824✔
14
      self.toggle_lock = Mutex.new
3,824✔
15
      self.toggle_resource = ConditionVariable.new
3,824✔
16
      self.retry_count = 0
3,824✔
17

18
      begin
954✔
19
        # if bootstrap configuration is available, initialize. An immediate API read is also triggered
20
        if Unleash.configuration.use_bootstrap?
3,824✔
21
          bootstrap
3,520✔
22
        else
23
          fetch
304✔
24
        end
25
      rescue StandardError => e
40✔
26
        # fail back to reading the backup file
27
        Unleash.logger.warn "ToggleFetcher was unable to fetch from the network, attempting to read from backup file."
64✔
28
        Unleash.logger.debug "Exception Caught: #{e}"
64✔
29
        read!
64✔
30
      end
31

32
      # once initialized, somewhere else you will want to start a loop with fetch()
33
    end
34

35
    # rename to refresh_from_server!  ??
36
    def fetch
16✔
37
      Unleash.logger.debug "fetch()"
304✔
38
      return if Unleash.configuration.disable_client
304✔
39

40
      response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_uri, etag)
176✔
41

42
      if response.code == '304'
176✔
43
        Unleash.logger.debug "No changes according to the unleash server, nothing to do."
×
44
        return
×
45
      elsif response.code != '200'
172✔
46
        raise IOError, "Unleash server returned a non 200/304 HTTP result."
16✔
47
      end
48

49
      self.etag = response['ETag']
160✔
50

51
      # always synchronize with the local cache when fetching:
52
      update_engine_state!(response.body)
160✔
53

54
      save! response.body
112✔
55
    end
56

57
    def save!(toggle_data)
16✔
58
      Unleash.logger.debug "Will save toggles to disk now"
112✔
59

60
      backup_file = Unleash.configuration.backup_file
112✔
61
      backup_file_tmp = "#{backup_file}.tmp"
112✔
62

63
      self.toggle_lock.synchronize do
112✔
64
        File.open(backup_file_tmp, "w") do |file|
112✔
65
          file.write(toggle_data)
112✔
66
        end
67
        File.rename(backup_file_tmp, backup_file)
112✔
68
      end
69
    rescue StandardError => e
70
      # This is not really the end of the world. Swallowing the exception.
UNCOV
71
      Unleash.logger.error "Unable to save backup file. Exception thrown #{e.class}:'#{e}'"
×
UNCOV
72
      Unleash.logger.error "stacktrace: #{e.backtrace}"
×
73
    end
74

75
    private
16✔
76

77
    def update_engine_state!(toggle_data)
16✔
78
      self.toggle_lock.synchronize do
3,696✔
79
        self.toggle_engine.take_state(toggle_data)
3,696✔
80
      end
81

82
      # notify all threads waiting for this resource to no longer wait
83
      self.toggle_resource.broadcast
3,648✔
84
    end
85

86
    def read!
16✔
87
      Unleash.logger.debug "read!()"
64✔
88
      backup_file = Unleash.configuration.backup_file
64✔
89
      return nil unless File.exist?(backup_file)
64✔
90

91
      backup_data = File.read(backup_file)
16✔
92
      update_engine_state!(backup_data)
16✔
93
    rescue IOError => e
94
      # :nocov:
95
      Unleash.logger.error "Unable to read the backup_file: #{e}"
96
      # :nocov:
97
    rescue JSON::ParserError => e
98
      # :nocov:
99
      Unleash.logger.error "Unable to parse JSON from existing backup_file: #{e}"
100
      # :nocov:
101
    rescue StandardError => e
102
      # :nocov:
103
      Unleash.logger.error "Unable to extract valid data from backup_file. Exception thrown: #{e}"
104
      # :nocov:
105
    end
106

107
    def bootstrap
16✔
108
      bootstrap_payload = Unleash::Bootstrap::Handler.new(Unleash.configuration.bootstrap_config).retrieve_toggles
3,520✔
109
      update_engine_state! bootstrap_payload
3,520✔
110

111
      # reset Unleash.configuration.bootstrap_data to free up memory, as we will never use it again
112
      Unleash.configuration.bootstrap_config = nil
3,520✔
113
    end
114
  end
115
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc