In a project I’m working on, users can subscribe themselves to a course. The duration of the course is variable, calculated in weeks (so 4, 5, or 6 weeks). They can subscribe at any given moment, so the start date is different per user.
Now I want to send the users a personalized email at the beginning of every week (so when they enter the 2nd, 3rd, etc week.). The email is different every week.
I know I have to write a task and schedule that on a regular basis, but I’m not sure how I should select the correct users. I could write a SQL-command, selecting all users where the start-date is 7, 14, 21, or 28 days ago, and calculate what week they are in, but that seems quite error prone (what if the task fails, then they will not receive the message in the next task-cycle).
What would be the best way to achieve this?
The project is a ASP.NET MVC application in C#, with NHibernate on IIS.
you are talking about scheduling workflows that take place at a future date and time. this is best handled by a scheduler.
conceptually you define what needs to be done and when it needs to be done. being that IIS will shutdown the website, if there are no users on the site hosting this process within IIS is not a good idea. a windows service would be a better choice. that way it’s always running the background ready to execute commands.
the implementation of the scheduler could be as simple as storing the action in the database ad querying the database on a timed interval, executing tasks that should have happened.
another option is to integrate a scheduling framework into your service like quartz.net.
EDIT:
what you are currently envisioning is an implicit definition of what should happen. in other words at the time the reminder is sent you calculate when and who should be notified.
instead calculate these values at the time the use registers for the course and save this as an explicit event. everything you need would already be stored in the event, all that remains is determining what events should fire. and that should be easy enough: any event that should have already fired, but didn’t. once it fires delete the event.