Sidekiq可以运行带有等待的循环并查看对数据库的更改吗?

问题描述

我有一个sidekiq工作人员,等待远程客户端所做的记录发生更改。类似于以下内容:

#myworker async process to wait for client to confirm status

 perform(myRecordID)
   sendClient(myRecordID)
   didClientAcknowledge = false
   while !didClientAcknowledge
    didClientAcknowledge = myRecords.find(myRecordID).status == :ACK_OK
    if didClientAcknowledge 
      break
    end
    # wait for client to perform an update on the record to confirm status
    sleep 5.seconds
   end
   Rails.logger.info("client got the message")
end
   

我的问题是,尽管我可以看到客户端实际上已经执行了确认并使用正确的状态更新(ACK_OK)更新了记录,但是我的sidekiq线程继续看到myRecord的旧状态。

我确定这里的逻辑存在缺陷,但是sidekiq进程似乎无法“看到”对数据库的更改...但是,如果使用Rails控制台,我可以看到客户端实际上已经更新了数据库符合预期...

谢谢!

编辑1

好吧,这是一个想法,而不是循环,我将在5秒内安排对工作人员的另一个呼叫...这是更新的代码:

perform(myRecordID,retry_count)
   retry_count -= 1
   if retry_count < 1
      return
   end
   sendClient(myRecordID)
   didClientAcknowledge = false
   if !didClientAcknowledge
    didClientAcknowledge = myRecords.find(myRecordID).status == :ACK_OK
    if didClientAcknowledge 
      Rails.logger.info("client got the message")
      return
    end
    # wait for client to perform an update on the record to confirm status
    myWorker.perform_in(5.seconds)
   end
   Rails.logger.info("client got the message")
end

这似乎可行,但还会进行更多测试。.一个挑战是需要重试计数,这意味着我需要在调用工人之间保持某种变量...

edit2 可能可以通过将时间传递给第一个调用,然后在调用下一个实例之前检查是否已超过超时来完成此操作……假设时间还没有停滞不前在异步调用中...

edit3 添加retry_count参数使我们可以控制该工作程序产生的次数...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)