The current situation:
I have an order that is started in the client side and that should run continiously in the backend doing tasks in that order until the time is not within 09:00 – 17:00 (The working time). It should not run on weekends as well. When it’s not working time the execution of tasks should stop until next working itme. I am currently implementing this functionality kind of succesfully with Thread.Sleep()
. I know this solution is frowned upon, so I am open to other solutions.
So the current behavior of the program:
- Client starts order
- It checks the time, it’s within 9-17 and not the weekend.
- It starts executing tasks until 17 and then succesfully stops.
- A utility function calculates how long the thread should sleep in milliseconds until the execution of task should start again.
- I check the next day (It’s running on a hosted Azure App Service) and it has succesfully sent the message to frontend at the correct time 09:00 that execution of tasks will presume.
- However the execution of tasks is not happening, when I know that there exists remaining tasks on that order object.
[HttpPost ("/StartWorkFromClientSide")]
public async Task < IActionResult > StartWork (string orderID)
{
var order = Orders.find (orderID);
while (!order.IsCompleted)
{
for (task in order.tasks) {
//this DateTime var should be recalculated
//when continue in condition block executes and then execute current task
DateTime currentDateTime = DateTime.Now;
if (currentDateTime is weekend || time is not within 0900:1700)
{
var sleepTime = calculateSleepTime (currentDateTime);
Thread.Sleep (sleepTime);
//when done sleeping (potentially in 1-3 days),
//start tasks again and send msg to frontend
await clients.hub.all.sendMessage (someMessage);
//since continue skips current loop item remove and append so its done later,
//the order of the tasks is not important:
order.tasks.remove (task);
order.tasks.append (task);
continue;
}
execute (task);
} //end of for loop
}
(Pseudo-ish code of my actual implementation ^)
Ole Askeland is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.