Skip to content

Commit 6b7efea

Browse files
authored
Merge pull request sidekiq-cron#246 from spk/empty-file
Check its an hash on load from hash to avoid errors
2 parents 578cf69 + 285ebff commit 6b7efea

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/sidekiq/cron/job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def active_job_message
150150
# }
151151
#
152152
def self.load_from_hash hash
153+
return [] unless hash.is_a?(Hash)
153154
array = hash.inject([]) do |out,(key, job)|
154155
job['name'] = key
155156
out << job
@@ -160,6 +161,7 @@ def self.load_from_hash hash
160161
# like to {#load_from_hash}
161162
# If exists old jobs in redis but removed from args, destroy old jobs
162163
def self.load_from_hash! hash
164+
return [] unless hash.is_a?(Hash)
163165
destroy_removed_jobs(hash.keys)
164166
load_from_hash(hash)
165167
end

test/unit/job_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,20 @@
955955
assert_equal out.size, 0, "should have no errors"
956956
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
957957
end
958+
959+
describe 'errors' do
960+
describe 'not a hash' do
961+
before do
962+
@jobs_hash = YAML.load_file('/dev/null') # => false
963+
end
964+
965+
it 'do not create jobs and do not fail' do
966+
assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
967+
out = Sidekiq::Cron::Job.load_from_hash! @jobs_hash
968+
assert_equal out.size, 0, "should have no errors"
969+
end
970+
end
971+
end
958972
end
959973

960974
describe "from array" do

0 commit comments

Comments
 (0)