Sidekiq - 获取另一个工作人员所做的数据库更改
Sidekiq - pick up DB changes made by another worker
在一个 Rails
应用程序中,我有 project
个,其中有很多 task
个。
task
可能有 predecessor
需要在任务开始前完成。
我使用 sidekiq
创建任务。
class ScheduleProjectJob < ApplicationJob
queue_as :default
def perform(project)
tasks = Array(project.tasks)
while !tasks.empty? do
task = tasks.shift
if task.without_predecessor? || task.predecessor_scheduled?
ScheduleTaskJob.perform_later(task)
else
tasks << task
end
end
end
我循环遍历 task
并安排一个任务,如果它没有前任,或者如果有,当前任已经安排时。
为了检查前任是否已安排,我在数据库中检查前任状态是否为 scheduled
(任务以 created
状态创建并在 scheduled
时更新为 scheduled
ScheduleTaskJob
.
结束
检查如下
Task.joins(:task_template).
where(%q(task_templates.dep_id = :dep AND
task_templates.tag = :tag AND
tasks.state = :state),
specification_id: task_template.dep_id,
tag: task_template.runs_after_tag,
state: 'scheduled').
count > 0
当我手动设置数据库并 运行 时,上面的查询似乎工作正常。
但是,当它在 ScheduleProjectJob
中 运行s 时,前置任务的状态总是报告为 created
即使我可以在数据库中看到记录中的值已更新为 scheduled
.
我在这里遗漏了什么吗?
结束
ActiveRecord 缓存查询结果,当您希望查询结果发生变化时,请使用以下内容包装您的查询:
ActiveRecord::Base.uncached do # or YourModel.uncached do
some_query.count
end
在一个 Rails
应用程序中,我有 project
个,其中有很多 task
个。
task
可能有 predecessor
需要在任务开始前完成。
我使用 sidekiq
创建任务。
class ScheduleProjectJob < ApplicationJob
queue_as :default
def perform(project)
tasks = Array(project.tasks)
while !tasks.empty? do
task = tasks.shift
if task.without_predecessor? || task.predecessor_scheduled?
ScheduleTaskJob.perform_later(task)
else
tasks << task
end
end
end
我循环遍历 task
并安排一个任务,如果它没有前任,或者如果有,当前任已经安排时。
为了检查前任是否已安排,我在数据库中检查前任状态是否为 scheduled
(任务以 created
状态创建并在 scheduled
时更新为 scheduled
ScheduleTaskJob
.
检查如下
Task.joins(:task_template).
where(%q(task_templates.dep_id = :dep AND
task_templates.tag = :tag AND
tasks.state = :state),
specification_id: task_template.dep_id,
tag: task_template.runs_after_tag,
state: 'scheduled').
count > 0
当我手动设置数据库并 运行 时,上面的查询似乎工作正常。
但是,当它在 ScheduleProjectJob
中 运行s 时,前置任务的状态总是报告为 created
即使我可以在数据库中看到记录中的值已更新为 scheduled
.
我在这里遗漏了什么吗?
结束
ActiveRecord 缓存查询结果,当您希望查询结果发生变化时,请使用以下内容包装您的查询:
ActiveRecord::Base.uncached do # or YourModel.uncached do
some_query.count
end