You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/handler_plugins.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -292,3 +292,61 @@ See the source code at `lib/hooks/utils/retry.rb` for more details on how `Retry
292
292
### `#failbot` and `#stats`
293
293
294
294
The `failbot` and `stats` methods are available in all handler plugins. They are used to report errors and send statistics, respectively. These are custom methods and you can learn more about them in the [Instrumentation Plugins](instrument_plugins.md) documentation.
295
+
296
+
### Extra Components
297
+
298
+
If you need even more flexibility, you can pass in extra components to your Hooks application when building it. These "extra components" are available globally and can be used in your handler plugins. Here is example that demonstrates using an extra component:
299
+
300
+
```ruby
301
+
# config.ru
302
+
303
+
# Define some class that you might want all your handlers to be able to call
304
+
class ExamplePublisher
305
+
def initialize
306
+
@published_messages = []
307
+
end
308
+
309
+
def call(data)
310
+
@published_messages << data
311
+
puts "Published: #{data.inspect}"
312
+
"Message published successfully"
313
+
end
314
+
315
+
def publish(data)
316
+
call(data)
317
+
end
318
+
319
+
def messages
320
+
@published_messages
321
+
end
322
+
end
323
+
324
+
# Create publisher instance
325
+
publisher = ExamplePublisher.new
326
+
327
+
# Create and run the hooks application with custom class
Now, in all handler plugins, you can access the `publisher` instance like so:
333
+
334
+
```ruby
335
+
# example file path: plugins/handlers/hello.rb
336
+
337
+
class Hello < Hooks::Plugins::Handlers::Base
338
+
def call(payload:, headers:, env:, config:)
339
+
# call the custom publisher instance
340
+
publisher.publish("hello")
341
+
342
+
{
343
+
status: "success",
344
+
handler: self.class.name,
345
+
timestamp: Time.now.utc.iso8601,
346
+
messages: publisher.messages,
347
+
}
348
+
end
349
+
end
350
+
```
351
+
352
+
It should be noted that any extra components you pass in like this should be thread-safe if you are running the Hooks server in a multi-threaded environment. This is because the Hooks server can handle multiple requests concurrently, and any shared state should be properly synchronized.
0 commit comments