summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-10-13 12:45:16 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-10-13 12:45:16 +0200
commitfafc5a1777484ba6b1b12c5e88f3fc783fb4d839 (patch)
tree961264587ff7dd930eb503589cf8bc4ed0caacf5
parent2461e10912b484c6942cd26f8fffd5c5557befa7 (diff)
downloadgitlab-ce-fafc5a1777484ba6b1b12c5e88f3fc783fb4d839.tar.gz
Perform CI build hooks asynchronously using worker
-rw-r--r--app/models/ci/build.rb5
-rw-r--r--app/workers/build_hooks_worker.rb9
-rw-r--r--spec/workers/build_hooks_worker_spec.rb23
3 files changed, 35 insertions, 2 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index 5dbf66173de..b0d13c1bf06 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -1,6 +1,7 @@
module Ci
class Build < CommitStatus
include TokenAuthenticatable
+ include AfterCommitQueue
belongs_to :runner, class_name: 'Ci::Runner'
belongs_to :trigger_request, class_name: 'Ci::TriggerRequest'
@@ -75,12 +76,12 @@ module Ci
state_machine :status do
after_transition pending: :running do |build|
- build.execute_hooks
+ build.run_after_commit { BuildHooksWorker.perform_async(id) }
end
after_transition any => [:success, :failed, :canceled] do |build|
build.update_coverage
- build.execute_hooks
+ build.run_after_commit { BuildHooksWorker.perform_async(id) }
end
after_transition any => [:success] do |build|
diff --git a/app/workers/build_hooks_worker.rb b/app/workers/build_hooks_worker.rb
new file mode 100644
index 00000000000..e22ececb3fd
--- /dev/null
+++ b/app/workers/build_hooks_worker.rb
@@ -0,0 +1,9 @@
+class BuildHooksWorker
+ include Sidekiq::Worker
+ sidekiq_options queue: :default
+
+ def perform(build_id)
+ Ci::Build.find_by(id: build_id)
+ .try(:execute_hooks)
+ end
+end
diff --git a/spec/workers/build_hooks_worker_spec.rb b/spec/workers/build_hooks_worker_spec.rb
new file mode 100644
index 00000000000..97654a93f5c
--- /dev/null
+++ b/spec/workers/build_hooks_worker_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe BuildHooksWorker do
+ describe '#perform' do
+ context 'when build exists' do
+ let!(:build) { create(:ci_build) }
+
+ it 'calls build hooks' do
+ expect_any_instance_of(Ci::Build)
+ .to receive(:execute_hooks)
+
+ described_class.new.perform(build.id)
+ end
+ end
+
+ context 'when build does not exist' do
+ it 'does not raise exception' do
+ expect { described_class.new.perform(123) }
+ .not_to raise_error
+ end
+ end
+ end
+end