ahoward (owner)

Revisions

gist: 214708 Download_button fork
public
Public Clone URL: git://gist.github.com/214708.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/env ruby
 
require 'rubygems'
require 'rbconfig'
require 'servolux'
require 'logger'
 
#
# to see the issue do:
#
# 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 {
  module Main
    def Main.factory(&block)
      Program.factory(&block)
    end
    def Main.run(*args, &block)
      klass = factory(&block).build(*args)
      klass.new(*args, &block).run()
    end
  end
  module Kernel
  private
    def Main(*args, &block)
      Main.run(*args, &block)
    end
  end
  module Main
    class Program
      def Program.factory(&block)
        Factory.new(&block)
      end
      class Factory
        def initialize(&block)
          @block = block || lambda{}
        end
        def to_proc
          @block
        end
        def build(*args, &block)
          factory = self
          program = Class.new(Object)
          def program.mode(mode, &block)
            module_eval(&block) if ARGV.first==mode
          end
          program.module_eval(&factory)
          program
        end
      end
    end
  end
}