Merge branch 'pl-serialize-ac-parameters' into 'master'
Make `ActionContorller::Parameters` serializable for sidekiq jobs See merge request gitlab-org/gitlab-ce!24864
This commit is contained in:
commit
55cb4bc9ca
|
@ -23,7 +23,7 @@ module MailScheduler
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.perform_async(*args)
|
def self.perform_async(*args)
|
||||||
super(*ActiveJob::Arguments.serialize(args))
|
super(*Arguments.serialize(args))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -38,5 +38,34 @@ module MailScheduler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Permit ActionController::Parameters for serializable Hash
|
||||||
|
#
|
||||||
|
# Port of
|
||||||
|
# https://github.com/rails/rails/commit/945fdd76925c9f615bf016717c4c8db2b2955357#diff-fc90ec41ef75be8b2259526fe1a8b663
|
||||||
|
module Arguments
|
||||||
|
include ActiveJob::Arguments
|
||||||
|
extend self
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def serialize_argument(argument)
|
||||||
|
case argument
|
||||||
|
when -> (arg) { arg.respond_to?(:permitted?) }
|
||||||
|
serialize_hash(argument.to_h).tap do |result|
|
||||||
|
result[WITH_INDIFFERENT_ACCESS_KEY] = serialize_argument(true)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Make sure we remove this patch starting with Rails 6.0.
|
||||||
|
if Rails.version.start_with?('6.0')
|
||||||
|
raise <<~MSG
|
||||||
|
Please remove the patch `Arguments` module and use `ActiveJob::Arguments` again.
|
||||||
|
MSG
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Make `ActionController::Parameters` serializable for sidekiq jobs
|
||||||
|
merge_request: 24864
|
||||||
|
author:
|
||||||
|
type: fixed
|
|
@ -9,6 +9,10 @@ describe MailScheduler::NotificationServiceWorker do
|
||||||
ActiveJob::Arguments.serialize(args)
|
ActiveJob::Arguments.serialize(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def deserialize(args)
|
||||||
|
ActiveJob::Arguments.deserialize(args)
|
||||||
|
end
|
||||||
|
|
||||||
describe '#perform' do
|
describe '#perform' do
|
||||||
it 'deserializes arguments from global IDs' do
|
it 'deserializes arguments from global IDs' do
|
||||||
expect(worker.notification_service).to receive(method).with(key)
|
expect(worker.notification_service).to receive(method).with(key)
|
||||||
|
@ -42,13 +46,48 @@ describe MailScheduler::NotificationServiceWorker do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.perform_async' do
|
describe '.perform_async', :sidekiq do
|
||||||
it 'serializes arguments as global IDs when scheduling' do
|
around do |example|
|
||||||
Sidekiq::Testing.fake! do
|
Sidekiq::Testing.fake! { example.run }
|
||||||
described_class.perform_async(method, key)
|
end
|
||||||
|
|
||||||
expect(described_class.jobs.count).to eq(1)
|
it 'serializes arguments as global IDs when scheduling' do
|
||||||
expect(described_class.jobs.first).to include('args' => [method, *serialize(key)])
|
described_class.perform_async(method, key)
|
||||||
|
|
||||||
|
expect(described_class.jobs.count).to eq(1)
|
||||||
|
expect(described_class.jobs.first).to include('args' => [method, *serialize(key)])
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with ActiveController::Parameters' do
|
||||||
|
let(:parameters) { ActionController::Parameters.new(hash) }
|
||||||
|
|
||||||
|
let(:hash) do
|
||||||
|
{
|
||||||
|
"nested" => {
|
||||||
|
"hash" => true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when permitted' do
|
||||||
|
before do
|
||||||
|
parameters.permit!
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'serializes as a serializable Hash' do
|
||||||
|
described_class.perform_async(method, parameters)
|
||||||
|
|
||||||
|
expect(described_class.jobs.count).to eq(1)
|
||||||
|
expect(deserialize(described_class.jobs.first['args']))
|
||||||
|
.to eq([method, hash])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when not permitted' do
|
||||||
|
it 'fails to serialize' do
|
||||||
|
expect { described_class.perform_async(method, parameters) }
|
||||||
|
.to raise_error(ActionController::UnfilteredParameters)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue