Class: Sv::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sv/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_dir) ⇒ Server



12
13
14
# File 'lib/sv/server.rb', line 12

def initialize(app_dir)
  @app_dir = app_dir
end

Instance Attribute Details

#app_dirObject (readonly)

Returns the value of attribute app_dir.



10
11
12
# File 'lib/sv/server.rb', line 10

def app_dir
  @app_dir
end

Instance Method Details

#health_checkObject

Raises:



68
69
70
71
72
73
74
# File 'lib/sv/server.rb', line 68

def health_check
  if instances == 0
    return
  end
  raise Error, "server not running" if not server_status.running?
  api.health_check
end


91
92
93
# File 'lib/sv/server.rb', line 91

def print_config
  puts File.read(supervisor_config.generated_path)
end

#reopen_logsObject



63
64
65
66
# File 'lib/sv/server.rb', line 63

def reopen_logs
  return if not server_status.running?
  api.reopen_logs
end

#required_pathsObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sv/server.rb', line 95

def required_paths
  paths = [
    "#{app_dir}/tmp/sockets/",
    "#{app_dir}/tmp/pids/",
    "#{app_dir}/log/"
  ]
  paths.each do |path|
    path = Pathname.new(path)
    raise ::Sv::Error, "required path missing #{path}" if not path.exist?
  end
end

#restart(auto_start: false, wait: false) ⇒ Object



42
43
44
45
# File 'lib/sv/server.rb', line 42

def restart(auto_start: false, wait: false)
  stop if server_status.running?
  start auto_start: auto_start, wait: wait
end

#rolling_restartObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sv/server.rb', line 47

def rolling_restart
  init_once
  if not server_status.running?
    start auto_start: true, wait: true
    return
  end
  if instances == 0
    puts "stopping supervisord: 0 instances"
    stop
    return
  end
  supervisor_config.generated_path
  rolling_restart = RollingRestart.new(config.jobs, api)
  rolling_restart.run
end

#start(auto_start: false, wait: false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sv/server.rb', line 16

def start(auto_start: false, wait: false)
  init_once
  if instances == 0
    puts "skipping supervisord start: 0 instances"
    return
  end
  if server_status.running?
    puts "supervisor already running with pid #{api.pid}"
    return
  end
  system "supervisord -c #{supervisor_config.generated_path}"
  puts "Started"
  api.start_jobs(wait: wait) if auto_start
end

#start_jobsObject



76
77
78
79
# File 'lib/sv/server.rb', line 76

def start_jobs
  api.start_jobs
  puts api.errors
end

#statusObject



81
82
83
84
85
86
87
88
89
# File 'lib/sv/server.rb', line 81

def status
 if server_status.running?
   api.print_status
   puts "-"* 20
   puts "active_groups: #{api.active_groups.size}"
 else
   puts "Stopped"
 end
end

#stopObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/sv/server.rb', line 31

def stop
  init_once
  if server_status.stopped?
    puts "Stopped"
    return
  end
  api.shutdown
  server_status.wait_until_stopped
  puts "Stopped"
end