Revert "Revert "Merge branch 'master' of github.com:travis-ci/travis-api""

This reverts commit 171b9c57c7.
This commit is contained in:
carlad 2015-01-27 16:22:30 +01:00
parent 737a31ad23
commit 517a3b04f7
13 changed files with 234 additions and 50 deletions

1
.gitignore vendored
View File

@ -4,4 +4,3 @@ log/
vendor vendor
config/skylight.yml config/skylight.yml
.coverage .coverage
.coverage/

View File

@ -8,7 +8,7 @@ gem 's3', github: 'travis-ci/s3'
gem 'travis-core', github: 'travis-ci/travis-core' gem 'travis-core', github: 'travis-ci/travis-core'
gem 'travis-support', github: 'travis-ci/travis-support' gem 'travis-support', github: 'travis-ci/travis-support'
gem 'travis-config', '~> 0.1.0' gem 'travis-config', '~> 0.1.0'
gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs', require: nil, ref: 'cde9741' gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs', require: nil
gem 'travis-yaml', github: 'travis-ci/travis-yaml' gem 'travis-yaml', github: 'travis-ci/travis-yaml'
gem 'mustermann', github: 'rkh/mustermann' gem 'mustermann', github: 'rkh/mustermann'
gem 'sinatra' gem 'sinatra'

View File

@ -74,11 +74,10 @@ GIT
GIT GIT
remote: git://github.com/travis-ci/travis-sidekiqs.git remote: git://github.com/travis-ci/travis-sidekiqs.git
revision: cde9741f8e6811fe3929bdd0dd653b87289c5eef revision: 21a2fee158e25252dd78f5fa31e81b4f6583be23
ref: cde9741
specs: specs:
travis-sidekiqs (0.0.1) travis-sidekiqs (0.0.1)
sidekiq (~> 2.5.0) sidekiq
GIT GIT
remote: git://github.com/travis-ci/travis-support.git remote: git://github.com/travis-ci/travis-support.git
@ -149,26 +148,13 @@ GEM
backports (3.6.4) backports (3.6.4)
builder (3.0.4) builder (3.0.4)
bunny (0.8.0) bunny (0.8.0)
celluloid (0.12.0) celluloid (0.16.0)
timers (>= 1.0.0) timers (~> 4.0.0)
chunky_png (1.3.3)
coder (0.4.0) coder (0.4.0)
coderay (1.1.0) coderay (1.1.0)
coercible (1.0.0) coercible (1.0.0)
descendants_tracker (~> 0.0.1) descendants_tracker (~> 0.0.1)
compass (1.0.1) connection_pool (2.1.1)
chunky_png (~> 1.2)
compass-core (~> 1.0.1)
compass-import-once (~> 1.0.5)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
sass (>= 3.3.13, < 3.5)
compass-core (1.0.1)
multi_json (~> 1.0)
sass (>= 3.3.0, < 3.5)
compass-import-once (1.0.5)
sass (>= 3.2, < 3.5)
connection_pool (0.9.3)
daemons (1.1.9) daemons (1.1.9)
dalli (2.7.2) dalli (2.7.2)
data_migrations (0.0.1) data_migrations (0.0.1)
@ -282,16 +268,12 @@ GEM
rspec-expectations (2.99.2) rspec-expectations (2.99.2)
diff-lcs (>= 1.1.3, < 2.0) diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.99.2) rspec-mocks (2.99.2)
sass (3.4.6) sidekiq (3.3.0)
sidekiq (2.5.0) celluloid (>= 0.16.0)
celluloid (~> 0.12.0) connection_pool (>= 2.0.0)
compass json
connection_pool (~> 0.9.2) redis (>= 3.0.6)
multi_json (~> 1) redis-namespace (>= 1.3.1)
redis (~> 3)
redis-namespace
sass
sprockets-sass
signature (0.1.7) signature (0.1.7)
simple_states (1.0.1) simple_states (1.0.1)
activesupport activesupport
@ -320,9 +302,6 @@ GEM
multi_json (~> 1.0) multi_json (~> 1.0)
rack (~> 1.0) rack (~> 1.0)
tilt (~> 1.1, != 1.3.0) tilt (~> 1.1, != 1.3.0)
sprockets-sass (1.2.0)
sprockets (~> 2.0)
tilt (~> 1.1)
thin (1.6.3) thin (1.6.3)
daemons (~> 1.0, >= 1.0.9) daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0) eventmachine (~> 1.0)

View File

@ -1,2 +1,3 @@
web: bundle exec ./script/server web: bundle exec ./script/server
console: bundle exec ./script/console console: bundle exec ./script/console
sidekiq: bundle exec sidekiq -c 5 -r ./lib/travis/sidekiq.rb -q build_cancellations, -q build_restarts, -q job_cancellations, -q job_restarts

View File

@ -1,4 +1,6 @@
require 'travis/api/app' require 'travis/api/app'
require 'travis/api/workers/build_cancellation'
require 'travis/api/workers/build_restart'
class Travis::Api::App class Travis::Api::App
class Endpoint class Endpoint
@ -37,7 +39,7 @@ class Travis::Api::App
status 422 status 422
respond_with json respond_with json
else else
service.run Travis::Sidekiq::BuildCancellation.perform_async(id: params[:id], user_id: current_user.id, source: 'api')
Metriks.meter("api.request.cancel_build.success").mark Metriks.meter("api.request.cancel_build.success").mark
status 204 status 204
@ -46,7 +48,17 @@ class Travis::Api::App
post '/:id/restart' do post '/:id/restart' do
Metriks.meter("api.request.restart_build").mark Metriks.meter("api.request.restart_build").mark
respond_with service(:reset_model, build_id: params[:id])
service = self.service(:reset_model, build_id: params[:id])
if !service.accept?
status 400
result = false
else
Travis::Sidekiq::BuildRestart.perform_async(id: params[:id], user_id: current_user.id)
status 202
result = true
end
respond_with(result: result, flash: service.messages)
end end
end end
end end

View File

@ -1,4 +1,6 @@
require 'travis/api/app' require 'travis/api/app'
require 'travis/api/workers/job_cancellation'
require 'travis/api/workers/job_restart'
class Travis::Api::App class Travis::Api::App
class Endpoint class Endpoint
@ -44,7 +46,7 @@ class Travis::Api::App
status 422 status 422
respond_with json respond_with json
else else
service.run Travis::Sidekiq::JobCancellation.perform_async(id: params[:id], user_id: current_user.id, source: 'api')
Metriks.meter("api.request.cancel_job.success").mark Metriks.meter("api.request.cancel_job.success").mark
status 204 status 204
@ -53,7 +55,17 @@ class Travis::Api::App
post '/:id/restart' do post '/:id/restart' do
Metriks.meter("api.request.restart_job").mark Metriks.meter("api.request.restart_job").mark
respond_with service(:reset_model, job_id: params[:id])
service = self.service(:reset_model, job_id: params[:id])
if !service.accept?
status 400
result = false
else
Travis::Sidekiq::JobRestart.perform_async(id: params[:id], user_id: current_user.id)
status 202
result = true
end
respond_with(result: result, flash: service.messages)
end end
get '/:job_id/log' do get '/:job_id/log' do

View File

@ -0,0 +1,19 @@
require 'sidekiq/worker'
require 'multi_json'
module Travis
module Sidekiq
class BuildCancellation
class ProcessingError < StandardError; end
include ::Sidekiq::Worker
sidekiq_options queue: :build_cancellations
def perform(data)
user = User.find(data['user_id'])
Travis.service(:cancel_build, user, { id: data['id'], source: data['source'] }).run
end
end
end
end

View File

@ -0,0 +1,19 @@
require 'sidekiq/worker'
require 'multi_json'
module Travis
module Sidekiq
class BuildRestart
class ProcessingError < StandardError; end
include ::Sidekiq::Worker
sidekiq_options queue: :build_restarts
def perform(data)
user = User.find(data['user_id'])
Travis.service(:reset_model, user, build_id: data['id']).run
end
end
end
end

View File

@ -0,0 +1,19 @@
require 'sidekiq/worker'
require 'multi_json'
module Travis
module Sidekiq
class JobCancellation
class ProcessingError < StandardError; end
include ::Sidekiq::Worker
sidekiq_options queue: :job_cancellations
def perform(data)
user = User.find(data['user_id'])
Travis.service(:cancel_job, user, { id: data['id'], source: data['source'] }).run
end
end
end
end

View File

@ -0,0 +1,19 @@
require 'sidekiq/worker'
require 'multi_json'
module Travis
module Sidekiq
class JobRestart
class ProcessingError < StandardError; end
include ::Sidekiq::Worker
sidekiq_options queue: :job_restarts
def perform(data)
user = User.find(data['user_id'])
Travis.service(:reset_model, user, job_id: data['id']).run
end
end
end
end

22
lib/travis/sidekiq.rb Normal file
View File

@ -0,0 +1,22 @@
#$: << './lib'
require 'sidekiq'
require 'travis'
require 'travis/api/workers/build_cancellation'
require 'travis/api/workers/build_restart'
require 'travis/api/workers/job_cancellation'
require 'travis/api/workers/job_restart'
require 'travis/support/amqp'
Travis::Database.connect
Travis::Async.enabled = true
Travis::Amqp.config = Travis.config.amqp
Travis::Metrics.setup
Travis::Notification.setup
Sidekiq.configure_server do |config|
config.redis = Travis.config.redis.merge(namespace: Travis.config.sidekiq.namespace)
end
Sidekiq.configure_client do |config|
config.redis = Travis.config.redis.merge(size: 1, namespace: Travis.config.sidekiq.namespace)
end

View File

@ -75,17 +75,60 @@ describe 'Builds' do
end end
context 'when build can be canceled' do context 'when build can be canceled' do
it 'cancels the build and responds with 204' do before do
Travis::Sidekiq::BuildCancellation.stubs(:perform_async)
build.matrix.each { |j| j.update_attribute(:state, 'created') } build.matrix.each { |j| j.update_attribute(:state, 'created') }
build.update_attribute(:state, 'created') build.update_attribute(:state, 'created')
end
response = nil it 'cancels the build' do
expect { Travis::Sidekiq::BuildCancellation.expects(:perform_async).with( id: build.id.to_s, user_id: user.id, source: 'api')
response = post "/builds/#{build.id}/cancel", {}, headers post "/builds/#{build.id}/cancel", {}, headers
}.to change { build.reload.state } end
it 'responds with 204' do
response = post "/builds/#{build.id}/cancel", {}, headers
response.status.should == 204 response.status.should == 204
end
end
end
build.state.should == 'canceled' describe 'POST /builds/:id/restart' do
let(:user) { User.where(login: 'svenfuchs').first }
let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: -1) }
before {
headers.merge! 'HTTP_AUTHORIZATION' => "token #{token}"
user.permissions.create!(repository_id: build.repository.id, :pull => true, :push => true)
}
context 'when restart is not acceptable' do
before { user.permissions.destroy_all }
it 'responds with 400' do
response = post "/builds/#{build.id}/restart", {}, headers
response.status.should == 400
end
end
context 'when build passed' do
before do
Travis::Sidekiq::BuildCancellation.stubs(:perform_async)
build.matrix.each { |j| j.update_attribute(:state, 'passed') }
build.update_attribute(:state, 'passed')
end
it 'restarts the build' do
Travis::Sidekiq::BuildRestart.expects(:perform_async).with(id: build.id.to_s, user_id: user.id)
response = post "/builds/#{build.id}/restart", {}, headers
response.status.should == 202
end
it 'sends the correct response body' do
Travis::Sidekiq::BuildRestart.expects(:perform_async).with(id: build.id.to_s, user_id: user.id)
response = post "/builds/#{build.id}/restart", {}, headers
body = JSON.parse(response.body)
body.should == {"result"=>true, "flash"=>[{"notice"=>"The build was successfully restarted."}]}
end end
end end
end end

View File

@ -242,16 +242,56 @@ describe 'Jobs' do
end end
context 'when job can be canceled' do context 'when job can be canceled' do
it 'cancels the job and responds with 204' do before do
job.update_attribute(:state, 'created') job.update_attribute(:state, 'created')
end
response = nil it 'cancels the job' do
expect { Travis::Sidekiq::JobCancellation.expects(:perform_async).with( id: job.id.to_s, user_id: user.id, source: 'api')
response = post "/jobs/#{job.id}/cancel", {}, headers post "/jobs/#{job.id}/cancel", {}, headers
}.to change { job.reload.state } end
it 'responds with 204' do
response = post "/jobs/#{job.id}/cancel", {}, headers
response.status.should == 204 response.status.should == 204
end
end
end
job.state.should == 'canceled' describe 'POST /jobs/:id/restart' do
let(:user) { User.where(login: 'svenfuchs').first }
let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: -1) }
before {
headers.merge! 'HTTP_AUTHORIZATION' => "token #{token}"
user.permissions.create!(repository_id: job.repository.id, :pull => true, :push => true)
}
context 'when restart is not acceptable' do
before { user.permissions.destroy_all }
it 'responds with 400' do
response = post "/jobs/#{job.id}/restart", {}, headers
response.status.should == 400
end
end
context 'when job passed' do
before do
Travis::Sidekiq::JobCancellation.stubs(:perform_async)
job.update_attribute(:state, 'passed')
end
it 'restarts the job' do
Travis::Sidekiq::JobRestart.expects(:perform_async).with(id: job.id.to_s, user_id: user.id)
response = post "/jobs/#{job.id}/restart", {}, headers
response.status.should == 202
end
it 'sends the correct response body' do
Travis::Sidekiq::JobRestart.expects(:perform_async).with(id: job.id.to_s, user_id: user.id)
response = post "/jobs/#{job.id}/restart", {}, headers
body = JSON.parse(response.body)
body.should == {"result"=>true, "flash"=>[{"notice"=>"The job was successfully restarted."}]}
end end
end end
end end