Class: Uc::Server

Inherits:
Object
  • Object
show all
Includes:
Logger, ShellHelper
Defined in:
lib/uc/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#event_queue, event_stream, #event_stream, logger, #logger, stderr, #stderr

Methods included from ShellHelper

#cmd, #kill, #process_running?

Constructor Details

#initialize(app_dir, rails_env: "production", debug: false) ⇒ Server

Returns a new instance of Server.



19
20
21
22
23
# File 'lib/uc/server.rb', line 19

def initialize(app_dir, rails_env: "production", debug: false)
  @app_dir = app_dir
  @rails_env = rails_env
  @debug = debug
end

Instance Attribute Details

#app_dirObject (readonly)

Returns the value of attribute app_dir.



16
17
18
# File 'lib/uc/server.rb', line 16

def app_dir
  @app_dir
end

#rails_envObject (readonly)

Returns the value of attribute rails_env.



16
17
18
# File 'lib/uc/server.rb', line 16

def rails_env
  @rails_env
end

#use_pidObject

Returns the value of attribute use_pid.



17
18
19
# File 'lib/uc/server.rb', line 17

def use_pid
  @use_pid
end

Instance Method Details



70
71
72
73
74
75
76
# File 'lib/uc/server.rb', line 70

def print_config
  init_once
  config.to_h.each do |k,v| 
    v = %{ "#{v}" } if not v.is_a? Numeric
    puts "#{k} #{v}" 
  end
end

#reopen_logsObject



78
79
80
81
82
83
# File 'lib/uc/server.rb', line 78

def reopen_logs
  init_once
  return if not server_status.running?
  Process.kill(:USR1 , server_status.pid)
  puts "reopened logs"
end

#restartObject



53
54
55
56
# File 'lib/uc/server.rb', line 53

def restart
  stop
  start
end

#rolling_restartObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/uc/server.rb', line 58

def rolling_restart
  init_once
  uconfig.generate_once
  if not server_status.running?
    start
    return
  end
  event_stream.expect :fin do
    Process.kill("USR2", server_status.pid)
  end
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uc/server.rb', line 25

def start 
  init_once
  if server_status.running?
    puts server_status
    return
  end
  ENV["UNICORN_APP_DIR"] = config.app_dir
  event_stream.expect_in_background :fin do
    cmd %{unicorn -c #{uconfig.path} -D -E #{rails_env} }, return_output: false,
      error_msg: "error starting unicorn"
  end
end

#statusObject



47
48
49
50
51
# File 'lib/uc/server.rb', line 47

def status
  paths.validate_required
  Dir.chdir app_dir
  puts server_status
end

#stopObject



38
39
40
41
42
43
44
45
# File 'lib/uc/server.rb', line 38

def stop
  init_once
  if server_status.stopped?
    logger.info "unicorn not running"
    return
  end
  kill(server_status.pid, 30)
end