Class: Talkshow::Daemon
- Inherits:
-
Object
- Object
- Talkshow::Daemon
- Defined in:
- lib/talkshow/daemon.rb
Instance Attribute Summary collapse
-
#port_requests ⇒ Object
Returns the value of attribute port_requests.
-
#processes ⇒ Object
Returns the value of attribute processes.
-
#thread ⇒ Object
Returns the value of attribute thread.
Instance Method Summary collapse
- #check_processes ⇒ Object
- #check_status(port) ⇒ Object
- #deal_with_port_requests ⇒ Object
-
#initialize ⇒ Daemon
constructor
Create a new Talkshow object to get going.
- #run ⇒ Object
- #spawn_process(port) ⇒ Object
- #start_server ⇒ Object
-
#stop_server ⇒ Object
Stop the webserver.
Constructor Details
Instance Attribute Details
#port_requests ⇒ Object
Returns the value of attribute port_requests.
14 15 16 |
# File 'lib/talkshow/daemon.rb', line 14 def port_requests @port_requests end |
#processes ⇒ Object
Returns the value of attribute processes.
15 16 17 |
# File 'lib/talkshow/daemon.rb', line 15 def processes @processes end |
#thread ⇒ Object
Returns the value of attribute thread.
13 14 15 |
# File 'lib/talkshow/daemon.rb', line 13 def thread @thread end |
Instance Method Details
#check_processes ⇒ Object
92 93 94 95 96 |
# File 'lib/talkshow/daemon.rb', line 92 def check_processes() @processes.each do |port, status| @processes[port] = check_status(port) end end |
#check_status(port) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/talkshow/daemon.rb', line 74 def check_status(port) uri = URI.parse("http://localhost:#{port}/status") begin response = Net::HTTP.get_response(uri) rescue status = 'dead' end if !status if response.code.to_i == 200 status = 'ok' else status = "dead #{response.code}" end end status end |
#deal_with_port_requests ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/talkshow/daemon.rb', line 49 def deal_with_port_requests begin port = @port_requests.pop(true) rescue port = nil end if port if @processes[port] puts "Port request -- checking aliveness" if check_status(port) == 'dead' @processes[port] = spawn_process(port) end else puts "New port request" @processes[port] = spawn_process(port) end end end |
#run ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/talkshow/daemon.rb', line 40 def run self.start_server loop do deal_with_port_requests sleep 5 check_processes end end |
#spawn_process(port) ⇒ Object
68 69 70 71 72 |
# File 'lib/talkshow/daemon.rb', line 68 def spawn_process(port) `TALKSHOW_PORT=#{port} bundle exec ./bin/talkshow_server.rb > logs/talkshow.#{port}.log 2>&1 &` sleep 5 'starting' end |
#start_server ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/talkshow/daemon.rb', line 25 def start_server @thread = Thread.new do Talkshow::WebControl.port_requests(@port_requests) Talkshow::WebControl.processes(@processes) Talkshow::WebControl.run! end p @thread sleep 10 end |
#stop_server ⇒ Object
Stop the webserver
36 37 38 |
# File 'lib/talkshow/daemon.rb', line 36 def stop_server @thread.exit end |