Class: BoltRb::App
- Inherits:
-
Object
- Object
- BoltRb::App
- 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.
Instance Attribute Summary collapse
-
#client ⇒ Slack::Web::Client
readonly
The Slack Web API client.
-
#config ⇒ Configuration
readonly
The configuration instance.
-
#router ⇒ Router
readonly
The router instance for dispatching events.
-
#socket_client ⇒ SocketMode::Client
readonly
The Socket Mode client instance.
-
#worker_pool ⇒ WorkerPool
readonly
The pool that runs handlers off the socket thread.
Instance Method Summary collapse
-
#initialize ⇒ App
constructor
Creates a new App instance.
-
#process_event(payload) ⇒ void
Processes an incoming event payload.
-
#request_stop ⇒ void
Requests a stop - safe to call from trap context.
-
#running? ⇒ Boolean
Whether the app is currently running.
-
#start ⇒ void
Starts the worker pool and the Socket Mode connection.
-
#stop ⇒ void
Stops the Socket Mode connection.
Constructor Details
#initialize ⇒ App
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
#client ⇒ Slack::Web::Client (readonly)
Returns The Slack Web API client.
31 32 33 |
# File 'lib/bolt_rb/app.rb', line 31 def client @client end |
#config ⇒ Configuration (readonly)
Returns The configuration instance.
37 38 39 |
# File 'lib/bolt_rb/app.rb', line 37 def config @config end |
#router ⇒ Router (readonly)
Returns The router instance for dispatching events.
34 35 36 |
# File 'lib/bolt_rb/app.rb', line 34 def router @router end |
#socket_client ⇒ SocketMode::Client (readonly)
Returns The Socket Mode client instance.
40 41 42 |
# File 'lib/bolt_rb/app.rb', line 40 def socket_client @socket_client end |
#worker_pool ⇒ WorkerPool (readonly)
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.
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_stop ⇒ void
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.
96 97 98 |
# File 'lib/bolt_rb/app.rb', line 96 def running? @socket_client.running? end |
#start ⇒ void
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 |