fix: preserve RemoteTemporaryFile type when working with remote storage #4290
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.
1️⃣ Why should it be added? What are the benefits of this change?
This PR fixes a critical bug in distributed environments where Laravel Excel exports are queued but processed across multiple servers. When using a remote disk for temporary files, if a different server processes the initial export job than the one that received the request, the export fails.
The issue occurs because the current code flow replaces the
RemoteTemporaryFile
instance with a newLocalTemporaryFile
when trying to ensure the local file exists 1. This type change breaks the subsequent operations that expect aRemoteTemporaryFile
and the remote file is never updated 2. This results in an empty file being loaded when the next job in the chain tries to process it.The fix preserves the
RemoteTemporaryFile
type while ensuring the local directory structure exists for writing, making queued exports work reliably in distributed environments.2️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out.
No, this PR contains a single focused fix for the
RemoteTemporaryFile
handling issue.3️⃣ Does it include tests, if possible?
No, as this is specifically related to multi-server distributed environments which would be challenging to simulate in automated tests.
4️⃣ Any drawbacks? Possible breaking changes?
No breaking changes. The modification to
RemoteTemporaryFile::sync()
adds an optional parameter with a default value that preserves the original behavior.5️⃣ Mark the following tasks as done:
[ ] Added tests to ensure against regression.(not applicable in this case)6️⃣ Thanks for contributing! 🙌
Additional Information
Problem Details:
In a distributed environment with multiple servers (A and B), when using a remote disk for temporary files:
Exportable::queue()
LocalTemporaryFile
instance is created 3 (RemoteTemporaryFile
if a temporalDisk is specified 4).LocalTemporaryFile
touches the file on the local disk 5.RemoteTemporaryFile
touches the file on the remote server 6.QueueExport
→AppendQueryToSheet
|AppendDataToSheet
|... →CloseSheet
→StoreQueuedExport
).QueueExport
job, it detects that the local file doesn't exist 1.LocalTemporaryFile
instance, changing the type fromRemoteTemporaryFile
toLocalTemporaryFile
.instanceof RemoteTemporaryFile
2 failed.The fix preserves the
RemoteTemporaryFile
type by modifyingRemoteTemporaryFile::sync()
to accept an optional parameter that allows creating the directory structure without copying content (which would be redundant since it's about to be overwritten anyway) 2.Footnotes
https://github.com/SpartnerNL/Laravel-Excel/blob/e25d44a2d91da9179cd2d7fec952313548597a79/src/Writer.php#L179-L182 ↩ ↩2
https://github.com/SpartnerNL/Laravel-Excel/blob/e25d44a2d91da9179cd2d7fec952313548597a79/src/Writer.php#L188-L191 ↩ ↩2 ↩3
https://github.com/SpartnerNL/Laravel-Excel/blob/e25d44a2d91da9179cd2d7fec952313548597a79/src/QueuedWriter.php#L64 ↩
https://github.com/SpartnerNL/Laravel-Excel/blob/e25d44a2d91da9179cd2d7fec952313548597a79/src/Files/TemporaryFileFactory.php#L35-L37 ↩
https://github.com/SpartnerNL/Laravel-Excel/blob/e25d44a2d91da9179cd2d7fec952313548597a79/src/Files/LocalTemporaryFile.php#L17 ↩
https://github.com/SpartnerNL/Laravel-Excel/blob/e25d44a2d91da9179cd2d7fec952313548597a79/src/Files/RemoteTemporaryFile.php#L40 ↩
https://github.com/SpartnerNL/Laravel-Excel/blob/e25d44a2d91da9179cd2d7fec952313548597a79/src/QueuedWriter.php#L76 ↩