Module: Kernel
- Defined in:
- lib/ruby/kernel/bench.rb,
lib/ruby/kernel/daemonize.rb,
lib/ruby/kernel/safe_fork.rb,
lib/ruby/kernel/non_verbose.rb
Instance Method Summary collapse
- #bench(n = 1, &b) ⇒ Object
-
#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.
- #non_verbose ⇒ Object
-
#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.
Instance Method Details
#bench(n = 1, &b) ⇒ Object
10 11 12 13 14 |
# File 'lib/ruby/kernel/bench.rb', line 10 def bench(n=1,&b) start = Time.now n.to_i.times(&b) Time.now - start 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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby/kernel/daemonize.rb', line 18 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 |
#non_verbose ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/ruby/kernel/non_verbose.rb', line 10 def non_verbose old_verbose = $VERBOSE $VERBOSE = false yield ensure $VERBOSE = old_verbose 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
12 13 14 15 16 17 |
# File 'lib/ruby/kernel/safe_fork.rb', line 12 def safe_fork(delay=5) fork rescue Errno::EWOULDBLOCK sleep(delay) retry end |