Below code snippets show I use to ensure only one invocation is happening at a time, using the class.Video can’t be loaded because JavaScript is disabled: Running Background Tasks in ASP.NET Core (HANGFIRE) () Hangfire I simply want to throttle the processing rate such that I only check and process an item from the queue at 5 seconds interval and only if no other processing is in progress. In my case, I need to fetch and act on the data coming from an azure queue storage. Handling overlapping invocationsĭepending on the context, you may not want to invoke a service unless the previous call has finished. In the Startup class, you simply configure your hosted service as in the snippets below: public void ConfigureServices(IServiceCollection services)Ībove, the JobPDFHostedService is a class that implements the IHostedService interface. Change the start time to infinite, thereby stop the timer. _logger.LogError("Received cancellation request before stopping timer.") public Task StopAsync(CancellationToken cancellationToken) This method is a good place to stop the timer. Once you have set and started the timer, it will keep invoking the method at the specified interval, irrespective of whether or not the previous invocation has finished, unless you stop the timer or the application shuts down.Īt application shut down, the system calls the StopAsync method. The Timer class accepts a parameter for storing state of the invocations. The example uses async lambdas as the DoWork method is asynchronous. This method is where you would have your business logic for your background job. In the above code snippets, DoWork is a method that accepts an object. Period: TimeSpan.FromSeconds(JobIntervalInSecs)) State: null, dueTime: TimeSpan.FromSeconds(0), _timer = new Timer(callback: async o => await DoWork(o), Invoke the DoWork method every 5 seconds. _logger.LogError("Received cancellation requestĬancellationToken.ThrowIfCancellationRequested() If (cancellationToken.IsCancellationRequested) Public Task StartAsync(CancellationToken cancellationToken) This is a good place to initiate the timer to run your job at The system invokes StartAsync method at application start up. private readonly int JobIntervalInSecs = 5 In the StartAsync method, you initiate and start the timer that invokes your job at a specified interval. The system calls StartAsync at application startup and StopAsync at application shutdown. The IHostedService interface exposes two primary methods, StartAsync and StopAsync. Configure dependency injection in Startup.In this class, setup the timer to invoke your job at a specified interval. Create a class that implements the IHostedService interface. If you host your ASP.NET core on IIS, check out my other post to see how you can have your application and thus your background task auto start and run continuously on IIS.īelow are the high level steps of what you need to get your background job running inside of an ASP.NET core application: One thing that was not clear from the documents was handling overlapping invocations that happens when an invocation starts but the previous one has not finished within the specified interval. It is straightforward for the most part, and Microsoft provides good documentation on the libraries. In this post, I share how I used the interface and class available in ASP.NET core to run a background job at a specified interval.
0 Comments
Leave a Reply. |