Single line queue in server farm


I will first explain the situation. Our software is running in a cloud server farm. We need to send e-mails to our clients with some kind of alerts. We decided that is better to send only 1 e-mail per 30 seconds, to avoid going to SPAM filters or blacklists.

    public void SendMail()

          //really send email

    public void Check()
        for (int i = 0; i < 10; i++)
            BackgroundJob.Enqueue(() =>

But, as Hangfire uses all possible servers to run threads and sometimes we have like 5 servers online, we could send 5 e-mails at same time. All servers run the same code, and I am unable to detect who server is who in code.

So, is it possible to categorize all my SendMail functions, in order to specify that them must follow only one line, maybe in one of the servers? I preffer to not specify which server, so it goes offline, another could continue the queue.

Thanks a lot.

So it sounds like you only want your server farm to provide high availability for your email jobs ? (But want other jobs to process concurrently still ?)

You should take a look at the DisableConcurrentExecution attribute. Apply this to your SendMail() method and it will get a distributed lock for each method it is applied to. You should keep in mind that each server will try to process a mail job at the same time but will block, holding up one of the threads on each server.