Skip to content

Refactor ByStage job sorting #84

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: master
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
14 changes: 3 additions & 11 deletions lib/travis/scheduler/limit/by_stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def queueable
def jobs
@jobs ||= begin
# TODO would it make sense to cache these on `state`?
jobs = Job.where(source_id: job.source_id)
sort(jobs).map { |job| attrs(job) }
Job.where(source_id: job.source_id)
.sort_by(&:stage_number_parts)
.map(&method(:attrs))
end
end

Expand All @@ -45,15 +46,6 @@ def attrs(job)
}
end

def sort(jobs)
num = ->(job) { job.stage_number.split('.').map(&:to_i) }
jobs.sort { |lft, rgt| num.(lft) <=> num.(rgt) }
end

def stages
jobs.map { |job| job[:stage] }
end

def report
reports << MSGS[:max_stage] % ["build #{job.source_id}", job.stage.number, queueable.size]
end
Expand Down
4 changes: 4 additions & 0 deletions lib/travis/scheduler/record/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def finished?
FINISHED_STATES.include?(state.try(:to_sym))
end

def stage_number_parts
stage_number.split('.').map(&:to_i)
end

def queueable=(value)
if value
queueable || create_queueable
Expand Down
6 changes: 5 additions & 1 deletion spec/travis/scheduler/record/job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
describe Job do
let(:config) { { rvm: '1.8.7' } }
let(:job) { FactoryGirl.create(:job, config: config).reload }
let(:job) { FactoryGirl.create(:job, config: config, stage_number: '1.2').reload }

it 'deserializes config' do
expect(job.config).to be_a(Hash)
end

it 'renders its stage number numeric parts' do
expect(job.stage_number_parts).to eq [1, 2]
end
end