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



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

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

#reopen_logsObject



87
88
89
90
91
92
# File 'lib/uc/server.rb', line 87

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

#restartObject



57
58
59
60
# File 'lib/uc/server.rb', line 57

def restart
  stop
  start
end

#rolling_restartObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/uc/server.rb', line 62

def rolling_restart
  init_once
  if config[:instances] == 0
    puts "0 instances specified: stopping"
    stop if server_status.running?
    return
  end
  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
37
38
39
40
# File 'lib/uc/server.rb', line 25

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

#statusObject



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

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

#stopObject



42
43
44
45
46
47
48
49
# File 'lib/uc/server.rb', line 42

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