|
2 | 2 |
|
3 | 3 | module PuppetLanguageServer |
4 | 4 | module FacterHelper |
5 | | - @ops_lock = Mutex.new |
6 | 5 | @facts_loaded = nil |
7 | 6 |
|
8 | | - def self.reset |
9 | | - @ops_lock.synchronize do |
10 | | - _reset |
11 | | - end |
| 7 | + def self.cache |
| 8 | + PuppetLanguageServer::PuppetHelper.cache |
12 | 9 | end |
13 | 10 |
|
14 | | - def self.load_facts_async |
15 | | - Thread.new do |
16 | | - load_facts |
17 | | - end |
| 11 | + def self.sidecar_queue |
| 12 | + PuppetLanguageServer::PuppetHelper.sidecar_queue |
18 | 13 | end |
19 | 14 |
|
| 15 | + # Facts |
20 | 16 | def self.facts_loaded? |
21 | 17 | @facts_loaded.nil? ? false : @facts_loaded |
22 | 18 | end |
23 | 19 |
|
24 | | - def self.load_facts |
25 | | - @ops_lock.synchronize do |
26 | | - _load_facts |
27 | | - end |
28 | | - end |
29 | | - |
30 | | - def self.facts |
31 | | - return {} if @facts_loaded == false |
32 | | - @ops_lock.synchronize do |
33 | | - _load_facts if @fact_hash.nil? |
34 | | - @fact_hash.clone |
35 | | - end |
36 | | - end |
37 | | - |
38 | | - # DO NOT ops_lock on any of these methods |
39 | | - # deadlocks will ensue! |
40 | | - def self._reset |
41 | | - @facts_loaded = nil |
42 | | - Facter.reset |
43 | | - @fact_hash = nil |
44 | | - end |
45 | | - private_class_method :_reset |
46 | | - |
47 | | - def self._load_facts |
48 | | - _reset |
49 | | - @fact_hash = {} |
50 | | - begin |
51 | | - Facter.loadfacts |
52 | | - @fact_hash = Facter.to_hash |
53 | | - rescue StandardError => e |
54 | | - PuppetLanguageServer.log_message(:error, "[FacterHelper::_load_facts] Error loading facts #{e.message} #{e.backtrace}") |
55 | | - rescue LoadError => e |
56 | | - PuppetLanguageServer.log_message(:error, "[FacterHelper::_load_facts] Error loading facts (LoadError) #{e.message} #{e.backtrace}") |
57 | | - end |
58 | | - PuppetLanguageServer.log_message(:debug, "[FacterHelper::_load_facts] Finished loading #{@fact_hash.keys.count} facts") |
| 20 | + def self.assert_facts_loaded |
59 | 21 | @facts_loaded = true |
60 | 22 | end |
61 | | - private_class_method :_load_facts |
| 23 | + |
| 24 | + def self.load_facts |
| 25 | + @facts_loaded = false |
| 26 | + sidecar_queue.execute_sync('facts', []) |
| 27 | + end |
| 28 | + |
| 29 | + def self.load_facts_async |
| 30 | + @facts_loaded = false |
| 31 | + sidecar_queue.enqueue('facts', []) |
| 32 | + end |
| 33 | + |
| 34 | + def self.fact(name) |
| 35 | + return nil if @facts_loaded == false |
| 36 | + cache.object_by_name(:fact, name) |
| 37 | + end |
| 38 | + |
| 39 | + def self.fact_value(name) |
| 40 | + return nil if @facts_loaded == false |
| 41 | + object = cache.object_by_name(:fact, name) |
| 42 | + object.nil? ? nil : object.value |
| 43 | + end |
| 44 | + |
| 45 | + def self.fact_names |
| 46 | + return [] if @facts_loaded == false |
| 47 | + cache.object_names_by_section(:fact).map(&:to_s) |
| 48 | + end |
62 | 49 | end |
63 | 50 | end |
0 commit comments