Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use application tmp dir for product import #12649

Merged
merged 4 commits into from
Jul 11, 2024

Conversation

dacook
Copy link
Member

@dacook dacook commented Jul 9, 2024

Please use clockify code #7198 Back Office Uplift (Product) while reviewing or working on this task.

What? Why?

This came up because the product import doesn't work on my macOS. So I fixed it in order to continue with:

What should we test?

  • Product import works with a valid CSV file
  • shows error with invalid file

The file path was never going to be 'tmp/product_import', so I guess it never deleted the file.
Hmm but shouldn't we clean up on success too? I suppose the tmp dir will be cleaned up eventually, and maybe we want to keep them for debugging purposes.
In openfoodfoundation#3435, it was switched to the system tmp dir because it conventiently provided a method to generate a unique filename. However it didn't handle the case where the system provided a symlink (macOS).

I could have fixed that, but surely it's safer to use the Rails tmp directory.
So I changed back to that, using a tip from https://stackoverflow.com/questions/13787746/creating-a-thread-safe-temporary-file-name to generate a unique name. Perhaps we could use a larger string (eg uuid) or append a timestamp too, but I don't know that it's necessary. Instead, we can just check that the dir didn't exist first (as mentioned in the PR). Let's do that..
This method is not documented, but is used by Dir.mktmpdir.  https://apidock.com/ruby/Dir/Tmpname/create
@dacook dacook added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label Jul 9, 2024
@dacook dacook self-assigned this Jul 9, 2024
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one 👍

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

I have some potential ideas to clean this up but it's not that important and I don't think we should put more energy into this.

Comment on lines +136 to +138
def tmpdir_base
Rails.root.join('tmp', TMPDIR_PREFIX).to_s
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a constant which can then be re-used in specs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The knowledge of the path construction is in multiple places now as well. Instead of having a directory name prefix, we could just create a directory tmp/product_import/ and then have random filenames within. Would that make more sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would probably make more sense, but we would need to re-implement without Dir::Tmpname.create (which handles name collisions for us).
Hmm, I guess that's what make_tmpname mentioned in #3435 is for, but that method was abandoned on that PR. I forget why, but as you say I don't think we should put more energy in ;)

@drummer83 drummer83 self-assigned this Jul 11, 2024
@drummer83 drummer83 added the pr-staged-au staging.openfoodnetwork.org.au label Jul 11, 2024
@drummer83
Copy link
Contributor

Hi @dacook,
Thanks for this fix!

I have tested the following:

  • Invalid file type throws an error. ✔️
    image

  • Valid csv-file with invalid entries throws an error. ✔️
    image
    image

  • Valid csv-file with valid entries imports the products correctly. ✔️
    image

  • Import works for both admin interfaces (legacy and admin_v3). ✔️

  • Import works for both admin users and super admin users. ✔️

Conclusion

It's looking good! 💪
I couldn't spot any regressions or other problems. Ready to go, merging! 🎉 🚀

Thanks again, David!

@drummer83 drummer83 merged commit e097451 into openfoodfoundation:master Jul 11, 2024
55 checks passed
@drummer83 drummer83 removed the pr-staged-au staging.openfoodnetwork.org.au label Jul 11, 2024
@dacook
Copy link
Member Author

dacook commented Jul 14, 2024

Thanks Konrad!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants