Class: ESM::Websocket::Request::Overseer

Inherits:
Object
  • Object
show all
Defined in:
lib/esm/websocket/request/overseer.rb

Class Method Summary collapse

Class Method Details

.check_connectionsObject

Checks all the connections



29
30
31
32
33
# File 'lib/esm/websocket/request/overseer.rb', line 29

def self.check_connections
  ESM::Websocket.connections.each do |server_id, connection|
    process_connection(server_id, connection)
  end
end

.check_request(id, request) ⇒ Object

Finally, check a single request, and remove it if it's timed out



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
# File 'lib/esm/websocket/request/overseer.rb', line 46

def self.check_request(id, request)
  return if !request.timed_out?

  # Remove the request
  @connection.remove_request(id)

  # Don't warn on our internal messages
  return if request.user.nil?

  embed =
    ESM::Embed.build do |e|
      e.description = I18n.t(
        "request_timed_out",
        command_message: request.command.usage,
        server_id: @connection.server.server_id,
        user: request.user.mention
      )

      e.color = :red
    end

  # Let the user know
  request.command.reply(embed)
rescue => e
  ESM.logger.error("#{self.class}##{__method__}") do
    ESM::JSON.pretty_generate(
      server_id: @connection&.server&.server_id,
      command_name: request&.command&.name,
      request: request&.to_h,
      message: e.message,
      backtrace: e.backtrace
    )
  end
end

.dieObject

Stops the EM thread



21
22
23
24
25
# File 'lib/esm/websocket/request/overseer.rb', line 21

def self.die
  return if @thread.nil?

  Thread.kill(@thread)
end

.process_connection(_server_id, connection) ⇒ Object

Checks a connection's requests



37
38
39
40
41
42
# File 'lib/esm/websocket/request/overseer.rb', line 37

def self.process_connection(_server_id, connection)
  @connection = connection
  @connection.requests.each do |id, request|
    check_request(id, request)
  end
end

.watch!Object

Starts a thread that loops over all connections requests and times them out if they are taking too long



9
10
11
12
13
14
15
16
17
18
# File 'lib/esm/websocket/request/overseer.rb', line 9

def self.watch!
  check_every = ESM.config.websocket_request_overseer.check_every

  @thread = Thread.new do
    loop do
      check_connections
      sleep(check_every)
    end
  end
end