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() .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()); } /// /// 서버체크 /// public void Check() { try { using (SqlConnection con = new SqlConnection(App._myConnection)) { con.Open(); var result = con.Query("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 연결 실패"); } } /// /// DB connection 체크 /// /// 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(); } } }