From 6de524d84cbf9f76089f5bda906a060cc4b54607 Mon Sep 17 00:00:00 2001 From: Ana Rosas Date: Thu, 26 May 2016 17:08:22 +0200 Subject: [PATCH] Enqueue restarting jobs for the Hub --- lib/travis/api/app/endpoint/jobs.rb | 29 +++++++--- .../api/enqueue/services/restart_model.rb | 12 +++-- spec/integration/v2/builds_spec.rb | 2 +- spec/integration/v2/jobs_spec.rb | 53 ++++++++++++++----- 4 files changed, 69 insertions(+), 27 deletions(-) diff --git a/lib/travis/api/app/endpoint/jobs.rb b/lib/travis/api/app/endpoint/jobs.rb index 9bb2fbc9..e8a0e1e9 100644 --- a/lib/travis/api/app/endpoint/jobs.rb +++ b/lib/travis/api/app/endpoint/jobs.rb @@ -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 diff --git a/lib/travis/api/enqueue/services/restart_model.rb b/lib/travis/api/enqueue/services/restart_model.rb index 7b7ae649..ff9f5fec 100644 --- a/lib/travis/api/enqueue/services/restart_model.rb +++ b/lib/travis/api/enqueue/services/restart_model.rb @@ -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? diff --git a/spec/integration/v2/builds_spec.rb b/spec/integration/v2/builds_spec.rb index 98a57e26..c4e78d44 100644 --- a/spec/integration/v2/builds_spec.rb +++ b/spec/integration/v2/builds_spec.rb @@ -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 diff --git a/spec/integration/v2/jobs_spec.rb b/spec/integration/v2/jobs_spec.rb index 81486845..72dac3d0 100644 --- a/spec/integration/v2/jobs_spec.rb +++ b/spec/integration/v2/jobs_spec.rb @@ -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