HealthService.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using Quartz;
  3. using log4net;
  4. using Dapper;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Configuration;
  8. using System.Windows;
  9. namespace Agent.Services
  10. {
  11. class HealthService
  12. {
  13. private static readonly ILog log = LogManager.GetLogger(typeof(HealthService));
  14. public readonly JobKey JobKey = new JobKey("HealthJob");
  15. public readonly TriggerKey TriggerKey = new TriggerKey("HealthTrigger");
  16. public IJobDetail GetHealthJob()
  17. {
  18. return JobBuilder.Create<HealthJob>()
  19. .WithIdentity(JobKey)
  20. .Build();
  21. }
  22. public ITrigger GetHealthTrigger()
  23. {
  24. return TriggerBuilder.Create()
  25. .WithIdentity(TriggerKey)
  26. .StartNow()
  27. .WithSimpleSchedule(x => x
  28. .WithIntervalInSeconds(10)
  29. .RepeatForever())
  30. .Build();
  31. }
  32. public void ScheduledHealthJob()
  33. {
  34. App.Scheduler.ScheduleJob(GetHealthJob(), GetHealthTrigger());
  35. }
  36. /// <summary>
  37. /// 서버체크
  38. /// </summary>
  39. public void Check()
  40. {
  41. try
  42. {
  43. using (SqlConnection con = new SqlConnection(App._myConnection))
  44. {
  45. con.Open();
  46. var result = con.Query<string>("SELECT 'OK'", new { }).Single();
  47. Application.Current.Properties["db_status"] = "OK".Equals(result) ? result : null;
  48. }
  49. }
  50. catch(Exception)
  51. {
  52. Application.Current.Properties["db_status"] = null;
  53. log.Error("DB 연결 실패");
  54. }
  55. }
  56. /// <summary>
  57. /// DB connection 체크
  58. /// </summary>
  59. /// <returns></returns>
  60. public bool IsConnectionSetting()
  61. {
  62. return !(string.Empty == App._myConnection);
  63. }
  64. }
  65. [DisallowConcurrentExecution]
  66. public partial class HealthJob : IJob
  67. {
  68. private static readonly ILog log = LogManager.GetLogger(typeof(HealthJob));
  69. private readonly HealthService _healthService = new HealthService();
  70. public void Execute(IJobExecutionContext context)
  71. {
  72. _healthService.Check();
  73. }
  74. }
  75. }