gpt4 book ai didi

ruby-on-rails - 如何将 current_user 传递给 Sidekiq 的 Worker

转载 作者:数据小太阳 更新时间:2023-10-29 07:47:39 27 4
gpt4 key购买 nike

我正在尝试将 current_userUser.find(1) 传递给工作模块,但在 sidekiq 的仪表板中出现错误 (localhost:3000/sidekiq/retries) :

NoMethodError: undefined method `supports' for "#":String

注意:我的关系还好,即:

u = User.find(1)
u.supports
#=> []

supports_controller.rb:

def create
@user = current_user
ProjectsWorker.perform_async(@user)

...

end

app/workers/projects_worker.rb:

class ProjectsWorker
include Sidekiq::Worker
def perform(user)
u = user
@support = u.supports.build(support_params)
end
end

重新启动我的 sidekiq 服务器没有任何区别。这是在我的开发机器上。

最佳答案

来自Sidekiq documentation :

The arguments you pass to perform_async must be composed of simple JSON datatypes: string, integer, float, boolean, null, array and hash. The Sidekiq client API uses JSON.dump to send the data to Redis. The Sidekiq server pulls that JSON data from Redis and uses JSON.load to convert the data back into Ruby types to pass to your perform method. Don't pass symbols or complex Ruby objects (like Date or Time!) as those will not survive the dump/load round trip correctly.

传递一个 id 而不是对象:

def create
ProjectsWorker.perform_async(current_user.id)
end

worker :

class ProjectsWorker
include Sidekiq::Worker
def perform(user_id)
u = User.find(user_id)
@support = u.supports.build(support_params)
end
end

关于ruby-on-rails - 如何将 current_user 传递给 Sidekiq 的 Worker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34380209/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com