Make sure that when serialing an just deserialized job arguments are there

When a job was just deserialized `arguments` is `nil` and the serialized
arguments are in the `@serialized_arguments` variable. If we try to
serialize this job again the arguments are going to be `nil` instead of
what was serialized.

The test we had was not checking this case because it was deserializing
the job in the same object that had the arguments.

To fix this, when the `@serialized_arguments` are present we return it
instead of the result of the `arguments` serialized.
上级 ac2bc004
......@@ -88,7 +88,7 @@ def serialize
"provider_job_id" => provider_job_id,
"queue_name" => queue_name,
"priority" => priority,
"arguments" => serialize_arguments(arguments),
"arguments" => serialize_arguments_if_needed(arguments),
"executions" => executions,
"locale" => I18n.locale.to_s,
"timezone" => Time.zone.try(:name)
......@@ -133,19 +133,31 @@ def deserialize(job_data)
end
private
def serialize_arguments_if_needed(arguments)
if arguments_serialized?
@serialized_arguments
else
serialize_arguments(arguments)
end
end
def deserialize_arguments_if_needed
if defined?(@serialized_arguments) && @serialized_arguments.present?
if arguments_serialized?
@arguments = deserialize_arguments(@serialized_arguments)
@serialized_arguments = nil
end
end
def serialize_arguments(serialized_args)
Arguments.serialize(serialized_args)
def serialize_arguments(arguments)
Arguments.serialize(arguments)
end
def deserialize_arguments(serialized_args)
Arguments.deserialize(serialized_args)
end
def arguments_serialized?
defined?(@serialized_arguments) && @serialized_arguments
end
end
end
......@@ -23,16 +23,16 @@ class JobSerializationTest < ActiveSupport::TestCase
test "serialize and deserialize are symmetric" do
# Round trip a job in memory only
h1 = HelloJob.new
h1.deserialize(h1.serialize)
h1 = HelloJob.new("Rafael")
h2 = HelloJob.deserialize(h1.serialize)
assert_equal h1.serialize, h2.serialize
# Now verify it's identical to a JSON round trip.
# We don't want any non-native JSON elements in the job hash,
# like symbols.
payload = JSON.dump(h1.serialize)
h2 = HelloJob.new
h2.deserialize(JSON.load(payload))
assert_equal h1.serialize, h2.serialize
payload = JSON.dump(h2.serialize)
h3 = HelloJob.deserialize(JSON.load(payload))
assert_equal h2.serialize, h3.serialize
end
test "deserialize sets locale" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册