Even though I have 15 years of experience as a full-stack developer, I make mistakes. Sometimes, silly mistakes. I am good at solving problems and providing solutions to complex business requirements. But I don’t know everything, even some basic things.
Recently, my client asked me to add a feature to attach a file to the emails they send from the application. As I use Laravel in the application, I simply added a file input to the form and added a few lines of code to attach the file to the email. It worked fine in my local env.
I deployed it on the live servers and I did not test this feature there as I thought I need to send an email to someone else to test it. I have forgotten that I also have an account on the production app which can be used for testing.
Apart from that, I thought that this works on my machine and I don’t need to test it on production for such a small feature addition. But no one realized that I have broken the complete feature.
We don’t track all the error logs on this project. Whenever there is some complaint that something is not working, I check the logs to find out the issue and fix it.
While working on another feature, I ended up seeing the log and saw that it was full of errors.
When I investigated I realized that client had tried to send a bulk email to many users with an attachment. As the emails are added to a queue, she did not see any error. But when the queue started to run the jobs, it did not work and threw errors.
The error was
Unable to open file for reading [/tmp/phphwnWbB]
First I thought it might be an issue with permissions. But file uploads work fine on the app. It should not be an issue with permission. I tried to find the file in /tmp/ but did not get it.
It took me really long to understand why it was not working. I suspected that the temp file might not be available when the job executes from the queue even though it runs pretty quickly. Soon, I realized that I have made a blunder by designing a feature like this.
I should not depend on a temp file to be used by jobs that run on a separate process. First, I need to handle the temp file and then make it usable by other processes. But I was still thinking about how does it work in my local dev environment.
Then I realized that in my local dev environment, I don’t run a real queue. The queue is sync. It runs in the same request cycle. Then I tried to find how much time the file stays in the temporary storage. I realized that the file is deleted at the end of the request.
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.https://www.php.net/manual/en/features.file-upload.post-method.php
I did not know that even though it was basic. As a result, I end up programming the feature badly and we ended up with the issue.