The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrackingService.cs 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Microsoft.Extensions.Hosting;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Teknik.Logging;
  9. using Teknik.Utilities;
  10. namespace Teknik.Tracking
  11. {
  12. public class TrackingService : BackgroundService
  13. {
  14. private readonly ILogger<Logger> _logger;
  15. public TrackingService(IBackgroundTaskQueue taskQueue,
  16. ILogger<Logger> logger)
  17. {
  18. TaskQueue = taskQueue;
  19. _logger = logger;
  20. }
  21. public IBackgroundTaskQueue TaskQueue { get; }
  22. protected async override Task ExecuteAsync(CancellationToken cancellationToken)
  23. {
  24. _logger.LogInformation("Queued Hosted Service is starting.");
  25. while (!cancellationToken.IsCancellationRequested)
  26. {
  27. var workItem = await TaskQueue.DequeueAsync(cancellationToken);
  28. try
  29. {
  30. await workItem(cancellationToken);
  31. }
  32. catch (Exception ex)
  33. {
  34. _logger.LogError(ex,
  35. $"Error occurred executing {nameof(workItem)}.");
  36. }
  37. }
  38. _logger.LogInformation("Queued Hosted Service is stopping.");
  39. }
  40. }
  41. }