Class: GitlabJanitor::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_janitor/utils.rb

Constant Summary collapse

TERM_SIGNALS =
%w[INT TERM].freeze

Class Method Summary collapse

Class Method Details

.exit!Object



15
16
17
# File 'lib/gitlab_janitor/utils.rb', line 15

def exit!
  $exiting = true
end

.exiting?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/gitlab_janitor/utils.rb', line 11

def exiting?
  $exiting
end

.initialize_signal_handlersObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gitlab_janitor/utils.rb', line 44

def initialize_signal_handlers
  TERM_SIGNALS.each do |sig|
    trap(sig) do |*_args|
      TERM_SIGNALS.each do |s|
        trap(s) do |*_args|
          warn 'Forcing exit!'
          Kernel.exit!(1)
        end
      end

      STDOUT.puts "Caught signal[#{sig}]: exiting...."
      GitlabJanitor::Util.exit!
    end
  end
end

.loggerObject



19
20
21
22
23
24
25
26
# File 'lib/gitlab_janitor/utils.rb', line 19

def logger
  $logger ||= ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)).tap do |logger|
    logger.level = ENV.fetch('LOG_LEVEL', Logger::INFO)
    formatter = Logger::Formatter.new
    formatter.extend ActiveSupport::TaggedLogging::Formatter
    logger.formatter = formatter
  end
end

.setupObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab_janitor/utils.rb', line 28

def setup
  STDOUT.sync = true
  STDERR.sync = true

  initialize_signal_handlers

  String.class_eval do
    def to_bool
      return true   if self == true   || self =~ (/(true|t|yes|y|1)$/i)
      return false  if self == false  || self.blank? || self =~ (/(false|f|no|n|0)$/i)

      raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
    end
  end
end