Merge pull request #308 from travis-ci/fix_crons

Fix crons
This commit is contained in:
Renée Hendricksen 2016-08-03 00:41:39 -04:00 committed by GitHub
commit 614b1f5338
5 changed files with 40 additions and 14 deletions

View File

@ -13,7 +13,7 @@ module Travis::API::V3
elsif last_cron_build_date >= planned_time(LastBuild) elsif last_cron_build_date >= planned_time(LastBuild)
planned_time(ThisBuild) planned_time(ThisBuild)
else else
Time.now Time.now - 5.minutes
end end
end end

View File

@ -7,10 +7,15 @@ module Travis::API::V3
def start_all() def start_all()
Models::Cron.all.select do |cron| Models::Cron.all.select do |cron|
begin
@cron = cron
start(cron) if cron.next_enqueuing <= Time.now start(cron) if cron.next_enqueuing <= Time.now
end
rescue => e rescue => e
Raven.capture_exception(e) Raven.capture_exception(e, tags: { 'cron_id' => @cron.try(:id) })
sleep(10) # This ensures the dyno does not spin down before the http request to send the error to sentry completes
next
end
end
end end
def start(cron) def start(cron)

View File

@ -43,11 +43,13 @@ describe Travis::API::V3::Models::Cron do
describe "push build is ignored if disable by build is false" do describe "push build is ignored if disable by build is false" do
before do before do
Timecop.return
Timecop.travel(DateTime.new(2015, 12, 31, 16)) Timecop.travel(DateTime.new(2015, 12, 31, 16))
end end
after do after do
Timecop.return Timecop.return
Timecop.freeze(Time.now.utc)
end end
it "for daily builds" do it "for daily builds" do
@ -85,11 +87,13 @@ describe Travis::API::V3::Models::Cron do
describe "disable by build works with build" do describe "disable by build works with build" do
before do before do
Timecop.return
Timecop.travel(DateTime.new(2015, 12, 31, 16)) Timecop.travel(DateTime.new(2015, 12, 31, 16))
end end
after do after do
Timecop.return Timecop.return
Timecop.freeze(Time.now.utc)
end end
it "for daily builds" do it "for daily builds" do
@ -121,11 +125,13 @@ describe Travis::API::V3::Models::Cron do
describe "disable by build works without build" do describe "disable by build works without build" do
before do before do
Timecop.return
Timecop.travel(DateTime.new(2015, 12, 31, 16)) Timecop.travel(DateTime.new(2015, 12, 31, 16))
end end
after do after do
Timecop.return Timecop.return
Timecop.freeze(Time.now.utc)
end end
it "for daily builds" do it "for daily builds" do
@ -157,12 +163,12 @@ describe Travis::API::V3::Models::Cron do
describe "build starts now if next build time is in the past" do describe "build starts now if next build time is in the past" do
before do before do
# nothing, this time Timecop.return
# time freeze is performed in examples
end end
after do after do
Timecop.return Timecop.return
Timecop.freeze(Time.now.utc)
end end
it "for daily builds with disable_by_build true" do it "for daily builds with disable_by_build true" do
@ -170,7 +176,7 @@ describe Travis::API::V3::Models::Cron do
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: true) cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: true)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 1, 19)) Timecop.freeze(DateTime.new(2016, 1, 1, 19))
expect(cron.next_enqueuing).to be == DateTime.now expect(cron.next_enqueuing).to be == DateTime.now - 5.minutes
build.destroy build.destroy
cron.destroy cron.destroy
end end
@ -180,7 +186,7 @@ describe Travis::API::V3::Models::Cron do
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: false) cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: false)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 1, 19)) Timecop.freeze(DateTime.new(2016, 1, 1, 19))
expect(cron.next_enqueuing).to be == DateTime.now expect(cron.next_enqueuing).to be == DateTime.now - 5.minutes
build.destroy build.destroy
cron.destroy cron.destroy
end end
@ -190,7 +196,7 @@ describe Travis::API::V3::Models::Cron do
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: true) cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: true)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 7, 19)) Timecop.freeze(DateTime.new(2016, 1, 7, 19))
expect(cron.next_enqueuing).to be == DateTime.now expect(cron.next_enqueuing).to be == DateTime.now - 5.minutes
build.destroy build.destroy
cron.destroy cron.destroy
end end
@ -200,7 +206,7 @@ describe Travis::API::V3::Models::Cron do
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: false) cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: false)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 7, 19)) Timecop.freeze(DateTime.new(2016, 1, 7, 19))
expect(cron.next_enqueuing).to be == DateTime.now expect(cron.next_enqueuing).to be == DateTime.now - 5.minutes
build.destroy build.destroy
cron.destroy cron.destroy
end end
@ -210,7 +216,7 @@ describe Travis::API::V3::Models::Cron do
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: true) cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: true)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 31, 19)) Timecop.freeze(DateTime.new(2016, 1, 31, 19))
expect(cron.next_enqueuing).to be == DateTime.now expect(cron.next_enqueuing).to be == DateTime.now - 5.minutes
build.destroy build.destroy
cron.destroy cron.destroy
end end
@ -220,7 +226,7 @@ describe Travis::API::V3::Models::Cron do
cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: false) cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: false)
build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron')
Timecop.freeze(DateTime.new(2016, 1, 31, 19)) Timecop.freeze(DateTime.new(2016, 1, 31, 19))
expect(cron.next_enqueuing).to be == DateTime.now expect(cron.next_enqueuing).to be == DateTime.now - 5.minutes
build.destroy build.destroy
cron.destroy cron.destroy
end end

View File

@ -4,6 +4,7 @@ describe Travis::API::V3::Queries::Crons do
let(:user) { Travis::API::V3::Models::User.find_by_login('svenfuchs') } 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(: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_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(: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') let(:query) { Travis::API::V3::Queries::Crons.new({}, 'Overview')
} }
@ -25,8 +26,22 @@ describe Travis::API::V3::Queries::Crons do
it 'enques error into a thread' 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) 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!') 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) Travis::API::V3::Models::Cron.any_instance.stubs(:branch).raises(error)
Raven.expects(:capture_exception).with(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 query.start_all
end end
end end