Last active
July 18, 2018 14:08
-
-
Save nicksieger/6557dc529fbb9e153a5a8150dbbe5eb2 to your computer and use it in GitHub Desktop.
Propagate RequestStore contents across a Delayed Job boundary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RequestStorePlugin < ::Delayed::Plugin | |
callbacks do |lifecycle| | |
lifecycle.before(:enqueue) do |job| | |
obj = job.payload_object | |
obj.instance_variable_set(:@_request_store, RequestStore.store) | |
job.payload_object = obj # reserialize the job | |
end | |
lifecycle.before(:perform) do |worker, job, &block| | |
request_store = YAML.load(job.handler).instance_variable_get(:@_request_store) || {} | |
request_store.each do |k, v| | |
RequestStore.store[k] = v | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment