Skip to content

Commit

Permalink
add settings
Browse files Browse the repository at this point in the history
  • Loading branch information
svenfuchs committed Oct 30, 2017
1 parent 0cce695 commit ae974c8
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 97 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ gem 'travis-rollout', git: 'https://github.com/travis-ci/travis-rollout', re
gem 'travis-exceptions', git: 'https://github.com/travis-ci/travis-exceptions'
gem 'travis-logger', git: 'https://github.com/travis-ci/travis-logger'
gem 'travis-settings', git: 'https://github.com/travis-ci/travis-settings'
gem 'travis-encrypt', git: 'https://github.com/travis-ci/travis-encrypt'
gem 'settings', git: 'https://github.com/travis-ci/settings'
gem 'gh', git: 'https://github.com/travis-ci/gh'
gem 'coder', git: 'https://github.com/rkh/coder'

Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ GIT
net-http-persistent (~> 2.9)
net-http-pipeline

GIT
remote: https://github.com/travis-ci/settings
revision: b590df5c1f6d2e9f75e8b84cd8540e8b046318ce
specs:
settings (0.0.1)

GIT
remote: https://github.com/travis-ci/travis-encrypt
revision: b91fac2529202dcc4f638146efa88ce08c9f2432
specs:
travis-encrypt (0.0.5)

GIT
remote: https://github.com/travis-ci/travis-exceptions
revision: ab236981f810b820bc3ebfaf33f317bbaa9b3465
Expand Down Expand Up @@ -187,8 +199,10 @@ DEPENDENCIES
rollout
rspec
sentry-raven
settings!
sidekiq-pro!
travis-config (~> 1.1.3)
travis-encrypt!
travis-exceptions!
travis-lock
travis-logger!
Expand Down
4 changes: 4 additions & 0 deletions lib/travis/owners/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def owners
attrs.any? ? attrs.map { |(type, id)| find(type, id) } : [owner]
end

def uuid
OwnerGroup.where(owner: owner).pluck(:uuid).first
end

private

def find(type, id)
Expand Down
12 changes: 11 additions & 1 deletion lib/travis/owners/group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'travis/owners/record'

module Travis
module Owners
class Group < Struct.new(:all, :config)
Expand All @@ -11,8 +13,12 @@ def key
logins.join(':')
end

def id
db.uuid
end

def logins
all.map(&:login)
all.map(&:login).sort
end

def max_jobs
Expand Down Expand Up @@ -46,6 +52,10 @@ def subscriptions
@subscriptions ||= Subscriptions.new(self, plans)
end

def db
Db.new(all.first)
end

def plans
config && config[:plans] || {}
end
Expand Down
34 changes: 21 additions & 13 deletions lib/travis/scheduler/limit/by_queue.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'travis/scheduler/helper/context'
require 'travis/scheduler/helper/logging'
require 'travis/scheduler/model/settings'

module Travis
module Scheduler
Expand All @@ -8,7 +9,6 @@ class ByQueue < Struct.new(:context, :reports, :owners, :job, :selected, :state,
include Helper::Context

def enqueue?
return true unless enabled?
return true unless queue == ENV['BY_QUEUE_NAME']
result = current < max
report(max) if result
Expand All @@ -17,39 +17,47 @@ def enqueue?

private

def enabled?
config[owners.key] || ENV['BY_QUEUE_DEFAULT']
def queue
job.queue ||= Queue.new(job, context.config, nil).select
end

def current
state.running_by_queue(job.queue) + selected.select { |j| j.queue == queue }.size
end

def max
config.fetch(owners.key, default).to_i
by_config || by_setting || default
end

def queue
job.queue ||= Queue.new(job, context.config, nil).select
def by_config
config[owners.key].to_i if config.key?(owners.key)
end

def repo
job.repository
def by_setting
# p settings[:by_queue_enabled].enabled?
settings[:by_queue_enabled].enabled? && settings[:by_queue].value
end

def report(value)
reports << MSGS[:max] % [owners.to_s, "queue #{job.queue}", value]
value
def repo
job.repository
end

def default
ENV['BY_QUEUE_DEFAULT'].to_i
ENV.fetch('BY_QUEUE_DEFAULT', 2).to_i
end

# TODO make this a repo setting at some point?
def config
@config ||= ENV['BY_QUEUE_LIMIT'].to_s.split(',').map { |pair| pair.split('=') }.to_h
end

def settings
@settings ||= Model::Settings.new(owners)
end

def report(value)
reports << MSGS[:max] % [owners.to_s, "queue #{job.queue}", value]
value
end
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions lib/travis/scheduler/model/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'settings'

# hmmm.
Settings::Definition::OWNERS[:owners] = 'Travis::Owners::Group'

module Travis
module Scheduler
module Model
class Settings < ::Settings::Group
int :by_queue,
owner: [:owners],
scope: :repo,
internal: true,
requires: :by_queue_enabled

bool :by_queue_enabled,
owner: [:owners],
scope: :repo,
internal: true
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

RSpec.configure do |c|
c.mock_with :mocha

c.include FactoryGirl::Syntax::Methods
c.include Support::Env
c.include Support::Features
c.include Support::Logger
Expand Down
3 changes: 2 additions & 1 deletion spec/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def env(vars)
end

def define_env(vars)
vars.each { |key, value| ENV[key.to_s] = value.to_s }
resolve = ->(v) { v.is_a?(Proc) ? instance_exec(&v) : v }
vars.each { |key, value| ENV[key.to_s] = resolve.(value).to_s }
end

def undefine_env(vars)
Expand Down
5 changes: 5 additions & 0 deletions spec/support/factories.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'factory_girl'
require 'settings'

FactoryGirl.define do
REPO_KEY = OpenSSL::PKey::RSA.generate(4096)
Expand All @@ -12,6 +13,8 @@
login 'travis-ci'
end

factory :owner_group

factory :subscription do
valid_to Time.now + 24 * 3600
end
Expand Down Expand Up @@ -75,5 +78,7 @@
author_name 'Sven Fuchs'
author_email '[email protected]'
end

factory :setting, class: Settings::Record::Setting
end

Loading

0 comments on commit ae974c8

Please sign in to comment.