Jump to content

Executing method on schedule in C#

Hey there! I need someone experienced in C# to help me with a suggestion. I have to implement a method in a windows service, that would have to run every day, only once, at 7 a.m. (method is sending emails and notifying customers as they are expected to pick up their orders from the shop that day). What is the best approach to implementing this? Thank you in advance!

Link to comment
https://linustechtips.com/topic/1259688-executing-method-on-schedule-in-c/
Share on other sites

Link to post
Share on other sites

1 - Get the current time

2 - Get difference between now and next iteration

3 - Start a System.Timers.Timer with the elapsed time and once it triggers run your code.

 

then rinse and repeat. Why ? because the calculation for next iteration might change due to daylight saving time and you need to recompute it. You can't simply set 24 hours in milliseconds.

 

Now why using specially the timer in System.Timers.Timer you would say. The reason is because this specific timer uses the windows scheduler which instead of spin wait it is simply not using any cpu and has a trigger set in the scheduler. Once the scheduler get to the set elapsed value it triggers your event. Not that it is NOT an atomic operation.

It might run the code at 6:59.999. The scheduler try to execute the trigger as close as possible to the requested time.

Link to post
Share on other sites

On 10/20/2020 at 2:47 AM, Elwing said:

(method is sending emails and notifying customers as they are expected to pick up their orders from the shop that day). What is the best approach to implementing this?

Others have told you the correct answer, but I'm interested:

Why do you batch all of the customer notifications to the next day instead of telling them as soon as their order is ready, so you can have same day pickup? I imagine it's some kind of business reason, but I'm interested to know the answer.

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

2 hours ago, straight_stewie said:

Others have told you the correct answer, but I'm interested:

Why do you batch all of the customer notifications to the next day instead of telling them as soon as their order is ready, so you can have same day pickup? I imagine it's some kind of business reason, but I'm interested to know the answer.

You just made me think about it. He has to watch out. Sending mass email like this might put you on international black list for spam emailer and your domain / ip range can be completely blocked. I do not know how to handle this part but a couple company i worked with were black listed. Their IT had to contact some company proving they were not spammer.

Link to post
Share on other sites

6 hours ago, Sakuriru said:

I would argue that your initial idea is on the right path. But if not built correctly it can lead to more problems than it can be worth. If the emailing process is put into the same service that's handling workflow or order management, then technical debt is increased significantly. What you actually need is a way to call a service that will send the email for you. What a better way to do this than through a web API? It's what they're designed for. The advantages when designed correctly are numerous

  1. The web app can be load balanced, meaning that it is scalable.
  2. The web app doesn't need to connect to any database or look up order details. Those are passed to it by the service calling its API. This reduces dependence on outside data sources.
  3. The service can handle an asynchronous request to this API, meaning that the workflow service can spin up a background process that does nothing but listen for a response. The workflow service is not impacted by any processing that would need to go into sending the email.
  4. The emails can be sent as push notification.

The obvious disadvantage is that it requires more server architecture and most important, developer skill. Your average junior developer would not be capable of setting up this system; it relies on a multitude of technologies such as containerization, cloud infrastructure, and web API development. But it should come as no surprise that micro-service architecture is as popular as it is considering these advantages.

This was basically where I was headed with that.
 

 

One thing I would like to add to your short analysis of options is that many small businesses are starting to open up webfronts, especially now that people are expecting contactless pickup/delivery and even same day delivery/pickups. This is going to become the commonplace way of doing business, and many people see that.

 

So my comment would be that, frequently, the business wants to do same day pickup, but can't find a contract developer capable of building/supporting the system necessary to do it. Mostly because, well, you're right. For the reasons you've listed, you practically have to have skilled in house developers to be able to build and maintain a same day pickup notification service.

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

On 10/24/2020 at 11:06 PM, straight_stewie said:

For the reasons you've listed, you practically have to have skilled in house developers to be able to build and maintain a same day pickup notification service.

Not necessary full time in house dev. I've made countless notification API that i haven't touched in years as nothing was required to changed or it was made to be scalable (as quoted) and possible to modify by everyday joe using an admin panel interface of some sort.

 

I admit with little experience it can be a huge task when you do not know this. I know it has quite a lot of complexity that people fresh out of school have not seen especially since it is a real life scenario. Nothing is perfect and a lot of component intertwine here.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×