I have two tables: assignments
and reports
. A report is made to keep stats on assignments. So, every time an assignment is created or updated, all reports must be indiscriminately updated. I do something like this:
Multi.new()
|> Multi.insert(:assignment, assignment_changeset(params))
|> Multi.update(:update_reports, update_all_reports())
Now, this takes such a huge toll to the server whenever I create or update an assignment because there are too many reports to update. I experienced timeouts because of this, so I disabled the updating of reports for now. Now, I was wondering if there was a better way to bulk update the reports without interrupting the assignment operations.
I'm sure you have good reason to update ALL reports, even if I think the implementation should be revised due to it's load on the server. The solution I'm giving you only address the timeout you spoke about.
You can wrap everything in a spawn :
spawn(fn ->
Multi.new()
|> Multi.insert(:assignment, assignment_changeset(params))
|> Multi.update(:update_reports, update_all_reports())
end)
It will created another process on which the create/update will take place, leaving the request process free to continue it's course. Keep in mind that only works if you do not need the results of the operation, otherwise you should consider a callback to get the results.