diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2021-10-27 16:55:38 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2021-11-16 11:30:35 -0800 |
commit | b7f0ff9966ac7b913557643863fac76eaf7b661b (patch) | |
tree | ccd361102da5680253d2c0c04c97a9963bbf7202 | |
parent | 195b541f9b0a3e5ead8881a4177ddc1d4bb4af43 (diff) | |
download | chef-b7f0ff9966ac7b913557643863fac76eaf7b661b.tar.gz |
Refactor intervals to expose remaining time
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/compliance/runner.rb | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/lib/chef/compliance/runner.rb b/lib/chef/compliance/runner.rb index 35dd11603e..88882d3d63 100644 --- a/lib/chef/compliance/runner.rb +++ b/lib/chef/compliance/runner.rb @@ -106,12 +106,11 @@ class Chef end def report_with_interval - interval = node["audit"]["interval"] - if check_interval_settings(interval) - create_timestamp_file if interval["enabled"] + if interval_seconds_left <= 0 + create_timestamp_file if interval_enabled report else - logger.info "Skipping Chef Infra Compliance Phase due to interval settings" + logger.info "Skipping Chef Infra Compliance Phase due to interval settings (next run in #{interval_seconds_left} secs)" end end @@ -377,29 +376,33 @@ class Chef end def report_timing_file - # Will create and return the complete folder path for the chef cache location and the passed in value ::File.join(Chef::FileCache.create_cache_path("compliance"), "report_timing.json") end - def profile_overdue_to_run?(interval_seconds) - # Calculate when a report was last created so we delay the next report if necessary - return true unless ::File.exist?(report_timing_file) + def interval_time + @interval_time ||= node.read("audit", "interval", "time") + end - seconds_since_last_run = Time.now - ::File.mtime(report_timing_file) - seconds_since_last_run > interval_seconds + def interval_enabled + @interval_enabled ||= node.read("audit", "interval", "enabled") end - def check_interval_settings(interval) - interval_enabled = interval["enabled"] - interval_time = interval["time"] - # handle intervals - interval_seconds = 0 # always run this by default, unless interval is defined - if !interval.nil? && interval_enabled - interval_seconds = interval_time * 60 # seconds in interval - Chef::Log.debug "Running Chef Infra Compliance Phase every #{interval_seconds} seconds" - end - # returns true if profile is overdue to run - profile_overdue_to_run?(interval_seconds) + def interval_seconds + @interval_seconds ||= + if interval_enabled + Chef::Log.debug "Running Chef Infra Compliance Phase every #{interval_time * 60} seconds" + interval_time * 60 + else + Chef::Log.debug "Running Chef Infra Compliance Phase on every run" + 0 + end + end + + def interval_seconds_left + return 0 unless ::File.exist?(report_timing_file) + + seconds_since_last_run = Time.now - ::File.mtime(report_timing_file) + interval_seconds - seconds_since_last_run end end end |