diff --git a/lib/travis/api/v3/queries/crons.rb b/lib/travis/api/v3/queries/crons.rb index c6ede5e8..f848b6e3 100644 --- a/lib/travis/api/v3/queries/crons.rb +++ b/lib/travis/api/v3/queries/crons.rb @@ -8,16 +8,17 @@ module Travis::API::V3 def start_all() puts "reviewing #{Models::Cron.count} crons." Models::Cron.all.select do |cron| - @cron = cron - ne = cron.next_enqueuing - puts "Next enqueuing: #{ne}, time now: #{Time.now}, will it run? #{ne <= Time.now}" - start(cron) if ne <= Time.now + begin + @cron = cron + ne = cron.next_enqueuing + puts "Next enqueuing: #{ne}, time now: #{Time.now}, will it run? #{ne <= Time.now}" + start(cron) if ne <= Time.now + rescue => e + Raven.capture_exception(e, tags: { 'cron_id' => @cron.try(:id) }) + sleep(10) + next + end end - rescue => e - puts "bad things happened" - puts e.inspect - puts Raven.capture_exception(e, tags: { 'cron_id' => @cron.try(:id) }) - sleep(10) end def start(cron) diff --git a/spec/v3/queries/cron_spec.rb b/spec/v3/queries/cron_spec.rb index 7013495b..1418a6e0 100644 --- a/spec/v3/queries/cron_spec.rb +++ b/spec/v3/queries/cron_spec.rb @@ -4,6 +4,7 @@ describe Travis::API::V3::Queries::Crons do let(:user) { Travis::API::V3::Models::User.find_by_login('svenfuchs') } let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first } let(:existing_branch) { Travis::API::V3::Models::Branch.create(repository: repo, name: 'cron-test-existing', exists_on_github: true) } + let(:existing_branch2) { Travis::API::V3::Models::Branch.create(repository: repo, name: 'cron-test-existing2', exists_on_github: true) } let(:non_existing_branch) { Travis::API::V3::Models::Branch.create(repository: repo, name: 'cron-test-non-existing', exists_on_github: false) } let(:query) { Travis::API::V3::Queries::Crons.new({}, 'Overview') } @@ -25,9 +26,23 @@ describe Travis::API::V3::Queries::Crons do it 'enques error into a thread' do cron = Travis::API::V3::Models::Cron.create(branch_id: existing_branch.id, interval: 'daily', disable_by_build: false) error = StandardError.new('Konstantin broke all the thingz!') + Travis::API::V3::Queries::Crons.any_instance.expects(:sleep).with(10) Travis::API::V3::Models::Cron.any_instance.stubs(:branch).raises(error) Raven.expects(:capture_exception).with(error, tags: {'cron_id' => cron.id }) query.start_all end + + it 'continues running crons if one breaks' do + cron = Travis::API::V3::Models::Cron.create(branch_id: existing_branch.id, interval: 'daily', disable_by_build: false) + cron2 = Travis::API::V3::Models::Cron.create(branch_id: existing_branch2.id, interval: 'daily', disable_by_build: false) + + error = StandardError.new('Konstantin broke all the thingz!') + Travis::API::V3::Models::Cron.any_instance.stubs(:branch).raises(error) + + Travis::API::V3::Queries::Crons.any_instance.expects(:sleep).twice.with(10) + Raven.expects(:capture_exception).with(error, tags: {'cron_id' => cron.id }) + Raven.expects(:capture_exception).with(error, tags: {'cron_id' => cron2.id }) + query.start_all + end end end