如何重现/清理凌乱的 POST 参数以避免延迟作业的 YAML 序列化问题? [英] How to reproduce/sanitize messy POST params to avoid YAML serialization issues with delayed_job?
问题描述
今天,每次我启动 delayed_job
工作程序时,该进程都会立即无声地死掉.
Today, every time I was starting delayed_job
workers, the process would die immediately and silently.
经过一番调查(并了解 delayed_job
的前台模式),我终于发现问题是 delayed_job
序列化我的活动记录对象的方式正在触发YAML 加载部分的异常:
After some investigation (and finding out about the foreground mode of delayed_job
), I finally found out the problem was the way delayed_job
had serialized my active record object was triggering an exception on the YAML load part:
Psych::SyntaxError: (<unknown>): mapping keys are not allowed in this context at line 7 column 14
from /Users/mick/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/psych.rb:203:in `parse'
from /Users/mick/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/psych.rb:203:in `parse_stream'
from /Users/mick/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/psych.rb:151:in `parse'
from /Users/mick/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/psych.rb:127:in `load'
from /Users/mick/.rvm/gems/ruby-1.9.3-p448/gems/safe_yaml-0.9.7/lib/safe_yaml.rb:144:in `load_with_options'
from (irb):111
from /Users/mick/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.16/lib/rails/commands/console.rb:47:in `start'
from /Users/mick/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.16/lib/rails/commands/console.rb:8:in `start'
from /Users/mick/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.16/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
delayed_job
尝试时发生的情况:
YAML.load(my_job.handler)
在找到有问题的 Delayed::Backend::ActiveRecord::Job
实例后,puts my_job.handler
会显示:
After finding the problematic Delayed::Backend::ActiveRecord::Job
instance, a puts my_job.handler
would show:
object: !ruby/ActiveRecord:MyActiveRecord
attributes:
id: 7648
... some good stuff ...
my_field: ? bla bla bla
... some other good stuff ...
method_name: :mail
args: []
我最初认为这是一个编码问题,但我意识到?"角色是一个真正的?"字符(即值 63),而不是对无法识别的字符的误解.
I first thought it was an encoding issue but I realized the '?' character was a real '?' character (i.e. value 63) and not a misinterpretation of an unrecognized character.
然后我尝试使用 的
但 YAML 看起来像下面这样:my_field
值创建活动记录类的新实例?Totot
Then I tried to create a new instance of my active record class with a my_field
value of ? Totot
but then the YAML looked like the following:
object: !ruby/ActiveRecord:MyActiveRecord
attributes:
id: 7648
... some good stuff ...
my_field: ! '? bla bla bla'
... some other good stuff ...
method_name: :mail
args: []
并且 YAML.load(...)
运行成功.
所以我的问题是:
- 知道我的数据库中是如何弄乱 YAML 的吗?
- 知道我应该如何清理我的参数以避免这样的问题吗?
- 知道如何在单元测试中重现这一点吗?(确保我实际上是用第 2 步修复它)
推荐答案
@house9 建议的详细解释:
A detailed explanation of what @house9 is suggesting:
请勿执行以下操作(即使延迟作业的 git repo 建议作为示例)
Do NOT do the following (even though the delayed_job's git repo suggests is as example)
Notifier.delay.signup(@user)
class NotifierMailer < ActionMailer::Base
def signup(user)
end
end
因为这将尝试对 @user
进行 yaml 编码(这可能会导致问题)
as this will attempt to yaml encode @user
(which can cause issues)
但是,任何时候你有一个对象(尤其是一个 AR 对象)有一个 id,你应该在调用延迟作业时传递 id 并在以后检索它:
But rather, any time you have an object (especially an AR object) that has an id, you should pass the id when calling the delayed job and retrieve it later:
Notifier.delay.signup(@user.id)
class NotifierMailer < ActionMailer::Base
def signup(id)
@user = User.find_by_id(id)
end
end
这篇关于如何重现/清理凌乱的 POST 参数以避免延迟作业的 YAML 序列化问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!