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
begin
current_runtime = Gitlab::Runtime.identify
Gitlab::AppLogger.info("Process #{Process.pid} (#{$0}) identified as: #{current_runtime}")
rescue => e
Gitlab::Runtime.identify
rescue Gitlab::Runtime::IdentificationError => e
message = <<-NOTICE
\n!! RUNTIME IDENTIFICATION FAILED: #{e}
Runtime based configuration settings may not work properly.
......
......@@ -4,8 +4,9 @@ module Gitlab
# Provides routines to identify the current runtime as which the application
# executes, such as whether it is an application server and which one.
module Runtime
AmbiguousProcessError = Class.new(StandardError)
UnknownProcessError = Class.new(StandardError)
IdentificationError = Class.new(RuntimeError)
AmbiguousProcessError = Class.new(IdentificationError)
UnknownProcessError = Class.new(IdentificationError)
class << self
def identify
......@@ -14,6 +15,8 @@ module Gitlab
matches << :unicorn if unicorn?
matches << :console if console?
matches << :sidekiq if sidekiq?
matches << :rake if rake?
matches << :rspec if rspec?
if matches.one?
matches.first
......@@ -41,6 +44,14 @@ module Gitlab
!!(defined?(::Sidekiq) && Sidekiq.server?)
end
def rake?
!!(defined?(::Rake) && Rake.application.top_level_tasks.any?)
end
def rspec?
Rails.env.test? && process_name == 'rspec'
end
def console?
!!defined?(::Rails::Console)
end
......@@ -52,6 +63,10 @@ module Gitlab
def multi_threaded?
puma? || sidekiq?
end
def process_name
File.basename($0)
end
end
end
end
......@@ -3,6 +3,10 @@
require 'spec_helper'
describe Gitlab::Runtime do
before do
allow(described_class).to receive(:process_name).and_return('ruby')
end
context "when unknown" do
it "raises an exception when trying to identify" do
expect { subject.identify }.to raise_error(subject::UnknownProcessError)
......@@ -36,6 +40,8 @@ describe Gitlab::Runtime do
expect(subject.unicorn?).to be(false)
expect(subject.sidekiq?).to be(false)
expect(subject.console?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.rspec?).to be(false)
end
end
......@@ -57,6 +63,8 @@ describe Gitlab::Runtime do
expect(subject.puma?).to be(false)
expect(subject.sidekiq?).to be(false)
expect(subject.console?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.rspec?).to be(false)
end
end
......@@ -77,6 +85,8 @@ describe Gitlab::Runtime do
expect(subject.unicorn?).to be(false)
expect(subject.puma?).to be(false)
expect(subject.console?).to be(false)
expect(subject.rake?).to be(false)
expect(subject.rspec?).to be(false)
end
end
......@@ -96,6 +106,26 @@ describe Gitlab::Runtime do
expect(subject.unicorn?).to be(false)
expect(subject.sidekiq?).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
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