Reputation: 5986
In a Rails
app, I have project
s which have many task
s.
A task
may have a predecessor
that need to be completed before the task can start.
I use sidekiq
for creating tasks.
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
I loop through the task
s and schedule a task if it doesn't have a predecessor or, in case it has one, when the predecessor has been already scheduled.
To check if the predecessor has been scheduled, I check in the database if the predecessor state is scheduled
(tasks are created with created
state and updated to scheduled
at the end of ScheduleTaskJob
.
The check is as follows
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
The query above seems to work fine when I manually set the DB up and run it.
However, when it runs inside the ScheduleProjectJob
the state of the predecessor task is always reported as created
even if I can see in the DB the value in the record has been updated to scheduled
.
Am I missing anything here?
end
Upvotes: 0
Views: 92
Reputation: 18474
ActiveRecord caches query results, when you're expecting query result to change, wrap your query with:
ActiveRecord::Base.uncached do # or YourModel.uncached do
some_query.count
end
Upvotes: 1