lib/model/endpoint.rb in puppetdb_rundeck-0.3.1 vs lib/model/endpoint.rb in puppetdb_rundeck-1.0.0
- old
+ new
@@ -1,73 +1,92 @@
require File.expand_path('../../helpers/process', __FILE__)
+require 'thread'
+require 'thwait'
class EndPoint
attr_accessor :cache_timeout
- THREAD_COUNT = 40
+ attr_accessor :thread_count
- def initialize(puppetdb_helper)
+ def initialize(puppetdb_helper, cache_timeout, thread_count)
@db_helper = puppetdb_helper
- @cache_timeout = 1800
+ @cache_timeout = cache_timeout
+ @thread_count = thread_count
end
def reload(type)
- nodes = @db_helper.get_nodes
+ p "reloading data"
+ if @nodes.nil? or @nodes.empty?
+ @nodes = @db_helper.get_nodes
+ end
@rundeck_data = Hash.new
helper = Helpers::Process.new
mutex = Mutex.new
per_type_cache = "/tmp/puppetdb-resource.#{type}"
- THREAD_COUNT.times.map {
- Thread.new(nodes) do |nodes|
- while node = mutex.synchronize { nodes.pop }
+ data_elements = []
+ process_threads = []
+
+ @thread_count.times.map {
+ t = Thread.new(@nodes) do |nodes|
+ while node = mutex.synchronize { @nodes.pop }
host = node['name']
facts = @db_helper.get_facts(host)
if !facts.nil?
- mutex.synchronize { helper.add_facts(facts, host, @rundeck_data) }
+ mutex.synchronize do
+ data_elements.push(helper.add_facts(facts, host))
+ end
end
end
end
- }.each(&:join)
+ process_threads.push(t)
+ }
+ ThreadsWait.all_waits(*process_threads)
+
+ data_elements.each do |item|
+ #sleep(Random.new.rand(1..10))
+ @rundeck_data.merge!(item) if @rundeck_data.is_a?(Hash)
+ end
+
data = case type
- when 'json' then to_json(false)
- when 'yaml' then to_yaml(false)
+ when 'json' then self.to_json(false)
+ when 'yaml' then self.to_yaml(false)
when 'xml' then to_xml(false)
- else "unknown"
+ else 'unknown'
end
File.open(per_type_cache, 'w') { |file| file.write(data) }
return data
end
- def to_json(parse_data=true)
+ def to_json(parse_data)
+ p "parse_data is: #{parse_data}"
parse('json') if parse_data == true
if @rundeck_data.is_a?(String)
@rundeck_data
else
@rundeck_data.to_json
end
end
- def to_yaml(parse_data=true)
+ def to_yaml(parse_data)
+ p "parse_data is: #{parse_data}"
parse('yaml') if parse_data == true
if @rundeck_data.is_a?(String)
@rundeck_data
else
@rundeck_data.to_yaml
end
end
def to_xml(parse_data=true)
- helper = Helpers::Process.new
-
parse('xml') if parse_data == true
if @rundeck_data.is_a?(String)
@rundeck_data
else
data = Array.new
@@ -84,11 +103,10 @@
@rundeck_data = xml
end
end
-
def parse(type)
per_type_cache = "/tmp/puppetdb-resource.#{type}"
if File.exist?(per_type_cache)
file = File.new(per_type_cache)
@@ -107,10 +125,10 @@
reload(type)
end
end
def clear_cache
- cache_files = ['/tmp/puppetdb-resource.xml','/tmp/puppetdb-resource.json','/tmp/puppetdb-resource.yaml']
+ cache_files = ['/tmp/puppetdb-resource.json','/tmp/puppetdb-resource.yaml']
cache_files.each do |file|
if File.exist?(file)
FileUtils.rm file
end