The problem I have right now is that my webapp needs to generate a spreadsheet for all accounts under a specific user, then email this spreadsheet as a report. Everything is stored in a Mongo databased, and accessed using Mongoid queries. The code is set up like this at the moment:
def some function
@user = #mongoid query
# use Axlsx to create a worksheet in a workbook
@user.get_accounts.each do |u|
#code to add account information to row
sheet.add_row(row)
end
#return sheet
end
This simple loop works great for most users. However, I have a few users with 100k+ accounts, and as you can imagine, this overloads the system memory and the spreadsheet is never created nor is sent.
I wanted to know if you guys had any suggestions to handle these users with this amount of accounts under them. The general architecture I was thinking of was to chunk it using Sidekiq workers of loops of more manageable size (for example, 100 workers each processing 1000 at a time, or 1000 workers processing 100). Would this be a proper usage of Sidekiq, and if so, is there any documentation I could read up on to figure this out? If not, is there another more efficient way?