Do not error when trying to delete nonexistent temp file #2434
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.
Requirements
Please take note of our contributing guidelines: https://docs.laravel-excel.com/3.1/getting-started/contributing.html
Filling out the template is required. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
Mark the following tasks as done:
Description of the Change
This suppresses the warning
No such file or directory
when trying to unlink a nonexistent file inLocalTemporaryFile::delete()
.Why Should This Be Added?
After migrating our infrastructure to Laravel Vapor, which runs on AWS Lambda, exports are intermittently throwing this exception:
It appears that on occasion the Lambda instance is removing the local temp file before
StoreQueuedExport
finishes. I'm not certain why this is happening; perhaps it has to do with the execution time of the queued job.We have no reports so far of files being deleted before they are read, so the exports would otherwise finish successfully, except that the Laravel error handler throws an exception when
unlink()
raises the warning that the file does not exist.Since the only function of
LocalTemporaryFile::delete()
is to delete the local temp file, this change suppresses the warning if the file does not exist because then there is no need to delete the file and execution may continue normally.Benefits
We can reliably export spreadsheets on Laravel Vapor.
Possible Drawbacks
Error suppression is inherently risky. This implementation checks for file existence only after
unlink()
fails, to prevent a race condition. If the file still exists, the method will callunlink()
a second time without suppression so that the error message can bubble up to the handler.It could also be possible that the file name being passed for deletion is different than the actual or correct file name. This scenario seems unlikely, but probably should be tested against because suppressing this warning will also silence the error in this case.
Verification Process
I first confirmed the issue by duplicating the line
$this->localTemporaryFile->delete();
inRemoteTemporaryFile::delete()
so that the second call would consistently raise the warning that the file did not exist. Then I made the change, and successfully ran an export.I prefer automated testing but did not see an obvious place to include one and did not know if this should be unit tested or tested as part of another job. I'm happy to add a test if you give me some direction on where to add it.
Applicable Issues
#1974 #2312