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



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_forkObject



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

#errfileObject



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

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

#load_envObject



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

#outfileObject



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

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

#pidfileObject



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

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

#portObject



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

def port
  File.basename(portfile).to_i
end

#procdirObject



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

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

#procnameObject



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

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

#redirectObject



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_loopObject



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, 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



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

def set_procname
  $0 = procname
end

#write_pidfileObject



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