I am building an application which will send status requests to users (via email & sms) on a regular basis. I want to execute the service each hour which will:
- Query the database for all requests that need to be sent (based on some logic)
- Send the requests through Amazon’s Simple Email Service (this is already working)
- Write a record of the status request notification back to the data store
I am considering wrapping up this series of operations into a single controller with an end point that can be called remotely to kick off the process within the rails app.
Longer term, I will break this process out into an app that can be run independently of my rails app, but for now I’m just trying to keep it simple.
My first inclination is to build the following:
- Controller with the following elements:
- A method which will orchestrate the steps outlined above (and can be called externally)
- A call to the
status_requestmodel which will bring back a collection of request needing to be sent - A loop to iterate through the pending requests, which will:
- Make a call to my AWS Simple Email Service module to actually send the email, and
- Make a call to the
status_requestmodel to log the request back to the database
- Model:
- A method on my
status_requestmodel which will bring back a collection of requests that need to be sent - A method in my
status_requestmodel which will log that a notification was sent
- A method on my
Since this will behave as a service that gets called periodically from an outside scheduler I don’t think I’ll need a view for this operation. (Will, of course, need views to show users and admins what requests have been sent, but that’s later…).
As someone new to Rails, I’m asking for review of this approach and any suggestions you may have.
Thanks!
Instead of a controller which Jeff pointed out exposes a security risk, you may just want to expose a rake task and use cron to invoke it on an hourly basis.
If you are still interested in building a controller, look at devise gem and its single access token, token_authenticatable, for securing the methods you are exposing.
You may also want to look at delayed_job or resque to offload the call to status_request and the loop to AWS simple service to a background worker process.
You may want a seperate controller and view for the log file so you can review progress on demand.
And if you want to get real fancy use Amazon SNS to send you alerts when the service reaches some unacceptable level of failures, backlog, etc.