summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-10-13 12:58:25 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-10-13 12:58:25 +0200
commit204fdcb1abb9c76b2d4bd6260c6e5ce91529aeb8 (patch)
tree0e62bd21453b627ae74139c985cd0faec623479b
parentfafc5a1777484ba6b1b12c5e88f3fc783fb4d839 (diff)
downloadgitlab-ce-204fdcb1abb9c76b2d4bd6260c6e5ce91529aeb8.tar.gz
Add build success worker that runs asynchronously
-rw-r--r--app/models/ci/build.rb21
-rw-r--r--app/workers/build_success_worker.rb27
-rw-r--r--spec/workers/build_success_worker_spec.rb25
3 files changed, 61 insertions, 12 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index b0d13c1bf06..ff70f3deb52 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -76,25 +76,22 @@ module Ci
state_machine :status do
after_transition pending: :running do |build|
- build.run_after_commit { BuildHooksWorker.perform_async(id) }
+ build.run_after_commit do
+ BuildHooksWorker.perform_async(id)
+ end
end
after_transition any => [:success, :failed, :canceled] do |build|
build.update_coverage
- build.run_after_commit { BuildHooksWorker.perform_async(id) }
+
+ build.run_after_commit do
+ BuildHooksWorker.perform_async(id)
+ end
end
after_transition any => [:success] do |build|
- if build.environment.present?
- service = CreateDeploymentService.new(
- build.project, build.user,
- environment: build.environment,
- sha: build.sha,
- ref: build.ref,
- tag: build.tag,
- options: build.options.to_h[:environment],
- variables: build.variables)
- service.execute(build)
+ build.run_after_commit do
+ BuildSuccessWorker.perform_async(id)
end
end
end
diff --git a/app/workers/build_success_worker.rb b/app/workers/build_success_worker.rb
new file mode 100644
index 00000000000..d6db7642d82
--- /dev/null
+++ b/app/workers/build_success_worker.rb
@@ -0,0 +1,27 @@
+class BuildSuccessWorker
+ include Sidekiq::Worker
+ sidekiq_options queue: :default
+
+ def perform(build_id)
+ Ci::Build.find_by(id: build_id).try do |build|
+ perform_deloyment(build)
+ end
+ end
+
+ private
+
+ def perform_deloyment(build)
+ return if build.environment.blank?
+
+ service = CreateDeploymentService.new(
+ build.project, build.user,
+ environment: build.environment,
+ sha: build.sha,
+ ref: build.ref,
+ tag: build.tag,
+ options: build.options.to_h[:environment],
+ variables: build.variables)
+
+ service.execute(build)
+ end
+end
diff --git a/spec/workers/build_success_worker_spec.rb b/spec/workers/build_success_worker_spec.rb
new file mode 100644
index 00000000000..c42bcd45d50
--- /dev/null
+++ b/spec/workers/build_success_worker_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe BuildSuccessWorker do
+ describe '#perform' do
+ context 'when build exists' do
+ context 'when build belogs to the environment' do
+ let!(:build) { create(:ci_build, environment: 'production') }
+
+ it 'executes deployment service' do
+ expect_any_instance_of(CreateDeploymentService)
+ .to receive(:execute)
+
+ described_class.new.perform(build.id)
+ end
+ 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