Skip to content

Instantly share code, notes, and snippets.

@nicksieger
Last active July 18, 2018 14:08
Show Gist options
  • Save nicksieger/6557dc529fbb9e153a5a8150dbbe5eb2 to your computer and use it in GitHub Desktop.
Save nicksieger/6557dc529fbb9e153a5a8150dbbe5eb2 to your computer and use it in GitHub Desktop.
Propagate RequestStore contents across a Delayed Job boundary
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