Class: Dokuen::Wrapper
- Inherits:
-
Object
- Object
- Dokuen::Wrapper
- Defined in:
- lib/dokuen/wrapper.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#portfile ⇒ Object
readonly
Returns the value of attribute portfile.
-
#proc ⇒ Object
readonly
Returns the value of attribute proc.
-
#release_dir ⇒ Object
readonly
Returns the value of attribute release_dir.
Instance Method Summary collapse
- #daemonize! ⇒ Object
- #do_fork ⇒ Object
- #errfile ⇒ Object
-
#initialize(app, proc, index, portfile) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #load_env ⇒ Object
- #outfile ⇒ Object
- #pidfile ⇒ Object
- #port ⇒ Object
- #procdir ⇒ Object
- #procname ⇒ Object
- #redirect ⇒ Object
- #run! ⇒ Object
- #run_loop ⇒ Object
- #set_procname ⇒ Object
- #term_signal ⇒ Object
- #write_pidfile ⇒ Object
Constructor Details
#initialize(app, proc, index, portfile) ⇒ Wrapper
Returns a new instance of Wrapper.
9 10 11 12 13 14 15 |
# File 'lib/dokuen/wrapper.rb', line 9 def initialize(app, proc, index, portfile) @release_dir = Dir.getwd @app = app @proc = proc @index = index @portfile = portfile end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
7 8 9 |
# File 'lib/dokuen/wrapper.rb', line 7 def app @app end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
7 8 9 |
# File 'lib/dokuen/wrapper.rb', line 7 def index @index end |
#portfile ⇒ Object (readonly)
Returns the value of attribute portfile.
7 8 9 |
# File 'lib/dokuen/wrapper.rb', line 7 def portfile @portfile end |
#proc ⇒ Object (readonly)
Returns the value of attribute proc.
7 8 9 |
# File 'lib/dokuen/wrapper.rb', line 7 def proc @proc end |
#release_dir ⇒ Object (readonly)
Returns the value of attribute release_dir.
7 8 9 |
# File 'lib/dokuen/wrapper.rb', line 7 def release_dir @release_dir end |
Instance Method Details
#daemonize! ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/dokuen/wrapper.rb', line 48 def daemonize! pf = File.join(pidfile) raise "Process already running!" if File.exists?(pf) return unless do_fork.nil? write_pidfile set_procname redirect end |
#do_fork ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/dokuen/wrapper.rb', line 75 def do_fork raise 'first fork failed' if (pid = fork) == -1 exit unless pid.nil? Process.setsid raise 'second fork failed' if (pid = fork) == -1 exit unless pid.nil? end |
#errfile ⇒ Object
40 41 42 |
# File 'lib/dokuen/wrapper.rb', line 40 def errfile File.join(procdir, "#{procname}.err") end |
#load_env ⇒ Object
83 84 85 86 87 |
# File 'lib/dokuen/wrapper.rb', line 83 def load_env app.env.each do |key, val| ENV[key] = val end end |
#outfile ⇒ Object
36 37 38 |
# File 'lib/dokuen/wrapper.rb', line 36 def outfile File.join(procdir, "#{procname}.out") end |
#pidfile ⇒ Object
32 33 34 |
# File 'lib/dokuen/wrapper.rb', line 32 def pidfile File.join(procdir, "#{procname}.pid") end |
#port ⇒ Object
44 45 46 |
# File 'lib/dokuen/wrapper.rb', line 44 def port File.basename(portfile).to_i end |
#procdir ⇒ Object
28 29 30 |
# File 'lib/dokuen/wrapper.rb', line 28 def procdir File.join(release_dir, '.dokuen') end |
#procname ⇒ Object
24 25 26 |
# File 'lib/dokuen/wrapper.rb', line 24 def procname "dokuen.#{app.name}.#{proc}.#{index}" end |
#redirect ⇒ Object
58 59 60 61 62 63 |
# File 'lib/dokuen/wrapper.rb', line 58 def redirect $stdin.reopen("/dev/null") $stdout.sync = $stderr.sync = true $stdout.reopen(File.new(outfile, "a")) $stderr.reopen(File.new(errfile, "a")) end |
#run! ⇒ Object
17 18 19 20 21 22 |
# File 'lib/dokuen/wrapper.rb', line 17 def run! return if daemonize! == nil loop do run_loop end end |
#run_loop ⇒ Object
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 130 131 132 133 134 135 136 |
# File 'lib/dokuen/wrapper.rb', line 93 def run_loop load_env reader, writer = (IO.method(:pipe).arity == 0 ? IO.pipe : IO.pipe("BINARY")) procfile = Foreman::Procfile.new("Procfile") entry = procfile[proc] process = Foreman::Process.new(entry, index.to_i, port.to_i) log_path = "../../logs/#{procname}" log_file = File.open(log_path, 'a') Signal.trap("USR2") do process.kill(term_signal) end Signal.trap("TERM") do if not process.kill(term_signal) raise "Failed to kill process #{process.pid}" end File.delete(pidfile) File.delete("../../../../ports/#{port}") sleep 15 if process.alive? process.kill("KILL") end exit! 0 end process.run(writer, release_dir, {}) thread = Thread.new do loop do data = reader.gets next unless data ps, = data.split(",", 2) log_file.puts("[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}][#{ps}] #{}") log_file.flush end end Process.wait(process.pid) thread.exit reader.close writer.close end |
#set_procname ⇒ Object
71 72 73 |
# File 'lib/dokuen/wrapper.rb', line 71 def set_procname $0 = procname end |
#term_signal ⇒ Object
89 90 91 |
# File 'lib/dokuen/wrapper.rb', line 89 def term_signal app.config.stop_signal rescue 15 end |
#write_pidfile ⇒ Object
65 66 67 68 69 |
# File 'lib/dokuen/wrapper.rb', line 65 def write_pidfile File.open(pidfile, "w+") do |f| f.write(YAML.dump({'pid' => Process.pid, 'port' => port})) end end |