Class: Vagrant::Node::ServerAPI::ServerManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-node/server.rb

Constant Summary collapse

PIDFILENAME =
"server.pid"
DEFAULT_BIND_PORT =
3333
BIND_ADDRESS =
"0.0.0.0"
LOG_FILE =
"webrick.log"

Class Method Summary collapse

Class Method Details

.run(pid_path, data_path, env, port = DEFAULT_BIND_PORT) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vagrant-node/server.rb', line 19

def self.run(pid_path,data_path,env,port=DEFAULT_BIND_PORT)
	
	check_password(data_path)
	
	pid_file = File.join(pid_path,PIDFILENAME)
        	
	pid = fork do
	
		
		log_file = File.open (data_path + LOG_FILE).to_s, 'a+'
		
		log = WEBrick::Log.new log_file
		
		access_log = [
			[log_file, WEBrick::AccessLog::COMBINED_LOG_FORMAT],
		]
		
		port = DEFAULT_BIND_PORT if port < 1024
		
		options = {
			:Port => port, 
			:BindAddress => BIND_ADDRESS,
			:Logger => log, 
			:AccessLog => access_log
		}
		


		
		#begin
			server = WEBrick::HTTPServer.new(options)	
			
			server.mount "/", Rack::Handler::WEBrick,ServerAPI::API.new
			
			trap("INT") { server.shutdown }

			trap("USR1") { 
				
				puts "SERVER.RB RESTARTING SERVER"
				
				#Stopping server
				server.shutdown
				
				#Restarting server
				server = WEBrick::HTTPServer.new(options)
				server.mount "/", Rack::Handler::WEBrick,ServerAPI::API.new	
				server.start
			}

			PidFile.create(pid_file,Process.pid)						
			
			server.start
			
			
		# rescue Exception => e  
				# puts e.message 
		# end
	
	#Alternative running mode
	#				ServerAPI::API.run! :bind => '0.0.0.0', :port => 1234					
	end
	
end

.stop(pid_path, data_path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-node/server.rb', line 83

def self.stop(pid_path,data_path)
	
		
		# check_password(data_path,passwd);
		
		
		pid_file = File.join(pid_path,PIDFILENAME)
		
		if !File.exists?(pid_file)
			return
		end
		
		
		pid = File.read(pid_file).to_i
		
		#Regardless the pid belongs to a running process or not
		#first delete the file
		PidFile.delete(pid_file)
		#FIXME No sé por qué cuando se crea un environment
		#en el cliente, el servidor deja de atrapar la señal 
		#de INT
		#Process.kill('INT', pid)
		Process.kill('KILL', pid)
		#Process.kill 9, pid	
						
	 
						
end