From 0cb3bd3128a70bba46dd7cc6d0df8b0a8269929d Mon Sep 17 00:00:00 2001 From: Johannes Haass Date: Thu, 19 Dec 2024 14:57:30 +0100 Subject: [PATCH] Explicitly configure worker sleep delay The default worker sleep delay in delayed_job is 5 seconds. Reducing this value could reduce the database load when there aren't many jobs to process. --- config/cloud_controller.yml | 1 + lib/cloud_controller/config_schemas/base/api_schema.rb | 5 ++++- lib/cloud_controller/config_schemas/base/worker_schema.rb | 5 ++++- lib/delayed_job/delayed_worker.rb | 1 + spec/unit/lib/delayed_job/delayed_worker_spec.rb | 1 + 5 files changed, 11 insertions(+), 2 deletions(-) diff --git a/config/cloud_controller.yml b/config/cloud_controller.yml index 9c1e0a819fc..51b471df184 100644 --- a/config/cloud_controller.yml +++ b/config/cloud_controller.yml @@ -33,6 +33,7 @@ system_hostnames: jobs: global: timeout_in_seconds: 14400 + worker_sleep_delay_in_seconds: 5 queues: {} app_usage_events: diff --git a/lib/cloud_controller/config_schemas/base/api_schema.rb b/lib/cloud_controller/config_schemas/base/api_schema.rb index 6bf5e263d89..37584dfda7c 100644 --- a/lib/cloud_controller/config_schemas/base/api_schema.rb +++ b/lib/cloud_controller/config_schemas/base/api_schema.rb @@ -335,7 +335,10 @@ class ApiSchema < VCAP::Config default_app_ssh_access: bool, jobs: { - global: { timeout_in_seconds: Integer }, + global: { + timeout_in_seconds: Integer, + worker_sleep_delay_in_seconds: Integer + }, queues: { optional(:cc_generic) => { timeout_in_seconds: Integer } }, diff --git a/lib/cloud_controller/config_schemas/base/worker_schema.rb b/lib/cloud_controller/config_schemas/base/worker_schema.rb index 0d19b654c03..466c40b16a3 100644 --- a/lib/cloud_controller/config_schemas/base/worker_schema.rb +++ b/lib/cloud_controller/config_schemas/base/worker_schema.rb @@ -161,7 +161,10 @@ class WorkerSchema < VCAP::Config default_app_ssh_access: bool, jobs: { - global: { timeout_in_seconds: Integer }, + global: { + timeout_in_seconds: Integer, + worker_sleep_delay_in_seconds: Integer + }, queues: { optional(:cc_generic) => { timeout_in_seconds: Integer } }, diff --git a/lib/delayed_job/delayed_worker.rb b/lib/delayed_job/delayed_worker.rb index f30c6eacbf3..9b7519d86a6 100644 --- a/lib/delayed_job/delayed_worker.rb +++ b/lib/delayed_job/delayed_worker.rb @@ -48,6 +48,7 @@ def get_initialized_delayed_worker(config, logger) Delayed::Worker.destroy_failed_jobs = false Delayed::Worker.max_attempts = 3 Delayed::Worker.max_run_time = config.get(:jobs, :global, :timeout_in_seconds) + 1 + Delayed::Worker.sleep_delay = config.get(:jobs, :global, :worker_sleep_delay_in_seconds) Delayed::Worker.logger = logger unless @queue_options[:num_threads].nil? diff --git a/spec/unit/lib/delayed_job/delayed_worker_spec.rb b/spec/unit/lib/delayed_job/delayed_worker_spec.rb index adfd81aee6c..091e32f97d8 100644 --- a/spec/unit/lib/delayed_job/delayed_worker_spec.rb +++ b/spec/unit/lib/delayed_job/delayed_worker_spec.rb @@ -83,6 +83,7 @@ expect(Delayed::Worker.destroy_failed_jobs).to be false expect(Delayed::Worker.max_attempts).to eq(3) expect(Delayed::Worker.max_run_time).to eq(14_401) + expect(Delayed::Worker.sleep_delay).to eq(5) end context 'when the number of threads is specified' do