Class: Dokuen::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/dokuen/wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#appObject (readonly)

Returns the value of attribute app.



7
8
9
# File 'lib/dokuen/wrapper.rb', line 7

def app
  @app
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/dokuen/wrapper.rb', line 7

def index
  @index
end

#portfileObject (readonly)

Returns the value of attribute portfile.



7
8
9
# File 'lib/dokuen/wrapper.rb', line 7

def portfile
  @portfile
end

#procObject (readonly)

Returns the value of attribute proc.



7
8
9
# File 'lib/dokuen/wrapper.rb', line 7

def proc
  @proc
end

#release_dirObject (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_forkObject



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

#errfileObject



40
41
42
# File 'lib/dokuen/wrapper.rb', line 40

def errfile
  File.join(procdir, "#{procname}.err")
end

#load_envObject



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

#outfileObject



36
37
38
# File 'lib/dokuen/wrapper.rb', line 36

def outfile
  File.join(procdir, "#{procname}.out")
end

#pidfileObject



32
33
34
# File 'lib/dokuen/wrapper.rb', line 32

def pidfile
  File.join(procdir, "#{procname}.pid")
end

#portObject



44
45
46
# File 'lib/dokuen/wrapper.rb', line 44

def port
  File.basename(portfile).to_i
end

#procdirObject



28
29
30
# File 'lib/dokuen/wrapper.rb', line 28

def procdir
  File.join(release_dir, '.dokuen')
end

#procnameObject



24
25
26
# File 'lib/dokuen/wrapper.rb', line 24

def procname
  "dokuen.#{app.name}.#{proc}.#{index}"
end

#redirectObject



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_loopObject



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, message = data.split(",", 2)
      log_file.puts("[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}][#{ps}] #{message}")
      log_file.flush
    end
  end
  Process.wait(process.pid)
  thread.exit
  reader.close
  writer.close
end

#set_procnameObject



71
72
73
# File 'lib/dokuen/wrapper.rb', line 71

def set_procname
  $0 = procname
end

#term_signalObject



89
90
91
# File 'lib/dokuen/wrapper.rb', line 89

def term_signal
  app.config.stop_signal rescue 15
end

#write_pidfileObject



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