From bbedb788a7951e2f69c1c2815a5c3c669ff02ae6 Mon Sep 17 00:00:00 2001 From: mh Date: Tue, 7 Jun 2016 21:59:33 +0200 Subject: keep trocla object around - addresses #18 Per puppet function call we now created a new Trocla object. This is a) a very naive approach and b) obviously can lead to a lot of inefficiency as we for example need to build up trocla each time again. Also this means that we are running into problems like opening a connection to a database system each time a trocla lookup is done (and we never close the connection :-/). The proper way to solve this is to make sure we don't create too many trocla objects. With this change, we should now create once a global trocla object PER puppet(-master/-server) process and keep it around for the life time of such a process. --- lib/puppet/util/trocla_helper.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/puppet/util/trocla_helper.rb b/lib/puppet/util/trocla_helper.rb index 94670ae..ce583f5 100644 --- a/lib/puppet/util/trocla_helper.rb +++ b/lib/puppet/util/trocla_helper.rb @@ -22,13 +22,22 @@ module Puppet::Util::TroclaHelper options = YAML.load(options) end - configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") + has_options ? store.send(trocla_func, key, format, options) : store.send(trocla_func, key, format) + end + module_function :trocla + + private - raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile) + def store + @store ||= begin + require 'trocla' + configfile = File.join(File.dirname(Puppet.settings[:config]), "troclarc.yaml") - require 'trocla' + raise(Puppet::ParseError, "Trocla config file #{configfile} is not readable") unless File.exist?(configfile) - has_options ? Trocla.new(configfile).send(trocla_func, key, format, options) : Trocla.new(configfile).send(trocla_func, key, format) + Trocla.new(configfile) + end end - module_function :trocla + module_function :store + end -- cgit v1.2.3