Search code examples
asp.net-mvcpush-notificationwindows-serviceslong-running-processes

How to do long running job and not wait for the result with ASP.NET MVC?


How to do long running job and not wait for the result with ASP.NET MVC? That job can be sending millions of emails, sending push notifications or any other long running job/process that we do not care for when that is being done, we just need to know that the process is started.

I have implemented that using a Windows Service which periodically checks if there are some email to be sent, and then execute the sending routine. That is good in a way because it is separate from web, can continue execution for as long as it is necessary and is pretty reliable. The bad thing is that needs to periodically check if there are emails to be sent, and from that interval deepens how long the users will wait. In the case of push notifications, and if the notifications has to be received immediately, maybe that will not be the best solution.

How to call a method in a separate thread from one user session and make sure it is executed and it continues to run even when the user logs-off from the site?


Solution

  • Don't use a threads of the web application for long running tasks because you will end up having scalability issues because the number of threads are limited.

    This is work for a windows service and to decouple it from the application creating the jobs use a queue (something like msmq, rabbitmq, aws sqs, azure queues). And if you say you want them to be processed faster put more consumers to those queues and if you are on aws or azure, then you should be able to auto-scale based on those queue numbers.