I'm using Postfix on a server to receive emails.
When an email is received I want to trigger some golang code.
I'm using the inotify package, and am monitoring the Maildir/new folder for new incoming emails.
I'm monitoring only the IN_CLOSE_WRITE event on the folder, but I see it's never being triggered. Only IN_CREATE events are being triggered.
I assume that as I'm monitoring the folder, and not the file, it sees just the create file events (as they effect the folder), not what happens later to the file (close write).
However I don't know the name of the file in advance, it's a temp file being created by Postfix outside of my control, and I don't want just to sleep for let's say 1 second as it may take longer for Postfix to finish writing, and then on the other hand if I try to set up a watch quickly on that file I may be on a race condition with Postfix, as it may finish writing the email much faster that I would have even established the initial watch, and I would not be able to distinguish between file created, written and closed, and write in progress, as events are not being triggered during that time.
How would you have elegantly resolved this issue? I don't want to sleep, as it's not elegant and I cannot know for sure for how long, and I want it to be as short as possible, and I don't want to be in any race condition with Postfix.
I believe Postfix easily lets you hook into the actual delivery event. Look at how Procmail does it, or even just create a Procmail rule to run your script when something is delivered.
In very brief, in your
.procmailrc, putto pipe a copy of each incoming message to your binary.