Program.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using DllEapBll.Services;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using System;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MQMonitor
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
  16. var configuration = builder.Build();
  17. IServiceCollection services = new ServiceCollection();
  18. services.AddOptions();
  19. //注入
  20. //services.Configure<AppConfig>(opts =>
  21. //{
  22. // configuration.GetSection("AppConfig").Bind(opts);
  23. //});
  24. services.AddTransient<QueuemonitorService>();
  25. services.AddTransient<IConfiguration>(t =>
  26. {
  27. return configuration;
  28. });
  29. //构建容器
  30. IServiceProvider serviceProvider = services.BuildServiceProvider();
  31. // var config = serviceProvider.GetRequiredService<AppConfig>();
  32. var config = serviceProvider.GetRequiredService<IConfiguration>();
  33. int interval = Convert.ToInt32(configuration["Interval"]);
  34. var checkMysqlInterval = Convert.ToInt32(configuration["MysqlCheckInterval"]);
  35. Task.Run(() =>
  36. {
  37. while (true)
  38. {
  39. var service = serviceProvider.GetRequiredService<QueuemonitorService>();
  40. service.CheckMysqlSlaveState();
  41. Thread.Sleep(checkMysqlInterval * 60 * 1000);
  42. }
  43. });
  44. while (true)
  45. {
  46. var connStr = configuration.GetConnectionString("eap");
  47. using (IDatabase db = DbFactory.Base(connStr, DatabaseType.MySql))
  48. {
  49. var service = serviceProvider.GetRequiredService<QueuemonitorService>();
  50. service.Monitor(db);
  51. Thread.Sleep(interval * 60 * 1000);
  52. }
  53. }
  54. }
  55. }
  56. }