Question

I have a Rails API that is accessed by a desktop application to alert me of certain events that take place. If a flag is toggled when the desktop application hits my API, it will send an email. In testing this from curl, the email sending process takes about eight seconds on my development environment. I suspect that once in the wild this may time out.

On to my question, What is the typical process for handling automatic emails? Do I typically send an email off as the API request comes in? Or do I have a background process that sits and waits for certain flags to be ticked then sends out emails?

Was it helpful?

Solution

There are many ways to approach that but given that you have an API you could just schedule a background job whenever your criteria are met.

So you receive an API request, you check whether your criteria are met, and if so you schedule an asynchronous email-sending job (in the background). Your API returns immediately.

This way if a job fails (for timeout or other reasons) you can see it, reschedule automatic retries, get nice logs/reporting etc. And you don't block your whole Rails app while waiting for the email to be sent.

There are many nice solutions for Rails background queuing systems including Sidekiq, resque, and delayed_job. I suggest that you go through their documentation to see which fits best your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top