Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore;
  7. using Microsoft.AspNetCore.Builder;
  8. using Microsoft.AspNetCore.Diagnostics.HealthChecks;
  9. using Microsoft.AspNetCore.Hosting;
  10. using Microsoft.AspNetCore.Routing;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.Diagnostics.HealthChecks;
  13. using Microsoft.Extensions.Hosting;
  14. using Microsoft.Extensions.Localization;
  15. using Microsoft.Extensions.Logging;
  16.  
  17. namespace WorkerService1
  18. {
  19. public class Program
  20. {
  21. public static void Main(string[] args)
  22. {
  23. var witness = new WorkerWitness();
  24.  
  25. WebHost.CreateDefaultBuilder()
  26. .ConfigureServices(services =>
  27. {
  28. services
  29. .AddSingleton(sp => witness)
  30. .AddHealthChecks()
  31. .AddCheck<WorkerHealthCheck>("worker1");
  32. })
  33. .Configure(app =>
  34. {
  35. app
  36. .UseRouting()
  37. .UseEndpoints(config =>
  38. {
  39. config.MapHealthChecks("/health", new HealthCheckOptions
  40. {
  41. Predicate = r => true
  42. });
  43. });
  44. })
  45. .Build()
  46. .StartAsync();
  47.  
  48. Host.CreateDefaultBuilder(args)
  49. .ConfigureServices((hostContext, services) =>
  50. {
  51. services
  52. .AddSingleton(sp => witness)
  53. .AddHostedService<Worker>();
  54. })
  55. .Build()
  56. .Run();
  57. }
  58.  
  59. }
  60.  
  61. public class Worker : BackgroundService
  62. {
  63. private readonly ILogger<Worker> _logger;
  64. private readonly WorkerWitness _witness;
  65.  
  66. public Worker(ILogger<Worker> logger, WorkerWitness witness)
  67. {
  68. _logger = logger;
  69. _witness = witness;
  70. }
  71.  
  72. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  73. {
  74. while (!stoppingToken.IsCancellationRequested)
  75. {
  76. _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
  77. _witness.LastExecution = DateTime.Now;
  78. await Task.Delay(1000, stoppingToken);
  79. }
  80. }
  81. }
  82.  
  83. internal class WorkerHealthCheck : IHealthCheck
  84. {
  85. private readonly WorkerWitness _witness;
  86.  
  87. public WorkerHealthCheck(WorkerWitness witness)
  88. {
  89. _witness = witness;
  90. }
  91. public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
  92. {
  93. //Check if service run for the last 2 mins
  94. return DateTime.Now.Subtract(_witness.LastExecution).TotalSeconds < 120 ?
  95. Task.FromResult(HealthCheckResult.Healthy()) :
  96. Task.FromResult(HealthCheckResult.Unhealthy());
  97. }
  98. }
  99.  
  100. public class WorkerWitness
  101. {
  102. public DateTime LastExecution { get; set; }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement