Commit cd29a8f9 authored by Matthias Kaeppler's avatar Matthias Kaeppler

Improve runtime identification errors

- don't allow `:unknown` runtime, raise error instead
- log errors to Sentry in initializer
parent d7d1295b
# frozen_string_literal: true
begin
Gitlab::AppLogger.info("Runtime: #{Gitlab::Runtime.name}")
current_runtime = Gitlab::Runtime.identify
Gitlab::AppLogger.info("Process #{Process.pid} (#{$0}) identified as: #{current_runtime}")
rescue => e
message = <<-NOTICE
\n!! RUNTIME IDENTIFICATION FAILED: #{e}
......@@ -10,4 +11,5 @@ rescue => e
https://gitlab.com/gitlab-org/gitlab/issues/new
NOTICE
Gitlab::AppLogger.error(message)
Gitlab::ErrorTracking.track_exception(e)
end
......@@ -4,17 +4,28 @@ 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)
class << self
def name
def identify
matches = []
matches << :puma if puma?
matches << :unicorn if unicorn?
matches << :console if console?
matches << :sidekiq if sidekiq?
raise "Ambiguous process match: #{matches}" if matches.size > 1
matches.first || :unknown
if matches.one?
matches.first
elsif matches.none?
raise UnknownProcessError.new(
"Failed to identify runtime for process #{Process.pid} (#{$0})"
)
else
raise AmbiguousProcessError.new(
"Ambiguous runtime #{matches} for process #{Process.pid} (#{$0})"
)
end
end
def puma?
......
......@@ -4,8 +4,8 @@ require 'spec_helper'
describe Gitlab::Runtime do
context "when unknown" do
it "identifies as :unknown" do
expect(subject.name).to eq(:unknown)
it "raises an exception when trying to identify" do
expect { subject.identify }.to raise_error(subject::UnknownProcessError)
end
end
......@@ -16,7 +16,7 @@ describe Gitlab::Runtime do
end
it "raises an exception when trying to identify" do
expect { subject.name }.to raise_error(RuntimeError, "Ambiguous process match: [:puma, :console]")
expect { subject.identify }.to raise_error(subject::AmbiguousProcessError)
end
end
......@@ -28,7 +28,7 @@ describe Gitlab::Runtime do
end
it "identifies itself" do
expect(subject.name).to eq(:puma)
expect(subject.identify).to eq(:puma)
expect(subject.puma?).to be(true)
end
......@@ -47,7 +47,7 @@ describe Gitlab::Runtime do
end
it "identifies itself" do
expect(subject.name).to eq(:unicorn)
expect(subject.identify).to eq(:unicorn)
expect(subject.unicorn?).to be(true)
end
......@@ -67,7 +67,7 @@ describe Gitlab::Runtime do
end
it "identifies itself" do
expect(subject.name).to eq(:sidekiq)
expect(subject.identify).to eq(:sidekiq)
expect(subject.sidekiq?).to be(true)
end
......@@ -86,7 +86,7 @@ describe Gitlab::Runtime do
end
it "identifies itself" do
expect(subject.name).to eq(:console)
expect(subject.identify).to eq(:console)
expect(subject.console?).to be(true)
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