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
- #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
49 50 51 52 53 54 55 56 57 |
# File 'lib/dokuen/wrapper.rb', line 49 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
76 77 78 79 80 81 82 |
# File 'lib/dokuen/wrapper.rb', line 76 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
41 42 43 |
# File 'lib/dokuen/wrapper.rb', line 41 def errfile File.join(procdir, "#{procname}.err") end |
#load_env ⇒ Object
84 85 86 87 88 |
# File 'lib/dokuen/wrapper.rb', line 84 def load_env app.env.each do |key, val| ENV[key] = val end end |
#outfile ⇒ Object
37 38 39 |
# File 'lib/dokuen/wrapper.rb', line 37 def outfile File.join(procdir, "#{procname}.out") end |
#pidfile ⇒ Object
33 34 35 |
# File 'lib/dokuen/wrapper.rb', line 33 def pidfile File.join(procdir, "#{procname}.pid") end |
#port ⇒ Object
45 46 47 |
# File 'lib/dokuen/wrapper.rb', line 45 def port File.basename(portfile).to_i end |
#procdir ⇒ Object
29 30 31 |
# File 'lib/dokuen/wrapper.rb', line 29 def procdir File.join(release_dir, '.dokuen') end |
#procname ⇒ Object
25 26 27 |
# File 'lib/dokuen/wrapper.rb', line 25 def procname "dokuen.#{app.name}.#{proc}.#{index}" end |
#redirect ⇒ Object
59 60 61 62 63 64 |
# File 'lib/dokuen/wrapper.rb', line 59 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 23 |
# File 'lib/dokuen/wrapper.rb', line 17 def run! return if daemonize! == nil load_env loop do run_loop end end |
#run_loop ⇒ Object
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 |
# File 'lib/dokuen/wrapper.rb', line 90 def run_loop 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") end Signal.trap("TERM") do if not process.kill(9) raise "Failed to kill process #{process.pid}" end File.delete(pidfile) File.delete("../../../../ports/#{port}") 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
72 73 74 |
# File 'lib/dokuen/wrapper.rb', line 72 def set_procname $0 = procname end |
#write_pidfile ⇒ Object
66 67 68 69 70 |
# File 'lib/dokuen/wrapper.rb', line 66 def write_pidfile File.open(pidfile, "w+") do |f| f.write(YAML.dump({'pid' => Process.pid, 'port' => port})) end end |