我想在 before_enqueue
回调中执行一些初始参数验证,以便在作业排队时存在问题时我可以提出问题。
这将使我能够立即向用户提供有关问题的反馈,而不是让作业尝试运行、失败,然后以某种方式向用户提供反馈。
我明白了in the API documentation作业对象被传递到回调中,但从 API 文档和 Rails Guides 中尚不清楚如何从该对象获取perform
参数。
最佳答案
通过在 REPL 中进行一些探索,我发现传递给回调的 job
对象有一个 arguments
访问器,它是一个参数数组。
对于普通参数,它们将按位置位于 arguments
数组中:
class TestJob < ApplicationJob
def perform(arg1, arg2); end
before_enqueue { |job| puts job.arguments }
end
TestJob.perform_later(:foo, 'bar')
# [:foo, "bar"]
对于命名参数,它们位于数组第一个位置的哈希中:
class TestJob < ApplicationJob
def perform(arg1:, arg2:); end
before_enqueue { |job| puts job.arguments }
end
TestJob.perform_later(arg1: :foo, arg2: 'bar')
# [{:arg1=>:foo, :arg2=>"bar"}]
关于ruby-on-rails - 是否可以在 before_enqueue 回调中访问作业的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62520518/