Optimization/large csv import and memory leakage job#5
Open
usama20 wants to merge 3 commits intoStaunchglobal:mainfrom
Open
Optimization/large csv import and memory leakage job#5usama20 wants to merge 3 commits intoStaunchglobal:mainfrom
usama20 wants to merge 3 commits intoStaunchglobal:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Task 1:
I have optimized csv reading task by using techniques
Solution 1: (When user does not wants to process csv file in the background)
I have first converted CSV import logic to a Service to follow single responsability pattern
I have then read csv file one by one instead of loading the whole file in memory
I have then make two arrays for storing validated blogs and invalidate blogs to keep track of record as well so that we can see which records was validated and which are not
I have then bulk inserted record in db using insert_all instead of inserting. records one by one to db for each batch size
I have then created a file in logs directory for storing invalidated records.
In this way I have solved the problem of memory overflow and too much queries.
Solution 2 : (When user is comfortable in running the file in the background)
I have stored the file temporarily in the tmp folder of our application
I have then created a background job and perform_later to run it in the background
This job will call the same service with a batch size and process the csv file
Summary:
I have taken care optimizing service to ensure memory overflow does not occur and to check if our file is processed successfully or not I have handled both valid and invalid record in CSV files.
==========================================================================================
Task 2:
I have done optimization in this task by focusing on 3 problems
I have also removed all extra variables like instance variables of @processed_blogs do avoid memory leakage and allow garbage collector of ruby to do its task.