Class: Command::Web
Instance Attribute Summary
Attributes inherited from CommandBase
#stream_io
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from CommandBase
#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids
Constructor Details
#initialize ⇒ Web
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/command/web.rb', line 13
def initialize
super("[options...]")
@opt.separator "\n \u30FBWEB\u30A2\u30D7\u30EA\u30B1\u30FC\u30B7\u30E7\u30F3\u7528\u30B5\u30FC\u30D0\u3092\u8D77\u52D5\u3057\u307E\u3059\n \u30FB\u5C0F\u8AAC\u306E\u7BA1\u7406\u53CA\u3073\u8A2D\u5B9A\u3092\u30D6\u30E9\u30A6\u30B6\u3067\u884C\u3046\u3053\u3068\u304C\u3067\u304D\u307E\u3059\n \u30FB--port \u3092\u6307\u5B9A\u3057\u306A\u3044\u5834\u5408\u3001\u30DD\u30FC\u30C8\u306F\u521D\u56DE\u8D77\u52D5\u6642\u306B\u30E9\u30F3\u30C0\u30E0\u3067\u8A2D\u5B9A\u3057\u307E\u3059\n(\u4EE5\u964D\u540C\u3058\u8A2D\u5B9A\u3092\u5F15\u304D\u7D99\u304E\u307E\u3059)\n \u30FB\u30B5\u30FC\u30D0\u8D77\u52D5\u5F8C\u306B\u30D6\u30E9\u30A6\u30B6\u3092\u7ACB\u3061\u4E0A\u3052\u307E\u3059\n \u30FB\u30B5\u30FC\u30D0\u306E\u505C\u6B62\u306F\u30B3\u30F3\u30BD\u30FC\u30EB\u3067 Ctrl+C \u3092\u5165\u529B\u3057\u307E\u3059\n\n Examples:\nnarou web # \u30B5\u30FC\u30D0\u8D77\u52D5(\u30DD\u30FC\u30C8\u306F\u30E9\u30F3\u30C0\u30E0\u3002\u30DD\u30FC\u30C8\u8A2D\u5B9A\u4FDD\u5B58)\nnarou web -p 4567 # \u30DD\u30FC\u30C84567\u3067\u8D77\u52D5(\u4FDD\u5B58\u306F\u3055\u308C\u306A\u3044)\n\n# \u5148\u306B\u6C7A\u3081\u3066\u304A\u304F\nnarou s server-port=8000\nnarou web # \u30DD\u30FC\u30C88000\u3067\u8D77\u52D5\n\n Options:\n EOS\n @opt.on(\"-p\", \"--port PORT\", Integer, \"\u8D77\u52D5\u3059\u308B\u30DD\u30FC\u30C8\u3092\u6307\u5B9A\") { |port|\n @options[\"port\"] = port\n }\n @opt.on(\"-n\", \"--no-browser\", \"\u8D77\u52D5\u6642\u306B\u30D6\u30E9\u30A6\u30B6\u306F\u958B\u304B\u306A\u3044\") {\n @options[\"no-browser\"] = true\n }\nend\n"
|
Class Method Details
.oneline_help ⇒ Object
9
10
11
|
# File 'lib/command/web.rb', line 9
def self.oneline_help
"WEBアプリケーション用サーバを起動します"
end
|
Instance Method Details
#boot ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/command/web.rb', line 117
def boot
require_relative "../web/all"
confirm_of_first
params = Narou::AppServer.create_address(@options["port"])
push_server = create_push_server(params)
Narou.web = true
Thread.abort_on_exception = true
address = "http://#{params[:host]}:#{params[:port]}/"
puts address
puts "サーバを止めるには Ctrl+C を入力"
puts
push_server.run
open_browser_when_server_boot(address)
send_rebooted_event_when_connection_recover(push_server)
$stdout = Narou::StreamingLogger.new(push_server)
$stdout2 = if Inventory.load["concurrency"]
Narou::StreamingLogger.new(push_server, $stdout2, target_console: "stdout2")
else
$stdout
end
ProgressBar.push_server = push_server
Narou::Worker.push_server = push_server
Narou::AppServer.push_server = push_server
Narou::WebWorker.run
Narou::AppServer.run!
push_server.quit
Narou::WebWorker.stop
Narou::Worker.stop
if Narou::AppServer.request_reboot?
exit Narou::EXIT_REQUEST_REBOOT
end
rescue Errno::EADDRINUSE => e
Helper.open_browser(address) unless @options["no-browser"]
STDOUT.puts "\#{e}\n\u30DD\u30FC\u30C8\u304C\u4F7F\u308F\u308C\u3066\u3044\u307E\u3059\u3002\u30B5\u30FC\u30D0\u304C\u3059\u3067\u306B\u7ACB\u3061\u4E0A\u304C\u3063\u3066\u3044\u308B\u304B\u3069\u3046\u304B\u78BA\u8A8D\u3057\u3066\u4E0B\u3055\u3044\u3002\n\u4ED6\u306E\u30A2\u30D7\u30EA\u30B1\u30FC\u30B7\u30E7\u30F3\u304C\u4F7F\u3063\u3066\u3044\u308B\u30DD\u30FC\u30C8\u3060\u3063\u305F\u5834\u5408\u3001\u30DD\u30FC\u30C8\u3092\u5909\u66F4\u3057\u3066\u4E0B\u3055\u3044\u3002\n\n\u30DD\u30FC\u30C8\u306E\u5909\u66F4\u65B9\u6CD5\n $ narou s server-port=5678\n EOS\n exit Narou::EXIT_ERROR_CODE\nend\n"
|
#confirm_of_first ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/command/web.rb', line 42
def confirm_of_first
setting = Inventory.load("server_setting", :global)
is_first = !setting["already-server-boot"]
if is_first
puts "\u521D\u3081\u3066\u30B5\u30FC\u30D0\u3092\u8D77\u52D5\u3057\u307E\u3059\u3002\u30D5\u30A1\u30A4\u30A2\u30A6\u30A9\u30FC\u30EB\u306E\u30A2\u30AF\u30BB\u30B9\u8A31\u53EF\u3092\u5C0B\u306D\u3089\u308C\u305F\u5834\u5408\u3001\u8A31\u53EF\u3092\u3057\u3066\u4E0B\u3055\u3044\u3002\n\u307E\u305F\u3001\u8D77\u52D5\u3057\u305F\u30B5\u30FC\u30D0\u3092\u6B62\u3081\u308B\u306B\u306F\u30B3\u30F3\u30BD\u30FC\u30EB\u4E0A\u3067 Ctrl+C \u3092\u5165\u529B\u3059\u308B\u304B\u3001\u30D6\u30E9\u30A6\u30B6\u4E0A\u3067\u300C\u8A2D\u5B9A(\u6B6F\u8ECA\u30DE\u30FC\u30AF)\u2192\u30B5\u30FC\u30D0\u3092\u30B7\u30E3\u30C3\u30C8\u30C0\u30A6\u30F3\u300D\u3092\u5B9F\u884C\u3057\u3066\u4E0B\u3055\u3044\u3002\n\n EOS\n if @options[\"no-browser\"]\n puts \"(\u4F55\u304B\u30AD\u30FC\u3092\u62BC\u3057\u3066\u4E0B\u3055\u3044)\"\n else\n puts \"(\u4F55\u304B\u30AD\u30FC\u3092\u62BC\u3057\u3066\u4E0B\u3055\u3044\u3002\u30B5\u30FC\u30D0\u8D77\u52D5\u5F8C\u30D6\u30E9\u30A6\u30B6\u304C\u7ACB\u3061\u4E0A\u304C\u308A\u307E\u3059)\"\n end\n $stdin.getch\n setting[\"already-server-boot\"] = true\n setting.save\n end\n is_first\nend\n"
|
#create_push_server(params) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/command/web.rb', line 63
def create_push_server(params)
host, port = params[:host], params[:port]
push_server = Narou::PushServer.instance
accepted_domains = (host == "0.0.0.0" ? "*" : host)
if accepted_domains != "*"
global_setting = Inventory.load("global_setting", :global)
addtional_accepted_domains = global_setting["server-ws-add-accepted-domains"]
if addtional_accepted_domains
accepted_domains = [
accepted_domains,
addtional_accepted_domains.split(",").map(&:strip)
].flatten
end
end
push_server.accepted_domains = accepted_domains
push_server.port = port + 1
push_server.host = host
push_server
end
|
#execute(argv) ⇒ 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
111
|
# File 'lib/command/web.rb', line 83
def execute(argv)
if argv.delete("--boot")
@rebooted = !!argv.delete("--reboot")
super
boot
else
argv << "--backtrace" if $display_backtrace
argv << "--no-color" if $disable_color
argv << "--boot"
argv_copy = argv.dup
kill_threads
begin
loop do
if $development
system(RbConfig.ruby, "-x", $0, "web", *argv)
else
system("narou", "web", *argv)
end
break unless $?.exitstatus == Narou::EXIT_REQUEST_REBOOT
argv = argv_copy.dup
argv.push("--no-browser", "--reboot")
end
rescue Interrupt => e
sleep 1
end
end
end
|
#kill_threads ⇒ Object
113
114
115
|
# File 'lib/command/web.rb', line 113
def kill_threads
Narou::Worker.stop
end
|
#open_browser_when_server_boot(address) ⇒ Object
164
165
166
167
168
169
170
|
# File 'lib/command/web.rb', line 164
def open_browser_when_server_boot(address)
return if @options["no-browser"]
Thread.new do
sleep 0.2 until Narou::AppServer.running?
Helper.open_browser(address)
end
end
|
#send_rebooted_event_when_connection_recover(push_server) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/command/web.rb', line 172
def send_rebooted_event_when_connection_recover(push_server)
return unless @rebooted
Thread.new do |th|
timeout = Time.now + 20
until push_server.connections.count != 0
sleep 0.2
th.kill if Time.now > timeout
end
puts "<yellow>再起動が完了しました。</yellow>".termcolor
push_server.send_all(:"server.rebooted")
end
end
|