I wrote a thread as documented on https://guides.rubyonrails.org/threading_and_code_execution.html.
Then I tried to access a model dependency.
<code> recalculate_archived_emails = Thread.new(company) do |c|
execution_context = Rails.application.executor.run!
stores = c.active_stores
puts "all stores:"
stores.each do |s|
puts "#{s.name}" # so far it works
all_emails = s.emails # hangs without error
# lengthy computation doing something with the data
...
ensure
execution_context.complete! if execution_context
end
recalculate_archived_emails.join
end
</code>
<code> recalculate_archived_emails = Thread.new(company) do |c|
execution_context = Rails.application.executor.run!
stores = c.active_stores
puts "all stores:"
stores.each do |s|
puts "#{s.name}" # so far it works
all_emails = s.emails # hangs without error
# lengthy computation doing something with the data
...
ensure
execution_context.complete! if execution_context
end
recalculate_archived_emails.join
end
</code>
recalculate_archived_emails = Thread.new(company) do |c|
execution_context = Rails.application.executor.run!
stores = c.active_stores
puts "all stores:"
stores.each do |s|
puts "#{s.name}" # so far it works
all_emails = s.emails # hangs without error
# lengthy computation doing something with the data
...
ensure
execution_context.complete! if execution_context
end
recalculate_archived_emails.join
end
This thread completely blocks the whole app. It seems to be unable to access the has_many dependency s.emails
.
I am using MongoDB and Mongoid 6.4. Rails version 5.2, Ruby 2.7.6.
What can I do?