I am building a rails 3.2 application which allows users to import contacts from CSV file. If any contact row in the CSV file contains validation errors, none of the contacts are imported and the errors are shown to the user in a CSV file. For example, if a contact CSV has 20 rows, with 4 and 15 rows having errors, none of the contact will be imported and user will be shown errors for both 4th and 15th row of the CSV.
I have done this functionality by reading each row at a time and ‘build‘ records in an array using ‘Contact.new‘. If errors are not present, I save all the contacts by iterating the array else show errors present.
If the number of rows increases to say 40, then it takes around 20 minutes to import the contacts which is bad. Is there any way to increase its performance?
UPDATE:
I had this app deployed on Heroku with Unicorn. If I import this CSV of 40 contacts, app crashes after 30 seconds, however all the contacts are imported. Following are the logs that caused app to crash:
2012-12-26T15:40:28+00:00 app[web.1]: E, [2012-12-26T15:40:27.233802 #2] ERROR -- : worker=0 PID:54 timeout (31s > 30s), killing
2012-12-26T15:40:29+00:00 app[web.1]: E, [2012-12-26T15:40:29.724725 #2] ERROR -- : reaped #<Process::Status: pid 54 SIGKILL (signal 9)> worker=0
2012-12-26T15:40:32+00:00 app[web.1]: Disconnected from ActiveRecord
2012-12-26T15:40:33+00:00 app[web.1]: Disconnected from Redis
Any idea on this?
Is that a typo or did you really say it takes 20 minutes to import 40 records from a CSV file? If that’s the case, there is something seriously wrong with your setup. Or your “contacts” must have a bazillion data fields and validations or something :/
As for heroku… heroku will timeout after 30 seconds. It does this to prevent runaway web requests. Their official answer is to move it to a background job and process it there.