medusa-project / book-tracker

Medusa Book Tracker
0 stars 0 forks source link

Fixes 500 Internal Server Error when downloading book record(s) as Marcxml files #31

Closed gaurijo closed 11 months ago

gaurijo commented 11 months ago

This PR handles issue #29

Issue: When a user clicks on the Export link and chooses 'MARCXML' to download book record(s) a 500 Internal Server Error is thrown.

Screenshot of error on local server:

Screenshot 2023-10-18 at 2 49 09 PM

The error was traced to the index action inside the Books Controller, particularly the use of Enumerator class inside the format.xml docode block. For some reason using Enumerator as a response body made Rails expect the object(s) to respond to .each, which threw a NoMethodError. Swapping out the Enumerator for the use of traditional response headers fixed the error.

Screencast with fixed changes: (note: my local version doesn't have any book objects so it populates an empty list of book records, but still goes through the download process and should work the same with book objects present)

https://github.com/medusa-project/book-tracker/assets/103534307/dee848c4-22d4-4c23-ac8d-d3ec9ed2ee74

Rails server terminal output:

Screenshot 2023-10-23 at 3 00 33 PM
adolski commented 11 months ago

I think your diagnosis is correct, that ActionController can't handle returned Enumerators anymore, but this change adds an xml_data array (in memory) which accumulates strings in memory and then sends out a joined string. The problem is that, depending on what the user is exporting, there may be hundreds of thousands of strings that take up more memory than is available to our ECS containers. For example, a full XML export may consume > 3GB.

I have pushed a different, memory-safe fix for this as 725eec4.

I have also opened #32 and #33.