lib/sfpagent/agent.rb in sfpagent-0.2.4 vs lib/sfpagent/agent.rb in sfpagent-0.2.5
- old
+ new
@@ -166,12 +166,15 @@
File.delete(PIDFile) if File.exist?(PIDFile)
end
end
def self.get_cache_model(name)
- model = JSON[File.read(CacheModelFile)]
- (model.has_key?(name) ? model[name] : nil)
+ if File.exist?(CacheModelFile)
+ model = JSON[File.read(CacheModelFile)]
+ return model[name] if model.has_key?(name)
+ end
+ nil
end
def self.set_cache_model(p={})
File.open(CacheModelFile, File::RDWR|File::CREAT, 0600) do |f|
f.flock(File::LOCK_EX)
@@ -179,11 +182,11 @@
model = (json.length >= 2 ? JSON[json] : {})
if p[:name]
if p[:model]
model[p[:name]] = p[:model]
- Sfp::Agent.logger.info "Setting cache model for #{p[:name]}..."
+ Sfp::Agent.logger.info "Saving cache model for #{p[:name]}..."
else
model.delete(p[:name]) if model.has_key?(p[:name])
Sfp::Agent.logger.info "Deleting cache model for #{p[:name]}..."
end
else
@@ -308,9 +311,28 @@
rescue Exception => e
Sfp::Agent.logger.error "Get state [Failed] #{e}\n#{e.backtrace.join("\n")}"
end
}
false
+ end
+
+ def self.resolve_model(path)
+ return Sfp::Undefined.new if !defined?(@@runtime) or @@runtime.nil? or @@runtime.root.nil?
+ begin
+ path = path.simplify
+ value = @@runtime.model.at?(path)
+ if value.is_a?(Sfp::Unknown)
+ _, name, rest = path.split('.', 3)
+ model = get_cache_model(name)
+ if !model.nil? and model.has_key?('model')
+ return (rest.to_s.length <= 0 ? model['model'] : model['model'].at?("$.#{rest}"))
+ end
+ end
+ return value
+ rescue Exception => e
+ Sfp::Agent.logger.error "Resolve model #{path} [Failed] #{e}\n#{e.backtrace.join("\n")}"
+ end
+ Sfp::Undefined.new
end
def self.resolve(path, as_sfp=true)
return Sfp::Undefined.new if !defined?(@@runtime) or @@runtime.nil? or @@runtime.root.nil?
begin