Skip to content

Commit

Permalink
Handle multiple key types
Browse files Browse the repository at this point in the history
When config is supplied as a hash, values can wrongly come through as symbols. We need to handle it all as strings for consistency.
  • Loading branch information
hlascelles committed Jan 28, 2021
1 parent 2ba77e7 commit 6f60dc9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

- Handle multiple key types [#237](https://github.com/hlascelles/que-scheduler/pull/237)
- Remove Postgres 9.4 support [#238](https://github.com/hlascelles/que-scheduler/pull/238)
- Upgrade Rubocop to 1.x and remove Ruby 2.4 support [#239](https://github.com/hlascelles/que-scheduler/pull/239)

Expand Down
5 changes: 3 additions & 2 deletions lib/que/scheduler/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def from_yaml(config)

def from_hash(config_hash)
config_hash.map do |name, defined_job_hash|
[name, hash_item_to_defined_job(name, defined_job_hash)]
name_str = name.to_s
[name_str, hash_item_to_defined_job(name_str, defined_job_hash)]
end.to_h
end

Expand Down Expand Up @@ -65,7 +66,7 @@ def hash_item_to_defined_job(name, defined_job_hash_in)

Que::Scheduler::DefinedJob.create(
name: name,
job_class: defined_job_hash["class"] || name,
job_class: defined_job_hash["class"]&.to_s || name,
queue: defined_job_hash["queue"],
args_array: args_array,
priority: defined_job_hash["priority"],
Expand Down
31 changes: 31 additions & 0 deletions spec/que/scheduler/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@
expect(job_config[:args_array]).to eq(["First", 1234, { "some_hash" => true }])
end

it "loads the schedule with all key types" do
Que::Scheduler.configure do |config|
config.schedule = {
# Symbol
AsSymbolSpecifiedByClassTestJob: {
class: :SpecifiedByClassTestJob,
cron: "01 * * * *",
},
# String
"AsStringSpecifiedByClassTestJob" => {
class: "SpecifiedByClassTestJob",
cron: "02 * * * *",
},
# Class
SpecifiedByClassTestJob => {
cron: "03 * * * *",
},
}
end
expect(Que::Scheduler.schedule.keys).to eq(
%w[
AsSymbolSpecifiedByClassTestJob
AsStringSpecifiedByClassTestJob
SpecifiedByClassTestJob
]
)
expect(Que::Scheduler.schedule.values.map(&:job_class)).to eq(
[SpecifiedByClassTestJob, SpecifiedByClassTestJob, SpecifiedByClassTestJob]
)
end

it "errors if the schedule hash is not set and the file is not present" do
Que::Scheduler.configure do |config|
config.schedule_location = "spec/config/not_here.yml"
Expand Down

0 comments on commit 6f60dc9

Please sign in to comment.