Module: Kernel

Defined in:
lib/epitools/its.rb,
lib/epitools/minimal.rb,
lib/epitools/daemonize.rb

Instance Method Summary collapse

Instance Method Details

#ap(object, **options) ⇒ Object

Global AwesomePrint method (which triggers the loading of AwesomePrint the first time it’s called)



256
257
258
259
# File 'lib/epitools/minimal.rb', line 256

def ap(object, **options)
  AwesomePrint
  Kernel.ap(object, options)
end

#daemonize(chdir = nil, &on_sighup) ⇒ Object

This method causes the current running process to become a daemon All further printing is relied to the error.log FIXME doesn’t belong into Butler::Bot, rather into botcontrol



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epitools/daemonize.rb', line 5

def daemonize(chdir=nil, &on_sighup)
  srand # Split rand streams between spawning and daemonized process
  safe_fork and exit # Fork and exit from the parent

  # Detach from the controlling terminal
  raise "Can't detach from controlling terminal" unless sess_id = Process.setsid

  # Prevent the possibility of acquiring a controlling terminal
  trap('SIGHUP', 'IGNORE')
  exit if safe_fork

  # In daemon mode, a SIGHUP means termination
  trap('SIGHUP', &on_sighup)

  # We can't get the originally controlling terminals stdout/stdin anymore
  STDIN.reopen("/dev/null")
  STDOUT.reopen("/dev/null", "a")
  STDERR.reopen(STDOUT)

  Dir.chdir(chdir) if chdir
  File.umask 0033 # FIXME ask somebody knowledgable about a sensible value

  sess_id
end

#displayln(out = $stdout) ⇒ Object

Print “self” with a linefeed at the end



249
250
251
# File 'lib/epitools/minimal.rb', line 249

def displayln(out=$stdout)
  out.puts self
end

#run(*cmd) ⇒ Object Also known as: backtick

Executes a command and returns its output. (Like the backtick operator, but doesn’t require shell ecaping arguments.)



227
228
229
230
231
232
# File 'lib/epitools/minimal.rb', line 227

def run(*cmd)
  result = IO.popen(cmd) do |io|
    block_given? ? yield(io) : io.read
  end
  String === result && result.empty? ? nil : result
end

#run_with_stderr(*cmd) ⇒ Object Also known as: backtick_with_stderr

Same as Kernel#run, but includes stderr in the result.



238
239
240
241
242
243
# File 'lib/epitools/minimal.rb', line 238

def run_with_stderr(*cmd)
  result = IO.popen(cmd, err: [:child, :out]) do |io|
    block_given? ? yield(io) : io.read
  end
  String === result && result.empty? ? nil : result
end

#safe_fork(delay = 5) ⇒ Object

Try to fork if at all possible retrying every delay sec (5s default) if the maximum process limit for the system has been reached



32
33
34
35
36
37
# File 'lib/epitools/daemonize.rb', line 32

def safe_fork(delay=5)
  fork
rescue Errno::EWOULDBLOCK
  sleep(delay)
  retry
end