Skip to content

Commit

Permalink
6057 - Yaml fact plugin should check mtime of files
Browse files Browse the repository at this point in the history
Create a new hook for fact plugins to override fact cache
aging.  A fact plugin can define force_reload? which should
return true or false.
  • Loading branch information
ripienaar committed Jan 29, 2011
1 parent 2cd774d commit 7194766
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/mcollective/facts/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def get_fact(fact=nil)

Thread.exclusive do
begin
if (Time.now.to_i - @last_facts_load > cache_time.to_i )
Log.debug("Resetting facter cache after #{cache_time} seconds, now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")
if (Time.now.to_i - @last_facts_load > cache_time.to_i ) || force_reload?
Log.debug("Resetting facter cache, now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")

tfacts = load_facts_from_source

Expand Down Expand Up @@ -76,6 +76,11 @@ def get_facts
def has_fact?(fact)
get_fact(nil).include?(fact)
end

# Plugins can override this to provide forced fact invalidation
def force_reload?
false
end
end
end
end
28 changes: 28 additions & 0 deletions plugins/mcollective/facts/yaml_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ module Facts
# config file, they will be merged with later files overriding
# earlier ones in the list.
class Yaml_facts<Base
def initialize
@yaml_file_mtimes = {}

super
end

def load_facts_from_source
config = Config.instance

Expand All @@ -28,6 +34,28 @@ def load_facts_from_source

facts
end

# force fact reloads when the mtime on the yaml file change
def force_reload?
config = Config.instance

fact_files = config.pluginconf["yaml"].split(":")

fact_files.each do |file|
@yaml_file_mtimes[file] ||= File.stat(file).mtime
mtime = File.stat(file).mtime

if mtime > @yaml_file_mtimes[file]
@yaml_file_mtimes[file] = mtme

Log.debug("Forcing fact reload due to age of #{file}")

return true
end
end

false
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions website/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ title: Changelog

|Date|Description|Ticket|
|----|-----------|------|
|2011/01/30|Let the YAML file force fact reloads when the files update|6057|
|2011/01/30|Add the ability for fact plugins to force fact invalidation|6057|
|2011/01/30|Document an approach to disable type validation in the DDL|6066|
|2011/01/19|Add basic filters to the mc-ping command|5933|
|2011/01/19|Add a ping action to the rpcutil agent|5937|
Expand Down
4 changes: 3 additions & 1 deletion website/reference/plugins/facts.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ end
{% endhighlight %}

If using the newer format in newer releases of mcollective you do not need to worry about caching or
thread safety as the base class does this for you.
thread safety as the base class does this for you. You can force reloading of facts by creating a
*force_reload?* method that should return *true* or *false*. Returning *true* will force the cache
to be rebuilt.

You can see that all you have to do is provide *self.get_facts* which should return a Hash as described above.

Expand Down

0 comments on commit 7194766

Please sign in to comment.