[RUBY] Don't forget to close the file just because it's in a temporary folder

Conclusion

Even if you delete the temporary folder, the file will not be deleted completely and it will put pressure on the hard disk, so let's close it properly

Introduction: Temporary folder

In Ruby, you can easily create a temporary folder and the folder disappears when the block ends, so it is easy to handle as a working folder.

Dir.mktmpdir('test') do |dir|
  #Create and open files
end

Failure: Open (do not close) file

If you forget to close it just by opening it because it disappears in the temporary folder anyway ....

Dir.mktmpdir('test') do |dir|
  #File generation process
  f = File.open(path)
  #work
end

Problem: The file does not disappear

The tmp folder is empty, but the hard disk is getting more and more compressed ... Yes, on Linux it's not completely gone as long as the process remains. Unless you restart the Rails server or Delayed Job Woker and drop the process that handles the problem, it will continue to remain.

$ lsof | grep delete
bundle 23931 (Abbreviation) /tmp/test20200317-23931-cayd6g/test.pdf (deleted)

*) In the above example, process 23931 is grasping, so it is temporarily deleted.

Action: Close

Since the file is released, it will be permanently deleted when the temporary folder is deleted. If you do this you don't have to drop the whole process

Dir.mktmpdir('test') do |dir|
  #File generation process
  File.open(path) do
    #work
  end
end

reference

Recommended Posts

Don't forget to close the file just because it's in a temporary folder
Get the file name in a folder using glob
Output the key list included in S3 Bucket to a file
A memo organized by renaming the file names in the folder with python
Create a command to delete all temporary files generated in a specific folder
A shell script to make sure you don't forget the pipenv shell again
I made a program to check the size of a file in Python
Various ways to read the last line of a csv file in Python
How to drop Google Docs in one folder in a .txt file with python
[Python] Get the files in a folder with Python
How to get the files in the [Python] folder
Save the object to a file with pickle
How to read a file in a different directory
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
How to display the modification date of a file in C language up to nanoseconds
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
Output a binary dump in binary and revert to a binary file
2 ways to read all csv files in a folder
Error in rsync to mount.cifs shared folder: File exists (17)
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Sublime Text 2] Always execute a specific file in the project
Save the pystan model and results in a pickle file
A memorandum to register the library written in Hy in PyPI
[Python] Open the csv file in the folder specified by pandas
[python] Change the image file name to a serial number
It's hard to write a very simple algorithm in php
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
Process the files in the folder in order with a shell script
How to generate a query using the IN operator in Django
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
Batch convert all xlsx files in the folder to CSV files
I'll never forget how to write a shell script, don't forget! !!
To write a test in Go, first design the interface
In omegaconf, let's pass the direct parameter file to the function
Just try to receive a webhook in ngrok and python