![]() The hard part is finding a Task where you really do not care about the result of the Task. You do want to make sure a network packet is sent to the receiverĪnd if you care about the result of the Task then fire-and-forget is the wrong method.You do want to make sure a Log entry is written to the hard drive.You do want to make sure a database write went through.But for most Tasks you do care about the result. You do not care if it exits successfully, if it is canceled or if it threw an exception. When you execute a Task using fire-and-forget you are basically telling me that you do not care about the result of this function. I think the reason why most devs have a problem with fire-and-forget is that it is often misused. So the output could be: Event 0Įdit: Is it ok in a production environment to execute code this way, without awaiting it? Using this implementation the order of the events logged in no longer in order as each task waits a random amount of time. Var item = await TaskQueue.DequeueAsync(_shutdown.Token) Id probably make a shell file to run the asp.net core app, and run the shell job as the cron task, but you can run directly. While (!_shutdown.IsCancellationRequested) Here is an example implementation that allows two Tasks to be executed in parallel: private async Task BackgroundProceessing() In order to introduce parallelism we have to change the implementation of the BackgroundProceessing method in the QueuedHostedService. But each item will be executed in order so that the output looks like this: Event 1 If you put the code in a controller method the events will still be logged even after controller method returns. We add ten tasks which will wait a random amount of time. This article provides a good overview.Ĭonsider the following example: _logger.LogInformation ("Before()") Īwait Task.Delay (random.Next (50, 1000), token) ![]() An IHostedService is running per definition in parallel to the web server. The code you posted executes the queued items in order, one at a time but also in parallel to the web server. ![]() Public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask Private int UntilNextExecution() => Math.Max(0, (int)_nextRun.Subtract(DateTime.Now).TotalMilliseconds) Such as: Background Processing In ASP.NET Core With Hangfire. And then search 'Hangfire Schedule Background Jobs in ASP.Net Core' using Google or Bing, there should have multiple articles. While (!cancellationToken.IsCancellationRequested)Īwait Task.Delay(UntilNextExecution(), cancellationToken) // wait until next timeĪwait _task.Execute() //execute some task To use Hangfire create a Cron job, you could check the Hangfire document to install and register the Hangfile. Public Task StartAsync(CancellationToken cancellationToken) _nextRun = _crontabSchedule.GetNextOccurrence(DateTime.Now) ![]() _crontabSchedule = CrontabSchedule.Parse(Schedule, new CrontabSchedule.ParseOptions) Public SomeScheduledService(SomeTask task) Private const string Schedule = "0 0 1 * * *" // run day at 1 am Private readonly CrontabSchedule _crontabSchedule public class SomeScheduledService: IHostedService The implementation below is lightweight (no architecture imposing libs) and no polling. I combined CrontabSchedule with IHostedService. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |