Commit 008bb14d authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch 'sh-remove-runtime-initializer' into 'master'

Handle rspec and rake tasks in runtime identifier

Closes #122656

See merge request gitlab-org/gitlab!22276
parents 5aa6e008 540810dc
# frozen_string_literal: true # frozen_string_literal: true
begin begin
current_runtime = Gitlab::Runtime.identify Gitlab::Runtime.identify
Gitlab::AppLogger.info("Process #{Process.pid} (#{$0}) identified as: #{current_runtime}") rescue Gitlab::Runtime::IdentificationError => e
rescue => e
message = <<-NOTICE message = <<-NOTICE
\n!! RUNTIME IDENTIFICATION FAILED: #{e} \n!! RUNTIME IDENTIFICATION FAILED: #{e}
Runtime based configuration settings may not work properly. Runtime based configuration settings may not work properly.
......
...@@ -4,8 +4,9 @@ module Gitlab ...@@ -4,8 +4,9 @@ module Gitlab
# Provides routines to identify the current runtime as which the application # Provides routines to identify the current runtime as which the application
# executes, such as whether it is an application server and which one. # executes, such as whether it is an application server and which one.
module Runtime module Runtime
AmbiguousProcessError = Class.new(StandardError) IdentificationError = Class.new(RuntimeError)
UnknownProcessError = Class.new(StandardError) AmbiguousProcessError = Class.new(IdentificationError)
UnknownProcessError = Class.new(IdentificationError)
class << self class << self
def identify def identify
...@@ -14,6 +15,8 @@ module Gitlab ...@@ -14,6 +15,8 @@ module Gitlab
matches << :unicorn if unicorn? matches << :unicorn if unicorn?
matches << :console if console? matches << :console if console?
matches << :sidekiq if sidekiq? matches << :sidekiq if sidekiq?
matches << :rake if rake?
matches << :rspec if rspec?
if matches.one? if matches.one?
matches.first matches.first
...@@ -41,6 +44,14 @@ module Gitlab ...@@ -41,6 +44,14 @@ module Gitlab
!!(defined?(::Sidekiq) && Sidekiq.server?) !!(defined?(::Sidekiq) && Sidekiq.server?)
end end
def rake?
!!(defined?(::Rake) && Rake.application.top_level_tasks.any?)
end
def rspec?
Rails.env.test? && process_name == 'rspec'
end
def console? def console?
!!defined?(::Rails::Console) !!defined?(::Rails::Console)
end end
...@@ -52,6 +63,10 @@ module Gitlab ...@@ -52,6 +63,10 @@ module Gitlab
def multi_threaded? def multi_threaded?
puma? || sidekiq? puma? || sidekiq?
end end
def process_name
File.basename($0)
end
end end
end end
end end
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Runtime do describe Gitlab::Runtime do
before do
allow(described_class).to receive(:process_name).and_return('ruby')
end
context "when unknown" do context "when unknown" do
it "raises an exception when trying to identify" do it "raises an exception when trying to identify" do
expect { subject.identify }.to raise_error(subject::UnknownProcessError) expect { subject.identify }.to raise_error(subject::UnknownProcessError)
...@@ -36,6 +40,8 @@ describe Gitlab::Runtime do ...@@ -36,6 +40,8 @@ describe Gitlab::Runtime do
expect(subject.unicorn?).to be(false) expect(subject.unicorn?).to be(false)
expect(subject.sidekiq?).to be(false) expect(subject.sidekiq?).to be(false)
expect(subject.console?).to be(false) expect(subject.console?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.rspec?).to be(false)
end end
end end
...@@ -57,6 +63,8 @@ describe Gitlab::Runtime do ...@@ -57,6 +63,8 @@ describe Gitlab::Runtime do
expect(subject.puma?).to be(false) expect(subject.puma?).to be(false)
expect(subject.sidekiq?).to be(false) expect(subject.sidekiq?).to be(false)
expect(subject.console?).to be(false) expect(subject.console?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.rspec?).to be(false)
end end
end end
...@@ -77,6 +85,8 @@ describe Gitlab::Runtime do ...@@ -77,6 +85,8 @@ describe Gitlab::Runtime do
expect(subject.unicorn?).to be(false) expect(subject.unicorn?).to be(false)
expect(subject.puma?).to be(false) expect(subject.puma?).to be(false)
expect(subject.console?).to be(false) expect(subject.console?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.rspec?).to be(false)
end end
end end
...@@ -96,6 +106,26 @@ describe Gitlab::Runtime do ...@@ -96,6 +106,26 @@ describe Gitlab::Runtime do
expect(subject.unicorn?).to be(false) expect(subject.unicorn?).to be(false)
expect(subject.sidekiq?).to be(false) expect(subject.sidekiq?).to be(false)
expect(subject.puma?).to be(false) expect(subject.puma?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.rspec?).to be(false)
end
end
context "rspec" do
before do
allow(described_class).to receive(:process_name).and_return('rspec')
end
it "identifies itself" do
expect(subject.identify).to eq(:rspec)
expect(subject.rspec?).to be(true)
end
it "does not identify as others" do
expect(subject.unicorn?).to be(false)
expect(subject.sidekiq?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.puma?).to be(false)
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment