ahoward (owner)

Revisions

gist: 214719 Download_button fork
public
Public Clone URL: git://gist.github.com/214719.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
97
#! /usr/bin/env ruby
 
require 'rubygems'
require 'rbconfig'
require 'servolux'
require 'logger'
 
# HANG=LOW ./main_servolux.rb daemon_stop; HANG=LOW ./main_servolux.rb popen
#
STDOUT.dup if ENV['HANG']=='LOW'
 
THIS_FILE = File.expand_path(__FILE__)
RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
PID_FILE = File.expand_path('dummy.pid')
 
LOG = Logger.new($stderr)
LOG.level = Logger::DEBUG
 
SERVER = Servolux::Server.new('dummy', :logger => LOG, :pid_file => PID_FILE, :noclose => true) do
  sleep 1 # pretend to work
end
 
Main do
  mode 'backquotes' do
    def run
      puts "Starting daemon with backquotes"
      `#{RUBY} #{THIS_FILE} daemon_start`
      puts "Finished starting daemon"
    end
  end
  mode 'system' do
    def run
      puts "Starting daemong with system()"
      system("#{RUBY} #{THIS_FILE} daemon_start")
      puts "Finished starting daemon"
    end
  end
  mode 'popen' do
    def run
      puts "Starting daemon with popen()"
      IO.popen("#{RUBY} #{THIS_FILE} daemon_start") do |io|
        puts "Reading from STDOUT"
        io.read
        puts "Finished reading"
      end
      puts "Finished starting daemon"
    end
  end
  mode 'open4' do
    def run
      require 'open4'
      puts "Starting daemon with Open4"
      Open4.popen4("#{RUBY} #{THIS_FILE} daemon_start") do |pid, stdin, stdout, stderr|
        stdin.close
        Process.waitpid(pid)
        puts "Reading from STDOUT"
        stdout.read
        puts "Finished reading"
      end
      puts "Finished starting daemon"
    end
  end
  mode 'server_start' do
    def run
      SERVER.startup
    end
  end
  mode 'daemon_start' do
    def run
    open('out','w'){ puts :daemon_start }
      daemon = Servolux::Daemon.new(:server => SERVER)
      daemon.startup
    end
  end
  mode 'daemon_stop' do
    def run
      daemon = Servolux::Daemon.new(:server => SERVER)
      daemon.shutdown
    end
  end
end
 
BEGIN {
  Mode = ARGV.first
 
  def Main(&block)
    block.call
  end
 
  def mode(name, &block)
    if Mode==name
      block.call
      run()
    end
  end
}