Allow an "around" hook to be configured for the worker
Use case: We want propergate a correlation_id in the payload when an async listener is used.
# ApplicationController
around_action(&block) do
Event.bus.with_metadata(correlation_id: SecureRandom.uuid) do
block.call
end
end
Events published in the request will have the same corrleation_id, but async jobs will not since it happens in a different process.
To solve this I propose we allow an around hook to be configured which would receive the event data, the hook might look like this:
# config/initializer/wisper_next.rb
WisperNext::ActiveJob.configure do
around = ->(class_name, event_name, payload, &block) do
metadata = payload.is_a?(Hash) ? payload[:metadata] : {}
Event.bus.with_metadata(metadata) do
block.call
end
end
end