Rails Sidekiq ERROR: can't find object
Rails Sidekiq ERROR: can't find object
我的 rails 4 应用程序中有几个 sidekiq worker,但无法弄清楚进程失败的原因。我将 User_id 作为字符串传递给工作人员,但它似乎无法找到用户。但是当我在我的控制台中搜索相同的 id 时,它会找到用户。我究竟做错了什么?这是我的代码。
Controller.rb
def update
@user = current_user
if @user.bill.update_attributes(bill_params)
RandomWorker.perform_async(@user.id.to_s)
redirect_to users_dashboard_path, :notice => "Payment Informaton Updated"
else
...
end
end
Randomworker.rb
def perform(user_id)
user_id = user_id["$oid"] unless user_id.is_a?(String)
user = User.find(user_id)
user.process.update_report
user.process.save
end
我的错误返回为
RandomWorker "575a..." Mongoid::Errors::DocumentNotFound: message: Document(s) not found for class User with id(s) 575a.... summary: When calling User.find with an id or array of ids, each parameter...
--编辑--
我的数据库配置文件如下所示:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 25
timeout: 5000
还有我的mongoid.yml
development:
# Configure available database clients. (required)
clients:
# Defines the default client. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: development_db
# Provides the hosts the default client can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
options:
我建议您包含 Sidekiq::Web
以在 Web 界面上查看排队的作业,以便您可以查看参数以及可能触发的失败。
然而,这是我前一段时间也遇到的错误,这让我很沮丧,因为我从错误通知程序 (Bugsnag) 收到了很多电子邮件。
我还没有找到最好的解决方案,但我想可能是数据库被锁定以供读取,或者某些记录在尝试使用它们之前未提交。
Sidekiq 的文档在您的模型使用中说,after_commit :perform_sidekiq_job, on: :create
。
但是,我并没有真正尝试过,因为我找到了另一种方法,我的新方法的另一面是我的作业执行时间很晚,有时大约 10 分钟后。
RandomWorker.perform_in(5.seconds, user_id)
Sidekiq::retry
选项不太可能失败。
我找到了解决办法。我不得不在 sidekiq 初始化程序中放置一个 monkeypatch 以将 user.id 视为 json。显然,mongoid 在 sidekiq 上遇到了这个问题,虽然我很难找到关于它的一些文档,但我在另一个不相关的问题中偶然发现了答案。 Have to_json return a mongoid as a string
我在初始化程序中添加了它,它似乎已经解决了问题
class BSON::ObjectId
def as_json
self.to_s
end
end
我的 rails 4 应用程序中有几个 sidekiq worker,但无法弄清楚进程失败的原因。我将 User_id 作为字符串传递给工作人员,但它似乎无法找到用户。但是当我在我的控制台中搜索相同的 id 时,它会找到用户。我究竟做错了什么?这是我的代码。
Controller.rb
def update
@user = current_user
if @user.bill.update_attributes(bill_params)
RandomWorker.perform_async(@user.id.to_s)
redirect_to users_dashboard_path, :notice => "Payment Informaton Updated"
else
...
end
end
Randomworker.rb
def perform(user_id)
user_id = user_id["$oid"] unless user_id.is_a?(String)
user = User.find(user_id)
user.process.update_report
user.process.save
end
我的错误返回为
RandomWorker "575a..." Mongoid::Errors::DocumentNotFound: message: Document(s) not found for class User with id(s) 575a.... summary: When calling User.find with an id or array of ids, each parameter...
--编辑--
我的数据库配置文件如下所示:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 25
timeout: 5000
还有我的mongoid.yml
development:
# Configure available database clients. (required)
clients:
# Defines the default client. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: development_db
# Provides the hosts the default client can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
options:
我建议您包含 Sidekiq::Web
以在 Web 界面上查看排队的作业,以便您可以查看参数以及可能触发的失败。
然而,这是我前一段时间也遇到的错误,这让我很沮丧,因为我从错误通知程序 (Bugsnag) 收到了很多电子邮件。
我还没有找到最好的解决方案,但我想可能是数据库被锁定以供读取,或者某些记录在尝试使用它们之前未提交。
Sidekiq 的文档在您的模型使用中说,after_commit :perform_sidekiq_job, on: :create
。
但是,我并没有真正尝试过,因为我找到了另一种方法,我的新方法的另一面是我的作业执行时间很晚,有时大约 10 分钟后。
RandomWorker.perform_in(5.seconds, user_id)
Sidekiq::retry
选项不太可能失败。
我找到了解决办法。我不得不在 sidekiq 初始化程序中放置一个 monkeypatch 以将 user.id 视为 json。显然,mongoid 在 sidekiq 上遇到了这个问题,虽然我很难找到关于它的一些文档,但我在另一个不相关的问题中偶然发现了答案。 Have to_json return a mongoid as a string
我在初始化程序中添加了它,它似乎已经解决了问题
class BSON::ObjectId
def as_json
self.to_s
end
end