summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-08-26 14:29:08 -0700
committerTim Smith <tsmith84@gmail.com>2020-08-26 14:29:08 -0700
commit4a5162a70ade915f2b5c992f7e3419c221717b60 (patch)
treee2e3dd555fb28f4a64b1a1a517b2d54984bff85d
parentff8a59dab5091968171c69297c315a6943ea28ed (diff)
downloadchef-4a5162a70ade915f2b5c992f7e3419c221717b60.tar.gz
Add back nice functionality to chef_client_cron
This was in the cookbook recipe before and was useful for low resource systems where the client could impact host operations. Since we support this in launchd now, it makes sense to do it here as well. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/chef_client_cron.rb7
-rw-r--r--spec/unit/resource/chef_client_cron_spec.rb21
2 files changed, 28 insertions, 0 deletions
diff --git a/lib/chef/resource/chef_client_cron.rb b/lib/chef/resource/chef_client_cron.rb
index f78bc848c7..80b0a8e00c 100644
--- a/lib/chef/resource/chef_client_cron.rb
+++ b/lib/chef/resource/chef_client_cron.rb
@@ -138,6 +138,12 @@ class Chef
default: lazy { {} },
description: "A Hash containing additional arbitrary environment variables under which the cron job will be run in the form of `({'ENV_VARIABLE' => 'VALUE'})`."
+ property :nice, [Integer, String],
+ description: "The process priority to run the #{Chef::Dist::CLIENT} process at. A value of -20 is the highest priority and 19 is the lowest priority.",
+ introduced: "16.5",
+ coerce: proc { |x| Integer(x) },
+ callbacks: { "should be a Integer between -20 and 19" => proc { |v| v >= -20 && v <= 19 } }
+
action :add do
# TODO: Replace this with a :create_if_missing action on directory when that exists
unless ::Dir.exist?(new_resource.log_directory)
@@ -190,6 +196,7 @@ class Chef
def cron_command
cmd = ""
cmd << "/bin/sleep #{splay_sleep_time(new_resource.splay)}; "
+ cmd << "#{which("nice")} -n #{new_resource.nice} " if new_resource.nice
cmd << "#{new_resource.chef_binary_path} "
cmd << "#{new_resource.daemon_options.join(" ")} " unless new_resource.daemon_options.empty?
cmd << "-c #{::File.join(new_resource.config_directory, "client.rb")} "
diff --git a/spec/unit/resource/chef_client_cron_spec.rb b/spec/unit/resource/chef_client_cron_spec.rb
index b6b28329a9..00d12b82a8 100644
--- a/spec/unit/resource/chef_client_cron_spec.rb
+++ b/spec/unit/resource/chef_client_cron_spec.rb
@@ -38,6 +38,19 @@ describe Chef::Resource::ChefClientCron do
expect { resource.splay("-10") }.to raise_error(Chef::Exceptions::ValidationFailed)
end
+ it "raises an error if nice is less than -20" do
+ expect { resource.nice(-21) }.to raise_error(Chef::Exceptions::ValidationFailed)
+ end
+
+ it "raises an error if nice is greater than 19" do
+ expect { resource.nice(20) }.to raise_error(Chef::Exceptions::ValidationFailed)
+ end
+
+ it "coerces nice to an Integer" do
+ resource.nice "10"
+ expect(resource.nice).to eql(10)
+ end
+
it "builds a default value for chef_binary_path dist values" do
expect(resource.chef_binary_path).to eql("/opt/chef/bin/chef-client")
end
@@ -131,5 +144,13 @@ describe Chef::Resource::ChefClientCron do
"/bin/sleep 123; /opt/chef/bin/chef-client -c #{root_path} --chef-license accept -L /var/log/chef/client.log"
)
end
+
+ it "uses nice if set" do
+ allow(provider).to receive(:which).with("nice").and_return("/usr/bin/nice")
+ resource.nice(-15)
+ expect(provider.cron_command).to eql(
+ "/bin/sleep 123; /usr/bin/nice -n -15 /opt/chef/bin/chef-client -c /etc/chef/client.rb -L /var/log/chef/client.log"
+ )
+ end
end
end