release: 发布版本 60176fa,RuboCop 零告警,网页新增版本号展示
This commit is contained in:
+28
-27
@@ -19,12 +19,12 @@ module ServerStatusDetector
|
||||
require 'timeout'
|
||||
|
||||
class TcpServer
|
||||
|
||||
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
||||
# @param host [String]
|
||||
# @param port [Integer]
|
||||
# @param timeout [Integer]
|
||||
# @return [Hash{Symbol=>Integer, Symbol=>Symbol, Symbol=>Float,nil}]
|
||||
def self.test_port(host = '127.0.0.1', port = 25565, timeout = 10)
|
||||
def self.test_port(host = '127.0.0.1', port = 25_565, timeout = 10)
|
||||
result = {
|
||||
port: port,
|
||||
status: :unknown,
|
||||
@@ -34,12 +34,11 @@ module ServerStatusDetector
|
||||
# 带错误处理的代码需要使用 begin...end
|
||||
begin
|
||||
Timeout.timeout(timeout) do # 此方法接收整数和代码块作为参数,并在超时时引发错误
|
||||
socket = TCPSocket.new(host,port)
|
||||
socket = TCPSocket.new(host, port)
|
||||
result[:status] = :open
|
||||
result[:response_time] = (Time.now - begin_time).round(4)
|
||||
socket.close
|
||||
end
|
||||
|
||||
rescue Timeout::Error
|
||||
result[:status] = :timeout
|
||||
rescue Errno::ECONNREFUSED
|
||||
@@ -48,66 +47,68 @@ module ServerStatusDetector
|
||||
result[:status] = :interrupted
|
||||
rescue Errno::ENETUNREACH, Errno::EHOSTUNREACH
|
||||
result[:status] = :unreachable
|
||||
rescue => error
|
||||
rescue StandardError => e
|
||||
result[:status] = :error
|
||||
result[:error] = error.message
|
||||
result[:error] = e.message
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
||||
end
|
||||
|
||||
class MinecraftServer < TcpServer # 继承为TcpServer编写的属性和方法
|
||||
|
||||
# 继承为TcpServer编写的属性和方法
|
||||
class MinecraftServer < TcpServer
|
||||
# rubocop:disable Metrics/MethodLength
|
||||
# @return [Hash{Symbol=>Boolean, Symbol=>Array<String>, Symbol=>String}]
|
||||
def self_diag
|
||||
res = test_port('127.0.0.1', 25_565)
|
||||
|
||||
res = test_port('127.0.0.1', 25565)
|
||||
|
||||
if res[:error] != nil
|
||||
unless res[:error].nil?
|
||||
message = "Unexpected ERROR when testing SMP by TCP: #{res[:status]},INFO: #{res[:error]}"
|
||||
puts message
|
||||
return {
|
||||
type: false,
|
||||
operation: %w[Record],
|
||||
message: message,
|
||||
message: message
|
||||
}
|
||||
end # 立刻错误处理是好习惯
|
||||
# 立刻错误处理是好习惯
|
||||
end
|
||||
|
||||
case res[:status]
|
||||
|
||||
when :open
|
||||
MethodExecutor.record("debug", "self_diag",
|
||||
MethodExecutor.record('debug', 'self_diag',
|
||||
"TCP check OK — #{res[:response_time]}s")
|
||||
return {
|
||||
type: true,
|
||||
{
|
||||
type: true
|
||||
}
|
||||
when :timeout
|
||||
MethodExecutor.record("warn", "self_diag",
|
||||
"TCP check timed out.")
|
||||
return {
|
||||
MethodExecutor.record('warn', 'self_diag',
|
||||
'TCP check timed out.')
|
||||
{
|
||||
type: false,
|
||||
operation: %w[Record],
|
||||
message: "We sent packet without response."
|
||||
message: 'We sent packet without response.'
|
||||
}
|
||||
when :closed
|
||||
MethodExecutor.record("warn", "self_diag",
|
||||
"TCP port closed — server is down.")
|
||||
return {
|
||||
MethodExecutor.record('warn', 'self_diag',
|
||||
'TCP port closed — server is down.')
|
||||
{
|
||||
type: false,
|
||||
operation: %w[Record ForceResetServerProcess], # 重启时注意要设置最大尝试次数
|
||||
message: "Server died for unknown reason, and will be restarted."
|
||||
message: 'Server died for unknown reason, and will be restarted.'
|
||||
}
|
||||
else
|
||||
MethodExecutor.record("warn", "self_diag",
|
||||
MethodExecutor.record('warn', 'self_diag',
|
||||
"TCP check returned unexpected status: #{res[:status]}.")
|
||||
return {
|
||||
{
|
||||
type: false,
|
||||
operation: %w[Record],
|
||||
message: "Unexpected status: #{res[:status]}."
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user