I have set up a cron job (PHP script), which does the following job :
1. Read all of the unseen emails, using imap
1.1 For each email readed
1.2. Fetches the details and save detail in database
1.3. Moves email to other mailbox named "Processed"
1.4. Goto step 1.1
But If the PHP script stops or fails executing, after step 1.2 but before 1.3, the emails will be re-processed, which is the problem here.
As we all know, the processing time depends on many factors such as server load and the number of emails to be processed, so I don't think that setting up max_execution_time will help.
How do I make this atomic? Any ideas? (I can change the process flow too, if you have any ideas about a different process flow).
Edit: Suppose that the email moved, and it was just going to commit, but the PHP script stops? What would happen then is that the email would move without a record being stored.