Class: BoltRb::App

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt_rb/app.rb

Overview

Main application class for running a Bolt app with Socket Mode

This class initializes the Slack clients, loads handlers, and processes incoming events through the middleware chain and router.

Examples:

Basic usage

BoltRb.configure do |config|
  config.bot_token = ENV['SLACK_BOT_TOKEN']
  config.app_token = ENV['SLACK_APP_TOKEN']
end

app = BoltRb::App.new
app.start

With custom handler paths

BoltRb.configure do |config|
  config.bot_token = ENV['SLACK_BOT_TOKEN']
  config.app_token = ENV['SLACK_APP_TOKEN']
  config.handler_paths = ['lib/handlers']
end

app = BoltRb::App.new
app.start

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Creates a new App instance

Initializes the Slack Web API client for making API calls, the worker pool for running handlers, and the Socket Mode client for receiving events.



50
51
52
53
54
55
56
57
# File 'lib/bolt_rb/app.rb', line 50

def initialize
  @config = BoltRb.configuration
  @router = BoltRb.router
  @client = Slack::Web::Client.new(token: config.bot_token)
  @worker_pool = WorkerPool.new(size: config.worker_threads, logger: BoltRb.logger)

  setup_socket_client
end

Instance Attribute Details

#clientSlack::Web::Client (readonly)

Returns The Slack Web API client.

Returns:

  • The Slack Web API client



31
32
33
# File 'lib/bolt_rb/app.rb', line 31

def client
  @client
end

#configConfiguration (readonly)

Returns The configuration instance.

Returns:

  • The configuration instance



37
38
39
# File 'lib/bolt_rb/app.rb', line 37

def config
  @config
end

#routerRouter (readonly)

Returns The router instance for dispatching events.

Returns:

  • The router instance for dispatching events



34
35
36
# File 'lib/bolt_rb/app.rb', line 34

def router
  @router
end

#socket_clientSocketMode::Client (readonly)

Returns The Socket Mode client instance.

Returns:

  • The Socket Mode client instance



40
41
42
# File 'lib/bolt_rb/app.rb', line 40

def socket_client
  @socket_client
end

#worker_poolWorkerPool (readonly)

Returns The pool that runs handlers off the socket thread.

Returns:

  • The pool that runs handlers off the socket thread



43
44
45
# File 'lib/bolt_rb/app.rb', line 43

def worker_pool
  @worker_pool
end

Instance Method Details

#process_event(payload) ⇒ void

This method returns an undefined value.

Processes an incoming event payload

Routes the event to matching handlers and executes them through the middleware chain. Errors in individual handlers are caught and passed to the configured error handler without stopping other handlers from executing.

Parameters:

  • The incoming Slack event payload



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bolt_rb/app.rb', line 109

def process_event(payload)
  handlers = router.route(payload)
  return if handlers.empty?

  context = build_context(payload)

  Middleware::Chain.new(config.middleware).call(context) do
    handlers.each do |handler_class|
      execute_handler(handler_class, context)
    end
  end
end

#request_stopvoid

This method returns an undefined value.

Requests a stop - safe to call from trap context

Use this in signal handlers instead of stop to avoid ThreadError from calling methods that use mutexes.



91
92
93
# File 'lib/bolt_rb/app.rb', line 91

def request_stop
  @socket_client.request_stop
end

#running?Boolean

Returns Whether the app is currently running.

Returns:

  • Whether the app is currently running



96
97
98
# File 'lib/bolt_rb/app.rb', line 96

def running?
  @socket_client.running?
end

#startvoid

This method returns an undefined value.

Starts the worker pool and the Socket Mode connection

Loads all handlers from configured paths and connects to Slack via Socket Mode to start receiving events. Blocks until the socket client stops, then drains the worker pool.



66
67
68
69
70
71
72
73
# File 'lib/bolt_rb/app.rb', line 66

def start
  load_handlers
  BoltRb.logger.info "[BoltRb] Starting app with #{config.worker_threads} worker threads..."
  @worker_pool.start
  @socket_client.start
ensure
  @worker_pool.shutdown
end

#stopvoid

This method returns an undefined value.

Stops the Socket Mode connection

Gracefully disconnects from Slack.



80
81
82
83
# File 'lib/bolt_rb/app.rb', line 80

def stop
  BoltRb.logger.info '[BoltRb] Stopping app...'
  @socket_client.stop
end