Class: DiscordRDA::RestartManager

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/core/restart_manager.rb

Constant Summary collapse

STATE_ENV =
'DISCORD_RDA_RESTART_STATE_PATH'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ RestartManager

Returns a new instance of RestartManager.



14
15
16
17
# File 'lib/discord_rda/core/restart_manager.rb', line 14

def initialize(logger:)
  @logger = logger
  @bot = nil
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/discord_rda/core/restart_manager.rb', line 12

def logger
  @logger
end

Instance Method Details

#attach(bot) ⇒ Object



19
20
21
# File 'lib/discord_rda/core/restart_manager.rb', line 19

def attach(bot)
  @bot = bot
end

#consume_boot_stateObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/discord_rda/core/restart_manager.rb', line 23

def consume_boot_state
  path = ENV.delete(STATE_ENV)
  return {} unless path && File.exist?(path)

  data = JSON.parse(File.read(path))
  File.delete(path)
  logger&.info('Loaded restart state', shards: (data['shards'] || []).length)
  data
rescue StandardError => e
  logger&.error('Failed to load restart state', error: e)
  {}
end

#restart!(command: nil, env: {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/discord_rda/core/restart_manager.rb', line 36

def restart!(command: nil, env: {})
  raise 'Restart manager is not attached to a bot instance' unless @bot

  state_path = write_restart_state
  restart_command = command || default_command

  logger&.info('Performing instant restart', command: restart_command)

  exec(
    {
      STATE_ENV => state_path,
      'DISCORD_RDA_RESTARTED_AT' => Time.now.utc.iso8601
    }.merge(env),
    RbConfig.ruby,
    *restart_command
  )
end