Enqueue restarting jobs for the Hub
This commit is contained in:
parent
fa50bd13de
commit
6de524d84c
|
@ -1,6 +1,7 @@
|
|||
require 'travis/api/app'
|
||||
require 'travis/api/workers/job_cancellation'
|
||||
require 'travis/api/workers/job_restart'
|
||||
require 'travis/api/enqueue/services/restart_model'
|
||||
|
||||
class Travis::Api::App
|
||||
class Endpoint
|
||||
|
@ -55,15 +56,27 @@ class Travis::Api::App
|
|||
|
||||
post '/:id/restart' do
|
||||
Metriks.meter("api.request.restart_job").mark
|
||||
|
||||
service = self.service(:reset_model, job_id: params[:id])
|
||||
if !service.accept?
|
||||
status 400
|
||||
result = false
|
||||
if Travis::Features.owner_active?(:enqueue_to_hub, current_user)
|
||||
service = Travis::Enqueue::Services::RestartModel.new(current_user, { job_id: params[:id] })
|
||||
if !service.accept?
|
||||
status 400
|
||||
result = false
|
||||
else
|
||||
payload = {id: params[:id], user_id: current_user.id}
|
||||
service.push("job:restart", payload)
|
||||
status 202
|
||||
result = true
|
||||
end
|
||||
else
|
||||
Travis::Sidekiq::JobRestart.perform_async(id: params[:id], user_id: current_user.id)
|
||||
status 202
|
||||
result = true
|
||||
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
|
||||
end
|
||||
respond_with(result: result, flash: service.messages)
|
||||
end
|
||||
|
|
|
@ -12,11 +12,13 @@ module Travis
|
|||
end
|
||||
|
||||
def push(event, payload)
|
||||
::Sidekiq::Client.push(
|
||||
'queue' => 'hub',
|
||||
'class' => 'Travis::Hub::Sidekiq::Worker',
|
||||
'args' => [event, payload]
|
||||
)
|
||||
if current_user && target && accept?
|
||||
::Sidekiq::Client.push(
|
||||
'queue' => 'hub',
|
||||
'class' => 'Travis::Hub::Sidekiq::Worker',
|
||||
'args' => [event, payload]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def accept?
|
||||
|
|
|
@ -117,7 +117,7 @@ describe 'Builds' do
|
|||
build.update_attribute(:state, 'passed')
|
||||
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) }
|
||||
|
||||
it 'restarts the build' do
|
||||
|
|
|
@ -274,24 +274,51 @@ describe 'Jobs' do
|
|||
response = post "/jobs/#{job.id}/restart", {}, headers
|
||||
response.status.should == 400
|
||||
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
|
||||
|
||||
context 'when job passed' do
|
||||
before do
|
||||
Travis::Sidekiq::JobCancellation.stubs(:perform_async)
|
||||
job.update_attribute(:state, 'passed')
|
||||
before { 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
|
||||
|
||||
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."}]}
|
||||
context 'Enqueues restart event for the Hub' do
|
||||
before { Travis::Features.activate_owner(:enqueue_to_hub, job.repository.owner) }
|
||||
|
||||
it 'restarts the job' do
|
||||
::Sidekiq::Client.expects(:push)
|
||||
response = post "/jobs/#{job.id}/restart", {}, headers
|
||||
response.status.should == 202
|
||||
end
|
||||
it 'sends the correct response body' do
|
||||
::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
|
||||
|
|
Loading…
Reference in New Issue
Block a user