简体   繁体   English

Sidekiq的活动记录问题

[英]Active Record issue with Sidekiq

I have created a Query resource in rails. 我已经在Rails中创建了一个查询资源。 When a Query is POSTed, I create Query record and fire 2 task workers in the background using Sidekiq. 发布查询后,我将创建查询记录并使用Sidekiq在后台触发2个任务工作者。
My query model in addition to having the query parameters also contains status for each of the task workers. 我的查询模型除了具有查询参数外,还包含每个任务工作者的状态。 At the time of Query creation, the status for each task is set to "created". 创建查询时,每个任务的状态都设置为“已创建”。

 @query = Query.create(param1: parameter1, param2: parameter2, task1_status: "created", task2_status: "created") 

 Task1Worker.perform_async(@query.id)
 Task2Worker.perform_async(@query.id)

Task1 worker uses the query parameters to perform some processing and based on the results updates the task_status to either "completed" or "failed" Task1工作程序使用查询参数执行一些处理,并根据结果将task_status更新为“完成”或“失败”

 q = Query.find(query_id)
 status = process_task1()
 if status == SUCCESS
   q.task1_status = "completed"
 else
   q.task1_status = "failed"
 end   
 q.save!  

Task2 worker waits for Task1 to be completed before starting processing Task2工作人员在开始处理之前等待Task1完成

 q = Query.find(query_id)
 count = 1
 while q.task1_status == "created"
   if count == 3
     logger.error("Task1 state change timed out")
     return
   end
   sleep(5)
   q = Query.find(query_id)
 end   
 status = process_task2()
 if status == SUCCESS
   q.task2_status = "completed"
 else
   q.task2_status = "failed"
 end   
 q.save! 

Task 1 needs about 4 secs for the processing and sets the state to either "completed" or "failed". 任务1需要大约4秒钟的处理时间,并将状态设置为“完成”或“失败”。 But Task 2 never sees the update & times out after 10 secs, without processing. 但是任务2在没有处理的情况下10秒钟后再也看不到更新和超时。 Is there something I am missing here. 这里有我想念的东西吗? Is there a better way to do this? 有一个更好的方法吗?

But Task 2 never sees the update & times out after 10 secs, without processing 但是,任务2在没有处理的情况下10秒钟后再也看不到更新和超时

I'm suspecting caching problems ( Query.find(query_id) hitting cache and not database). 我怀疑缓存问题( Query.find(query_id)命中缓存而不是数据库)。 Try using q.reload instead of q = Query.find(query_id) on each retry. 尝试在每次重试时使用q.reload而不是q = Query.find(query_id)

Or, much better, schedule Task2 from Task1 when it completes, so that you don't have to tie your worker for long periods of time doing nothing . 或者,更好的是,在Task2完成时安排Task2的时间,这样您就不必长时间束缚您的工作人员什么也不做。

Try this 尝试这个

3.times do |count|
  logger.error("Task1 state change timed out") and return if count == 2
  if (q = Query.find(query_id)).task1_status != 'created'
    q.task2_status = process_task2() == SUCCESS ? 'completed' : 'failed'
    q.save!
  end
  sleep(5)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM