Skip to content

Add option to skip recurring jobs via environment variable #580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/solid_queue/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/solid_queue/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions test/test_helpers/configuration_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down