Enqueue restarting jobs for the Hub

This commit is contained in:
Ana Rosas 2016-05-26 17:08:22 +02:00
parent fa50bd13de
commit 6de524d84c
4 changed files with 69 additions and 27 deletions

View File

@ -1,6 +1,7 @@
require 'travis/api/app' require 'travis/api/app'
require 'travis/api/workers/job_cancellation' require 'travis/api/workers/job_cancellation'
require 'travis/api/workers/job_restart' require 'travis/api/workers/job_restart'
require 'travis/api/enqueue/services/restart_model'
class Travis::Api::App class Travis::Api::App
class Endpoint class Endpoint
@ -55,15 +56,27 @@ 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
if Travis::Features.owner_active?(:enqueue_to_hub, current_user)
service = self.service(:reset_model, job_id: params[:id]) service = Travis::Enqueue::Services::RestartModel.new(current_user, { job_id: params[:id] })
if !service.accept? if !service.accept?
status 400 status 400
result = false result = false
else
payload = {id: params[:id], user_id: current_user.id}
service.push("job:restart", payload)
status 202
result = true
end
else else
Travis::Sidekiq::JobRestart.perform_async(id: params[:id], user_id: current_user.id) service = self.service(:reset_model, job_id: params[:id])
status 202 if !service.accept?
result = true status 400
result = false
else
Travis::Sidekiq::JobRestart.perform_async(id: params[:id], user_id: current_user.id)
status 202
result = true
end
end end
respond_with(result: result, flash: service.messages) respond_with(result: result, flash: service.messages)
end end

View File

@ -12,11 +12,13 @@ module Travis
end end
def push(event, payload) def push(event, payload)
::Sidekiq::Client.push( if current_user && target && accept?
'queue' => 'hub', ::Sidekiq::Client.push(
'class' => 'Travis::Hub::Sidekiq::Worker', 'queue' => 'hub',
'args' => [event, payload] 'class' => 'Travis::Hub::Sidekiq::Worker',
) 'args' => [event, payload]
)
end
end end
def accept? def accept?

View File

@ -117,7 +117,7 @@ describe 'Builds' do
build.update_attribute(:state, 'passed') build.update_attribute(:state, 'passed')
end end
describe 'Enqueues restart event to the Hub' do describe 'Enqueues restart event for the Hub' do
before { Travis::Features.activate_owner(:enqueue_to_hub, repo.owner) } before { Travis::Features.activate_owner(:enqueue_to_hub, repo.owner) }
it 'restarts the build' do it 'restarts the build' do

View File

@ -274,24 +274,51 @@ describe 'Jobs' do
response = post "/jobs/#{job.id}/restart", {}, headers response = post "/jobs/#{job.id}/restart", {}, headers
response.status.should == 400 response.status.should == 400
end end
context 'when enqueueing for the Hub' do
before { Travis::Features.activate_owner(:enqueue_to_hub, job.repository.owner) }
it 'responds with 400' do
response = post "/jobs/#{job.id}/restart", {}, headers
response.status.should == 400
end
end
end end
context 'when job passed' do context 'when job passed' do
before do before { job.update_attribute(:state, 'passed') }
Travis::Sidekiq::JobCancellation.stubs(:perform_async)
job.update_attribute(:state, 'passed') context 'Restart from travis-core' do
before { Travis::Sidekiq::JobCancellation.stubs(:perform_async) }
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
it 'restarts the job' do context 'Enqueues restart event for the Hub' do
Travis::Sidekiq::JobRestart.expects(:perform_async).with(id: job.id.to_s, user_id: user.id) before { Travis::Features.activate_owner(:enqueue_to_hub, job.repository.owner) }
response = post "/jobs/#{job.id}/restart", {}, headers
response.status.should == 202 it 'restarts the job' do
end ::Sidekiq::Client.expects(:push)
it 'sends the correct response body' do response = post "/jobs/#{job.id}/restart", {}, headers
Travis::Sidekiq::JobRestart.expects(:perform_async).with(id: job.id.to_s, user_id: user.id) response.status.should == 202
response = post "/jobs/#{job.id}/restart", {}, headers end
body = JSON.parse(response.body) it 'sends the correct response body' do
body.should == {"result"=>true, "flash"=>[{"notice"=>"The job was successfully restarted."}]} ::Sidekiq::Client.expects(:push)
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 end