Home > front end >  How to create a task that runs at a specific time, the most efficient way?
How to create a task that runs at a specific time, the most efficient way?

Time:11-27

Problem

How to execute many tasks defined at runtime at different set times in the most efficient way possible? Or have I already found the most efficient way?

Example System Being Built

For the sake of argument, I'll give an example of a sample program I'm trying to create.

Each time a user is added into a database, their birthday with birth time is stored, from this a task should run on their birthday and time to send them an email that says "happy birthday!".

What I've Tried

  1. Currently I would do this through Azure Functions and have a timer that executes every minute to check "is it anyone's birthday time?" If so, send email to all those with that birthday. Of course the issue with this is that you've then got a program running every minute.
  2. I've also looked into Azure Web Jobs but don't think that having potentially thousands of jobs running at the same time will work efficiently.

CodePudding user response:

I'm on linux ubuntu system, I'm using it as a server, I usually do that using cronjob/s.

CodePudding user response:

I've decided that the best approach for this task is a timer that runs every minute with the Cron Expression '0 * * * * *' (runs at second 0 of every minute of every hour). It's not the most efficient as it's essentially polling for results and especially when there's a pay per send with services such as Azure Functions. But if you need to check for results where it needs to be sent on the exact minute then this is the only feasible way for this task.

The only issue I have with it, is lets say there was 1,000,000 people with the exact same birthday (highly unlikely but lets say it happened), then the email sender would need to send a million emails at the same time, but of course this is something the sender would need to manage and is an entirely different question.

Luckily, as of writing this, when using Azure Functions (which I would be) then a million executions are allowed per month for free. There are only 43,800 minutes in a month and therefore the service would be free.

  • Related