Class: Codebot::Core

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

Overview

This class represents a Codebot bot.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Core

Creates a new bot from the supplied hash.

Parameters:

  • params (Hash) (defaults to: {})

    A hash with symbolic keys for initializing this instance. The only accepted keys are :config_file and :ipc_pipe. Any other keys are ignored.



28
29
30
31
32
33
# File 'lib/codebot/core.rb', line 28

def initialize(params = {})
  @config     = Config.new(self, params[:config_file])
  @irc_client = IRCClient.new(self)
  @web_server = WebServer.new(self)
  @ipc_server = IPCServer.new(self, params[:ipc_pipe])
end

Instance Attribute Details

#configConfig (readonly)

Returns the configuration used by this bot.

Returns:

  • (Config)

    the configuration used by this bot



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

def config
  @config
end

#ipc_serverIRCClient (readonly)

Returns the IPC server.

Returns:



21
22
23
# File 'lib/codebot/core.rb', line 21

def ipc_server
  @ipc_server
end

#irc_clientIRCClient (readonly)

Returns the IRC client.

Returns:



15
16
17
# File 'lib/codebot/core.rb', line 15

def irc_client
  @irc_client
end

#web_serverIRCClient (readonly)

Returns the web server.

Returns:



18
19
20
# File 'lib/codebot/core.rb', line 18

def web_server
  @web_server
end

Instance Method Details

#joinObject

Waits for this bot to stop. If any of the managed threads finish early, the bot is shut down immediately.



51
52
53
54
55
56
57
# File 'lib/codebot/core.rb', line 51

def join
  ipc = Thread.new { @ipc_server.join && stop }
  web = Thread.new { @web_server.join && stop }
  ipc.join
  web.join
  @irc_client.join
end

#migrate!Object

Requests that the running threads migrate to an updated configuration.



60
61
62
# File 'lib/codebot/core.rb', line 60

def migrate!
  @irc_client.migrate! unless @irc_client.nil?
end

#startObject

Starts this bot.



36
37
38
39
40
# File 'lib/codebot/core.rb', line 36

def start
  @irc_client.start
  @web_server.start
  @ipc_server.start
end

#stopObject

Stops this bot.



43
44
45
46
47
# File 'lib/codebot/core.rb', line 43

def stop
  @ipc_server.stop
  @web_server.stop
  @irc_client.stop
end

#trap_signalsObject

Sets traps for SIGINT and SIGTERM so Codebot can shut down gracefully.



65
66
67
68
69
70
71
72
73
74
# File 'lib/codebot/core.rb', line 65

def trap_signals
  shutdown = proc do |signal|
    STDERR.puts "\nReceived #{signal}, shutting down..."
    stop
    join
    exit 1
  end
  trap('INT')  { shutdown.call 'SIGINT'  }
  trap('TERM') { shutdown.call 'SIGTERM' }
end