Sidekiq 工作进程未正确更新数据库记录

Sidekiq worker processes not updating database records properly

我有一个 sidekiq worker 可以批量处理某些系列的任务。完成作业后,它会更新任务 success/failure 上的跟踪器 table。每个批次都有一个唯一标识符,该标识符将传递给工作脚本,工作进程查询 table 以获取此唯一 ID 并通过类似于以下的 activerecord 查询更新该特定行:

cpr = MODEL.find(tracker_unique_id)

cpr.update_attributes(:attempted => cpr[:attempted] + 1, :success => cpr[:success] + 1)

我注意到跟踪器只记录了 1 组任务 运行 即使我可以从 sidekiq 日志和另一个结果 table 中看到 x 个任务已完成 运行.

谁能帮我解决这个问题?

您的 update_attributes 调用存在竞争条件,因为您无法像那样安全地递增。多个线程会互相踩踏。您必须执行正确的 UPDATE SQL 语句。

update models set attempted = attempted + 1 where tracker_unique_id = ?