123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System;
- using Quartz;
- using log4net;
- using Dapper;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Configuration;
- using System.Windows;
- namespace Agent.Services
- {
- class HealthService
- {
- private static readonly ILog log = LogManager.GetLogger(typeof(HealthService));
- public readonly JobKey JobKey = new JobKey("HealthJob");
- public readonly TriggerKey TriggerKey = new TriggerKey("HealthTrigger");
- public IJobDetail GetHealthJob()
- {
- return JobBuilder.Create<HealthJob>()
- .WithIdentity(JobKey)
- .Build();
- }
- public ITrigger GetHealthTrigger()
- {
- return TriggerBuilder.Create()
- .WithIdentity(TriggerKey)
- .StartNow()
- .WithSimpleSchedule(x => x
- .WithIntervalInSeconds(10)
- .RepeatForever())
- .Build();
- }
- public void ScheduledHealthJob()
- {
- App.Scheduler.ScheduleJob(GetHealthJob(), GetHealthTrigger());
- }
- /// <summary>
- /// 서버체크
- /// </summary>
- public void Check()
- {
- try
- {
- using (SqlConnection con = new SqlConnection(App._myConnection))
- {
- con.Open();
- var result = con.Query<string>("SELECT 'OK'", new { }).Single();
- Application.Current.Properties["db_status"] = "OK".Equals(result) ? result : null;
- }
- }
- catch(Exception)
- {
- Application.Current.Properties["db_status"] = null;
- log.Error("DB 연결 실패");
- }
- }
- /// <summary>
- /// DB connection 체크
- /// </summary>
- /// <returns></returns>
- public bool IsConnectionSetting()
- {
- return !(string.Empty == App._myConnection);
- }
- }
- [DisallowConcurrentExecution]
- public partial class HealthJob : IJob
- {
- private static readonly ILog log = LogManager.GetLogger(typeof(HealthJob));
- private readonly HealthService _healthService = new HealthService();
- public void Execute(IJobExecutionContext context)
- {
- _healthService.Check();
- }
- }
- }
|