Events/Instrumentation
Introduce an events system that can provide an API to emit/handle them. The system is similar to ActiveSupport::Notifications
, and it should use wisper
gem.
The idea is to “broadcast” an event happening in the framework or in a Hanami project and let the other part of the system to react to it. There are several scenarios.
Pub/Sub
class WelcomeMailer
subscribe_to "user.signup"
end
class DripAdapter
subscribe_to "user.signup"
end
class Signup
include Hanami::Interactor
def call(data)
user = UserRepository.new.create(data[:user])
broadcast("user.signup", user: user)
end
end
When that event is emitted, WelcomeMailer
and DripAdapter
will receive a notification and react accordingly.
Async Execution
Continuing on the previous example, the broadcast is immediate, but the execution of the receiver logic can be async. We can send to a queue system (eg. sidekiq
) the name of the event and the payload and let it to schedule the execution.
The API could be
broadcast("user.signup", user: user).async(...)
Instrumentation
def render
broadcast('rendering.template') do
# ...
end
end
This reports the elapsed time. We can use it to log when a template was rendered.
This can be part of 1.x
series.