Email Monitor Sync

I am writing a program in C # that monitors a dedicated Gmail account using POP3 for specialized emails and responds accordingly.

For maximum reliability, I will run this program on multiple computers across the country. I currently have a race condition where two instances of a program can read the same message before one of them deletes it, causing the message to be processed twice.

How can I make sure each command is processed exactly once?

Gmail's POP3 access is only used to serve each message once (making RETR and DELE one atomic operation), but I can't reproduce that behavior anymore.

The only way to communicate between computers is SQL Server and the HTTP server (which I control).

+1


a source to share


2 answers


One option I thought of was to use the POP3 UIDL command and have a table on SQL Server with a unique UIDL column that has already been processed.



Then, before loading each message, the daemon will INSERT the UIDL into the table and if it gets an error, skip the message. (I am assuming the SQL Server INSERT command is an atomic operation).

+1


a source


At first I have to admit I don't know what commands POP3 supports, but ... if you can do an explicit "DELE" and get an error if the message no longer exists, I would say:

  • RETR message
  • DELE message
  • Process only if DELE succeeded

EDIT:



After reading RFC1939, this approach should work; from RFC:

DELE msg

     Arguments:
         a message-number (required) which may NOT refer to a
         message marked as deleted

     Restrictions:
         may only be given in the TRANSACTION state

     Discussion:
         The POP3 server marks the message as deleted.  Any future
         reference to the message-number associated with the message
         in a POP3 command generates an error.  The POP3 server does
         not actually delete the message until the POP3 session
         enters the UPDATE state.

     Possible Responses:
         +OK message deleted
         -ERR no such message

     Examples:
         C: DELE 1
         S: +OK message 1 deleted
            ...
         C: DELE 2
         S: -ERR message 2 already deleted

      

This means that the Gmail implementation is truly RFC-specific.

0


a source







All Articles