Module: ScoutAgent::CoreExtensions::Object

Defined in:
lib/scout_agent/core_extensions.rb

Overview

Globally available extensions.

Instance Method Summary collapse

Instance Method Details

#at_my_exit(&exit_code) ⇒ Object

Sets up an at_exit() hander that will only run when this process exits. Child processes do not inherit exit_code.



127
128
129
130
131
132
# File 'lib/scout_agent/core_extensions.rb', line 127

def at_my_exit(&exit_code)
  me = ::Process.pid
  at_exit do
    exit_code.call if me == ::Process.pid
  end
end

#install_shutdown_handler(&handler) ⇒ Object

This method installs handler for TERM signals. If the agent is running as a daemon, the same code will be installed for INT signals as well. Otherwise, INT signals will be set to IGNORE.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/scout_agent/core_extensions.rb', line 139

def install_shutdown_handler(&handler)
  shutdown_signals = %w[TERM]
  if ScoutAgent::Plan.run_as_daemon?
    shutdown_signals << "INT"
  else
    trap("INT", "IGNORE")
  end
  shutdown_signals.each do |signal|
    trap(signal, &handler)
  end
end

#lock_gem_version(library, version) ⇒ Object

Locks the library at version, if gem() is available (a recent version of RubyGems is loaded).



102
103
104
105
106
107
108
# File 'lib/scout_agent/core_extensions.rb', line 102

def lock_gem_version(library, version)
  begin
    gem(library.tr("_", "-"), version)
  rescue NoMethodError  # gem() not available
    # do nothing:  RubyGems not loaded
  end
end

#no_warningsObject

This is a helper for those libraries that aren’t well enough behaved not to spit out warnings as they work. Warnings are disabled, your block is run, then warnings are restored to their previous level.



115
116
117
118
119
120
121
# File 'lib/scout_agent/core_extensions.rb', line 115

def no_warnings
  old_verbose = $VERBOSE
  $VERBOSE    = nil
  yield
ensure
  $VERBOSE = old_verbose
end

#require_lib_or_gem(name, version = nil) ⇒ Object

Tries to require a library with name. If that fails, attempts to load RubyGems and tries the require again. If that too fails, this process will exit with a message about the missing library.

Setting version will cause this method to try to lock the gem with that version, if loading via RubyGems. This helps prevent gem upgrades from causing issues with the agent, but it’s still possible to load directly from installed libraries if you need to bypass RubyGems for any reason.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/scout_agent/core_extensions.rb', line 65

def require_lib_or_gem(name, version = nil)
  # special case JSON, which became a standard library in Ruby 1.9
  if name == "json" and RUBY_VERSION >= "1.9"
    no_warnings { return require(name) }
  end
  
  lock_gem_version(name, version) if version
  if %w[amalgalite rest_client].include?(name) and RUBY_VERSION >= "1.9"
    no_warnings { require name }
  else
    require name
  end
rescue LoadError    # library not found
  begin
    require "rubygems"
    lock_gem_version(name, version) if version
    if %w[amalgalite rest_client].include?(name) and RUBY_VERSION >= "1.9"
      no_warnings { require name }
    else
      require name
    end
  rescue LoadError  # library not found
    v_str = version.to_s[/\d\.\d(?:\.\d)?/]
    v_str = "-v #{v_str}" if v_str
    abort <<-END_LOAD_ERROR.trim
    Unable to load the required '#{name}' library.  Try:

        sudo gem install #{name.tr('_', '-')} #{v_str}

    END_LOAD_ERROR
  end
end