Question

I have a table in my database called reminders. So a row may look like

Id    Subject     Time
 1     "Foo"      2014-14-03 13:30:00

I will then like to send an email on 2014-14-03 13:30:00. What is the best way of doing so? The only solution that I can think of is querying the database every minute to see what emails I need to send. Now I need a windows service and my website because the windows service needs to constantly be running. I think there may be a better solution.

Was it helpful?

Solution

You can use a library like Quartz.NET, and read your table when your service starts and periodically.

OTHER TIPS

I can think in something like a Queue. Is only an idea. Well, is not only a Queue, is like a Queue and a Stack together, something created by you.

This way if you add a new tasks with the time less than your first task, you can put this task in the first position.

You can create a Queue/Stack, then Enqueue a new element to the queue, and start a Timer (or a counter) with the difference between the time right now and the time of the scheluded task. This way you only need to check one of the tasks.

Then when the time now is equal to thefirst task in your Queue, Dequeue this task, and do the same with the new one. If the Queue is empty, you will not need to perform any operation to the database, only check the time in the first task.

I hope this helps.

I would use NServiceBus, and have a looping saga that reads from the db and fires off local messages back to the bus for each thing that needs to happen, such as 'send email' 'run this script' and then handlers for each message type. Check out line 110 here, this is how it loops:

https://github.com/escherrer/EC2Utilities/blob/master/EC2Utilities.ServiceBus/Sagas/StartServerSaga.cs

Other ways I can think of are:

  1. Quartz.NET (as schglurps mentions)
  2. If you want to avoid having to install a service then schedule an exe using Windows scheduler to run every minute or so and do what it needs to do.
  3. Roll your own entirely, if your just starting out I recommend this approach.

HTH, Eric

You only need to query the database as often as it can be updated. If you never insert a row requesting a new task to be run within the next hour, then you can have the service query once per hour a list of tasks to run over the next hour and cache the results in memory.

If you are constantly adding new tasks that need to be run immediately, or very soon, you will have to query the database equally as often.

Alternatively, if you are allowed to run programs on the SQL Server host, you could write a stored procedure that performs the INSERT then executes a program (that either does the task or notifies a remote host to do the task) using the xp_cmdshell command:

http://technet.microsoft.com/en-us/library/ms175046.aspx

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