From 25ac49363e77520add4a79ba1f916d0bb94bf629 Mon Sep 17 00:00:00 2001 From: Varun Sharma Date: Tue, 17 Jun 2025 19:16:38 +0530 Subject: [PATCH] Add option to skip recurring jobs via environment variable --- README.md | 4 ++++ lib/solid_queue/cli.rb | 3 ++- lib/solid_queue/configuration.rb | 2 +- .../test_helpers/configuration_test_helper.rb | 14 +++++++++++ test/unit/configuration_test.rb | 24 +++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c7a2ef2c..9c3c44c7 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,8 @@ By default, Solid Queue will try to find your configuration under `config/queue. bin/jobs -c config/calendar.yml ``` +You can also skip all recurring tasks by setting the environment variable `SOLID_QUEUE_SKIP_RECURRING=true`. This is useful for environments like staging, review apps, or development where you don't want any recurring jobs to run. This is equivalent to using the `--skip-recurring` option with `bin/jobs`. + This is what this configuration looks like: ```yml @@ -571,6 +573,8 @@ Solid Queue supports defining recurring tasks that run at specific times in the bin/jobs --recurring_schedule_file=config/schedule.yml ``` +You can completely disable recurring tasks by setting the environment variable `SOLID_QUEUE_SKIP_RECURRING=true` or by using the `--skip-recurring` option with `bin/jobs`. + The configuration itself looks like this: ```yml diff --git a/lib/solid_queue/cli.rb b/lib/solid_queue/cli.rb index 33d2588b..404c7e72 100644 --- a/lib/solid_queue/cli.rb +++ b/lib/solid_queue/cli.rb @@ -13,7 +13,8 @@ class Cli < Thor banner: "SOLID_QUEUE_RECURRING_SCHEDULE" class_option :skip_recurring, type: :boolean, default: false, - desc: "Whether to skip recurring tasks scheduling" + desc: "Whether to skip recurring tasks scheduling", + banner: "SOLID_QUEUE_SKIP_RECURRING" def self.exit_on_failure? true diff --git a/lib/solid_queue/configuration.rb b/lib/solid_queue/configuration.rb index ba13f0f4..a002b41d 100644 --- a/lib/solid_queue/configuration.rb +++ b/lib/solid_queue/configuration.rb @@ -88,7 +88,7 @@ def default_options recurring_schedule_file: Rails.root.join(ENV["SOLID_QUEUE_RECURRING_SCHEDULE"] || DEFAULT_RECURRING_SCHEDULE_FILE_PATH), only_work: false, only_dispatch: false, - skip_recurring: false + skip_recurring: ActiveModel::Type::Boolean.new.cast(ENV["SOLID_QUEUE_SKIP_RECURRING"]) } end diff --git a/test/test_helpers/configuration_test_helper.rb b/test/test_helpers/configuration_test_helper.rb index 24b95e6b..fa3a402a 100644 --- a/test/test_helpers/configuration_test_helper.rb +++ b/test/test_helpers/configuration_test_helper.rb @@ -4,4 +4,18 @@ module ConfigurationTestHelper def config_file_path(name) Rails.root.join("config/#{name}.yml") end + + def with_env(env_vars) + original_values = {} + env_vars.each do |key, value| + original_values[key] = ENV[key] + ENV[key] = value + end + + yield + ensure + original_values.each do |key, value| + ENV[key] = value + end + end end diff --git a/test/unit/configuration_test.rb b/test/unit/configuration_test.rb index 68a693e3..2ccaa728 100644 --- a/test/unit/configuration_test.rb +++ b/test/unit/configuration_test.rb @@ -85,6 +85,30 @@ class ConfigurationTest < ActiveSupport::TestCase assert_processes configuration, :dispatcher, 1, polling_interval: 0.1, recurring_tasks: nil end + test "skip recurring tasks when SOLID_QUEUE_SKIP_RECURRING environment variable is set" do + with_env("SOLID_QUEUE_SKIP_RECURRING" => "true") do + configuration = SolidQueue::Configuration.new(dispatchers: [ { polling_interval: 0.1 } ]) + assert_processes configuration, :dispatcher, 1, polling_interval: 0.1 + assert_processes configuration, :scheduler, 0 + end + end + + test "include recurring tasks when SOLID_QUEUE_SKIP_RECURRING environment variable is false" do + with_env("SOLID_QUEUE_SKIP_RECURRING" => "false") do + configuration = SolidQueue::Configuration.new(dispatchers: [ { polling_interval: 0.1 } ]) + assert_processes configuration, :dispatcher, 1, polling_interval: 0.1 + assert_processes configuration, :scheduler, 1 + end + end + + test "include recurring tasks when SOLID_QUEUE_SKIP_RECURRING environment variable is not set" do + with_env("SOLID_QUEUE_SKIP_RECURRING" => nil) do + configuration = SolidQueue::Configuration.new(dispatchers: [ { polling_interval: 0.1 } ]) + assert_processes configuration, :dispatcher, 1, polling_interval: 0.1 + assert_processes configuration, :scheduler, 1 + end + end + test "validate configuration" do # Valid and invalid recurring tasks configuration = SolidQueue::Configuration.new(recurring_schedule_file: config_file_path(:recurring_with_invalid))