markus / MarkusLogview / LogviewSqlDependency / LogviewSqlDependency.cs @ a7372e37
이력 | 보기 | 이력해설 | 다운로드 (9.97 KB)
1 | 84578b97 | djkim | using IKCOM; |
---|---|---|---|
2 | using MarkusDataModel.Common; |
||
3 | using MarkusDataModel.DataModel; |
||
4 | using Microsoft.AspNet.SignalR.Client; |
||
5 | using ServiceInterface; |
||
6 | using System; |
||
7 | using System.Collections.Generic; |
||
8 | using System.ComponentModel; |
||
9 | using System.Data; |
||
10 | using System.Diagnostics; |
||
11 | using System.Linq; |
||
12 | using System.Reflection; |
||
13 | using System.ServiceProcess; |
||
14 | using System.Text; |
||
15 | using System.Threading.Tasks; |
||
16 | using TableDependency; |
||
17 | using TableDependency.EventArgs; |
||
18 | using TableDependency.SqlClient; |
||
19 | using static LogviewSqlDependency.HubConfiguration; |
||
20 | |||
21 | namespace LogviewSqlDependency |
||
22 | { |
||
23 | partial class LogviewSqlDependency : ServiceBase |
||
24 | { |
||
25 | private static SqlTableDependency<CONVERTER_DOC> _convertTableDependency; |
||
26 | private static SqlTableDependency<FINAL_PDF> _finalTableDependency; |
||
27 | private static string entityConnString = ConnectStringBuilder.MarkusEntitiesConnectionString().ProviderConnectionString.ToString(); |
||
28 | private static ServiceEventLogger_db _serviceEventLogger = new ServiceEventLogger_db(); |
||
29 | |||
30 | private static string hubBaseURL = LogviewHubBaseURI.Url + LogviewHubBaseURI.Port + LogviewHubBaseURI.SignalrPath; |
||
31 | private static string hubName = "MarkusHub"; |
||
32 | private static ConvertSetting convSetting = new ConvertSetting(); |
||
33 | private static FinalSetting finalSetting = new FinalSetting(); |
||
34 | private static DetectedType detectedType = new DetectedType(); |
||
35 | |||
36 | private static ServiceController _service = new ServiceController("logviewDep_watcher"); |
||
37 | |||
38 | public LogviewSqlDependency() |
||
39 | { |
||
40 | InitializeComponent(); |
||
41 | } |
||
42 | |||
43 | protected override void OnStart(string[] args) |
||
44 | { |
||
45 | // TODO: 여기에 서비스를 시작하는 코드를 추가합니다. |
||
46 | #region sql dependency config |
||
47 | // convert |
||
48 | try |
||
49 | { |
||
50 | // sql dependency config |
||
51 | var convertMapper = new ModelToTableMapper<CONVERTER_DOC>(); |
||
52 | convertMapper.AddMapping(s => s.CURRENT_PAGE, "CURRENT_PAGE"); |
||
53 | _convertTableDependency = new SqlTableDependency<CONVERTER_DOC>(entityConnString, "CONVERTER_DOC", convertMapper); |
||
54 | |||
55 | // add event ( mapping ) |
||
56 | _convertTableDependency.OnError += SqlTableDependency_OnError; |
||
57 | _convertTableDependency.OnChanged += Convert_SqlTableDependency_Changed; |
||
58 | _convertTableDependency.Start(); |
||
59 | |||
60 | // event log |
||
61 | _serviceEventLogger.WriteEventEntry("Convert Table dependency" + MethodBase.GetCurrentMethod().Name, EventLogEntryType.SuccessAudit); |
||
62 | |||
63 | } |
||
64 | catch (Exception ex) |
||
65 | { |
||
66 | _serviceEventLogger.WriteEventEntry("Convert Table dependency ERROR " + MethodBase.GetCurrentMethod().Name + " : " + ex.Message, EventLogEntryType.Error); |
||
67 | throw; |
||
68 | } |
||
69 | |||
70 | // final |
||
71 | try |
||
72 | { |
||
73 | // sql dependency config |
||
74 | var finalMapper = new ModelToTableMapper<FINAL_PDF>(); |
||
75 | finalMapper.AddMapping(s => s.CURRENT_PAGE, "CURRENT_PAGE"); |
||
76 | _finalTableDependency = new SqlTableDependency<FINAL_PDF>(entityConnString, "FINAL_PDF", finalMapper); |
||
77 | |||
78 | // add event ( mapping ) |
||
79 | _finalTableDependency.OnError += SqlTableDependency_OnError; |
||
80 | _finalTableDependency.OnChanged += Final_SqlTableDependency_Changed; |
||
81 | _finalTableDependency.Start(); |
||
82 | |||
83 | // event log |
||
84 | _serviceEventLogger.WriteEventEntry("final Table dependency" + MethodBase.GetCurrentMethod().Name, EventLogEntryType.SuccessAudit); |
||
85 | |||
86 | } |
||
87 | catch (Exception ex) |
||
88 | { |
||
89 | _serviceEventLogger.WriteEventEntry("final Table dependency ERROR " + MethodBase.GetCurrentMethod().Name + " : " + ex.Message, EventLogEntryType.Error); |
||
90 | throw; |
||
91 | } |
||
92 | #endregion |
||
93 | } |
||
94 | |||
95 | protected override void OnStop() |
||
96 | { |
||
97 | // TODO: 서비스를 중지하는 데 필요한 작업을 수행하는 코드를 여기에 추가합니다. |
||
98 | } |
||
99 | |||
100 | /// <summary> |
||
101 | /// sql dependency 에러 |
||
102 | /// </summary> |
||
103 | /// <param name="sender"></param> |
||
104 | /// <param name="e"></param> |
||
105 | public static void SqlTableDependency_OnError(object sender, ErrorEventArgs e) |
||
106 | { |
||
107 | #region dependency error |
||
108 | _serviceEventLogger.WriteEventEntry(" SQL Dependency ERROR " + MethodBase.GetCurrentMethod().Name + " :: " + e, EventLogEntryType.Error); |
||
109 | |||
110 | |||
111 | try |
||
112 | { |
||
113 | |||
114 | Process[] dep = Process.GetProcessesByName("LoviewSqlDependency_Watcher"); |
||
115 | |||
116 | if (dep.Length > 0) |
||
117 | { |
||
118 | |||
119 | dep[0].Kill(); |
||
120 | //this.EventLog.WriteEntry("Dependency wather :: Dependency Service Kill"); |
||
121 | |||
122 | _service.Refresh(); |
||
123 | |||
124 | if (_service.Status == ServiceControllerStatus.Running || _service.Status == ServiceControllerStatus.StartPending) |
||
125 | { |
||
126 | |||
127 | _service.WaitForStatus(ServiceControllerStatus.Stopped); |
||
128 | |||
129 | } |
||
130 | |||
131 | _service.Start(new string[] { "Restart" }); |
||
132 | _service.WaitForStatus(ServiceControllerStatus.Running); |
||
133 | |||
134 | //this.EventLog.WriteEntry("Dependency wather :: Dependency Service start"); |
||
135 | |||
136 | } |
||
137 | else |
||
138 | { |
||
139 | |||
140 | _service.Start(); |
||
141 | _service.WaitForStatus(ServiceControllerStatus.Running); |
||
142 | |||
143 | //this.EventLog.WriteEntry("Dependency wather :: process not found"); |
||
144 | |||
145 | } |
||
146 | |||
147 | |||
148 | |||
149 | } |
||
150 | catch (Exception ex) |
||
151 | { |
||
152 | |||
153 | //this.EventLog.WriteEntry("Dependency wather :: Dependency service restart Error " + ex.ToString()); |
||
154 | |||
155 | } |
||
156 | |||
157 | |||
158 | throw e.Error; |
||
159 | #endregion |
||
160 | } |
||
161 | |||
162 | /// <summary> |
||
163 | /// 변경사항 ( 추가, 삭제, 수정 ) 감지 |
||
164 | /// </summary> |
||
165 | /// <param name="sender"></param> |
||
166 | /// <param name="e"></param> |
||
167 | public static void Convert_SqlTableDependency_Changed(object sender, RecordChangedEventArgs<CONVERTER_DOC> e) |
||
168 | { |
||
169 | #region convert table dependency |
||
170 | try |
||
171 | { |
||
172 | string changeType = e.ChangeType.ToString(); |
||
173 | DetectedMakrusConvertTable(e.ChangeType.ToString(), e.Entity); |
||
174 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name + " changeType : " + changeType, EventLogEntryType.SuccessAudit); |
||
175 | } |
||
176 | catch (Exception ex) |
||
177 | { |
||
178 | _serviceEventLogger.WriteEventEntry("Convert_SqlTableDependency_Changed Error : " + ex, EventLogEntryType.Error); |
||
179 | throw; |
||
180 | } |
||
181 | #endregion |
||
182 | } |
||
183 | |||
184 | /// <summary> |
||
185 | /// 변경사항 ( 추가, 삭제, 수정 ) 감지 |
||
186 | /// </summary> |
||
187 | /// <param name="sender"></param> |
||
188 | /// <param name="e"></param> |
||
189 | public static void Final_SqlTableDependency_Changed(object sender, RecordChangedEventArgs<FINAL_PDF> e) |
||
190 | { |
||
191 | #region final table dependency |
||
192 | try |
||
193 | { |
||
194 | string changeType = e.ChangeType.ToString(); |
||
195 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name + " changeType : " + changeType, EventLogEntryType.SuccessAudit); |
||
196 | |||
197 | DetectedMakrusFinalTable(changeType, e.Entity); |
||
198 | |||
199 | } |
||
200 | catch (Exception ex) |
||
201 | { |
||
202 | _serviceEventLogger.WriteEventEntry("Final_SqlTableDependency_Changed Error : " + ex, EventLogEntryType.Error); |
||
203 | throw; |
||
204 | } |
||
205 | #endregion |
||
206 | } |
||
207 | |||
208 | /// <summary> |
||
209 | /// Convert 테이블 변경 시 signalR을 통해 클라이언트에게 전송 |
||
210 | /// </summary> |
||
211 | /// <param name="type"></param> |
||
212 | /// <param name="data"></param> |
||
213 | public static void DetectedMakrusConvertTable(string type, CONVERTER_DOC data) |
||
214 | { |
||
215 | #region Convert Table SignalR |
||
216 | using (var hubConn = new HubConnection(hubBaseURL)) |
||
217 | { |
||
218 | |||
219 | var hub = hubConn.CreateHubProxy(hubName); |
||
220 | |||
221 | hubConn.Start().Wait(); |
||
222 | |||
223 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name, EventLogEntryType.SuccessAudit); |
||
224 | |||
225 | if (type == detectedType.DataInsert) |
||
226 | { |
||
227 | hub.Invoke(convSetting.InvokeInsert, data); |
||
228 | } |
||
229 | else if (type == detectedType.DataUpdate) |
||
230 | { |
||
231 | hub.Invoke(convSetting.InvokeChange, data); |
||
232 | } |
||
233 | else if (type == detectedType.DataDelete) |
||
234 | { |
||
235 | hub.Invoke(convSetting.InvokeDelete, data); |
||
236 | } |
||
237 | |||
238 | } |
||
239 | #endregion |
||
240 | } |
||
241 | |||
242 | /// <summary> |
||
243 | /// Final 테이블 변경 시 signalR을 통해 클라이언트에게 전송 |
||
244 | /// </summary> |
||
245 | /// <param name="type"></param> |
||
246 | /// <param name="data"></param> |
||
247 | public static void DetectedMakrusFinalTable(string type, FINAL_PDF data) |
||
248 | { |
||
249 | #region Final Table SignalR |
||
250 | using (var hubConn = new HubConnection(hubBaseURL)) |
||
251 | { |
||
252 | |||
253 | var hub = hubConn.CreateHubProxy(hubName); |
||
254 | |||
255 | hubConn.Start().Wait(); |
||
256 | |||
257 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name, EventLogEntryType.SuccessAudit); |
||
258 | |||
259 | if (type == detectedType.DataInsert) |
||
260 | { |
||
261 | hub.Invoke(finalSetting.InvokeInsert, data); |
||
262 | } |
||
263 | else if (type == detectedType.DataUpdate) |
||
264 | { |
||
265 | hub.Invoke(finalSetting.InvokeChange, data); |
||
266 | } |
||
267 | else if (type == detectedType.DataDelete) |
||
268 | { |
||
269 | hub.Invoke(finalSetting.InvokeDelete, data); |
||
270 | } |
||
271 | |||
272 | } |
||
273 | #endregion |
||
274 | } |
||
275 | |||
276 | } |
||
277 | |||
278 | } |