diff options
author | Tim Smith <tsmith@chef.io> | 2020-08-26 15:02:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-26 15:02:06 -0700 |
commit | d3f88ab5ebdc7d053f9710334bb8245507452262 (patch) | |
tree | 96228bd25c0184a30ec12d107669201a987336b3 | |
parent | 42b88ccc0d01d6f4d1a1a5a0c34dbcdbc58e512c (diff) | |
parent | b045f01d7e7ce619bb58025f067b9b1762ba8d9c (diff) | |
download | chef-d3f88ab5ebdc7d053f9710334bb8245507452262.tar.gz |
Merge pull request #10358 from chef/add_back_nice
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/chef/resource/chef_client_cron.rb | 7 | ||||
-rw-r--r-- | spec/unit/resource/chef_client_cron_spec.rb | 21 |
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..7e216a75a3 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 an 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 |