markus / MarkusLogview / SignalREngineServiceWindowsService / SignalREngineServiceWindowsService.cs @ 84578b97
이력 | 보기 | 이력해설 | 다운로드 (13.7 KB)
1 | 84578b97 | djkim | using System; |
---|---|---|---|
2 | using System.Data.Common; |
||
3 | using System.Diagnostics; |
||
4 | using System.Diagnostics.Contracts; |
||
5 | using System.Linq; |
||
6 | using System.Reflection; |
||
7 | using System.ServiceProcess; |
||
8 | using System.Threading; |
||
9 | using System.Timers; |
||
10 | using MarkusDataModel.Common; |
||
11 | using MarkusDataModel.DataModel; |
||
12 | using MarkusDataModel.DTOs; |
||
13 | using Microsoft.AspNet.SignalR.Client; |
||
14 | using ServiceInterface; |
||
15 | using SignalREngine; |
||
16 | using SignalREngineServiceWindowsService.HubConfig; |
||
17 | using TableDependency; |
||
18 | using TableDependency.EventArgs; |
||
19 | using TableDependency.SqlClient; |
||
20 | |||
21 | namespace SignalREngineServiceWindowsService |
||
22 | { |
||
23 | class SignalREngineServiceWindowsService : ServiceBase |
||
24 | { |
||
25 | #region Default Configuration , Init variable |
||
26 | |||
27 | /// <summary> |
||
28 | /// Sql Table Dependency |
||
29 | /// </summary> |
||
30 | private static SqlTableDependency<CONVERTER_DOC> _convertTableDependency; |
||
31 | private static SqlTableDependency<FINAL_PDF> _finalTableDependency; |
||
32 | private static string entityConnString = ConnectStringBuilder.MarkusConnectionString().ProviderConnectionString.ToString(); |
||
33 | private static string hubBaseURL = "http://localhost:8089"; |
||
34 | private static string hubName = "MarkusHub"; |
||
35 | private static ConvertSetting convSetting = new ConvertSetting(); |
||
36 | private static FinalSetting finalSetting = new FinalSetting(); |
||
37 | private static DetectedType detectedType = new DetectedType(); |
||
38 | |||
39 | /// <summary> |
||
40 | /// Logger |
||
41 | /// </summary> |
||
42 | private static ServiceEventLogger _serviceEventLogger = new ServiceEventLogger(); |
||
43 | |||
44 | /// <summary> |
||
45 | /// Public Constructor for WindowsService. |
||
46 | /// - Put all of your Initialization code here. |
||
47 | /// </summary> |
||
48 | public SignalREngineServiceWindowsService() |
||
49 | { |
||
50 | |||
51 | this.ServiceName = "SignalREngineServiceWindowsService"; |
||
52 | this.EventLog.Log = "Application"; |
||
53 | |||
54 | // These Flags set whether or not to handle that specific |
||
55 | // type of event. Set to true if you need it, false otherwise. |
||
56 | this.CanHandlePowerEvent = true; |
||
57 | this.CanHandleSessionChangeEvent = true; |
||
58 | this.CanPauseAndContinue = true; |
||
59 | this.CanShutdown = true; |
||
60 | this.CanStop = true; |
||
61 | |||
62 | } |
||
63 | |||
64 | /// <summary> |
||
65 | /// Dispose of objects that need it here. |
||
66 | /// </summary> |
||
67 | /// <param name="disposing">Whether |
||
68 | /// or not disposing is going on.</param> |
||
69 | protected override void Dispose(bool disposing) |
||
70 | { |
||
71 | base.Dispose(disposing); |
||
72 | } |
||
73 | |||
74 | /// <summary> |
||
75 | /// OnStart(): Put startup code here |
||
76 | /// - Start threads, get inital data, etc. |
||
77 | /// </summary> |
||
78 | /// <param name="args"></param> |
||
79 | protected override void OnStart(string[] args) |
||
80 | { |
||
81 | |||
82 | AllStart_Service(); |
||
83 | base.OnStart(args); |
||
84 | |||
85 | } |
||
86 | #endregion |
||
87 | |||
88 | #region service override method |
||
89 | /// <summary> |
||
90 | /// OnStop(): Put your stop code here |
||
91 | /// - Stop threads, set final data, etc. |
||
92 | /// </summary> |
||
93 | protected override void OnStop() |
||
94 | { |
||
95 | //this.timer.Stop(); |
||
96 | //this.timer = null; |
||
97 | base.OnStop(); |
||
98 | } |
||
99 | |||
100 | /// <summary> |
||
101 | /// OnPause: Put your pause code here |
||
102 | /// - Pause working threads, etc. |
||
103 | /// </summary> |
||
104 | protected override void OnPause() |
||
105 | { |
||
106 | base.OnPause(); |
||
107 | } |
||
108 | |||
109 | /// <summary> |
||
110 | /// OnContinue(): Put your continue code here |
||
111 | /// - Un-pause working threads, etc. |
||
112 | /// </summary> |
||
113 | protected override void OnContinue() |
||
114 | { |
||
115 | base.OnContinue(); |
||
116 | } |
||
117 | |||
118 | /// <summary> |
||
119 | /// OnShutdown(): Called when the System is shutting down |
||
120 | /// - Put code here when you need special handling |
||
121 | /// of code that deals with a system shutdown, such |
||
122 | /// as saving special data before shutdown. |
||
123 | /// </summary> |
||
124 | protected override void OnShutdown() |
||
125 | { |
||
126 | base.OnShutdown(); |
||
127 | } |
||
128 | |||
129 | /// <summary> |
||
130 | /// OnCustomCommand(): If you need to send a command to your |
||
131 | /// service without the need for Remoting or Sockets, use |
||
132 | /// this method to do custom methods. |
||
133 | /// </summary> |
||
134 | /// <param name="command">Arbitrary Integer between 128 & 256</param> |
||
135 | protected override void OnCustomCommand(int command) |
||
136 | { |
||
137 | // A custom command can be sent to a service by using this method: |
||
138 | //# int command = 128; //Some Arbitrary number between 128 & 256 |
||
139 | //# ServiceController sc = new ServiceController("NameOfService"); |
||
140 | //# sc.ExecuteCommand(command); |
||
141 | |||
142 | base.OnCustomCommand(command); |
||
143 | } |
||
144 | |||
145 | /// <summary> |
||
146 | /// OnPowerEvent(): Useful for detecting power status changes, |
||
147 | /// such as going into Suspend mode or Low Battery for laptops. |
||
148 | /// </summary> |
||
149 | /// <param name="powerStatus">The Power Broadcast Status |
||
150 | /// (BatteryLow, Suspend, etc.)</param> |
||
151 | protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) |
||
152 | { |
||
153 | return base.OnPowerEvent(powerStatus); |
||
154 | } |
||
155 | |||
156 | /// <summary> |
||
157 | /// OnSessionChange(): To handle a change event |
||
158 | /// from a Terminal Server session. |
||
159 | /// Useful if you need to determine |
||
160 | /// when a user logs in remotely or logs off, |
||
161 | /// or when someone logs into the console. |
||
162 | /// </summary> |
||
163 | /// <param name="changeDescription">The Session Change |
||
164 | /// Event that occured.</param> |
||
165 | protected override void OnSessionChange( |
||
166 | SessionChangeDescription changeDescription) |
||
167 | { |
||
168 | base.OnSessionChange(changeDescription); |
||
169 | } |
||
170 | |||
171 | private void InitializeComponent() |
||
172 | { |
||
173 | |||
174 | } |
||
175 | #endregion |
||
176 | |||
177 | #region Table Dependency Configuration |
||
178 | |||
179 | |||
180 | public static void DependencyRestart() |
||
181 | { |
||
182 | |||
183 | TimeSpan _waitTime = TimeSpan.FromSeconds(2); |
||
184 | |||
185 | ServiceController _watcher = new ServiceController("SignalR_Watcher"); |
||
186 | |||
187 | Process[] watcher = Process.GetProcessesByName("SignalR_Server_Watcher"); |
||
188 | |||
189 | if ( watcher.Length > 0) |
||
190 | { |
||
191 | watcher[0].Kill(); |
||
192 | _serviceEventLogger.WriteEventEntry("singalR Server ::: signalr watcher Service kill ", EventLogEntryType.SuccessAudit); |
||
193 | |||
194 | |||
195 | _watcher.Refresh(); |
||
196 | |||
197 | if ( _watcher.Status == ServiceControllerStatus.Running || _watcher.Status == ServiceControllerStatus.StartPending) |
||
198 | { |
||
199 | _watcher.WaitForStatus(ServiceControllerStatus.Stopped); |
||
200 | } |
||
201 | |||
202 | _watcher.Start(new string[] { "Restart" }); |
||
203 | _watcher.WaitForStatus(ServiceControllerStatus.Running); |
||
204 | |||
205 | _serviceEventLogger.WriteEventEntry("singalR Server ::: signalr watcher Service Restart ", EventLogEntryType.SuccessAudit); |
||
206 | |||
207 | } |
||
208 | else |
||
209 | { |
||
210 | _serviceEventLogger.WriteEventEntry("singalR Server ::: signalr watcher Service Not Found", EventLogEntryType.Error); |
||
211 | } |
||
212 | |||
213 | } |
||
214 | |||
215 | public static void AllStart_Service() |
||
216 | { |
||
217 | |||
218 | ConvertDep(); |
||
219 | FinalDep(); |
||
220 | Startup.StartServer(); |
||
221 | |||
222 | _serviceEventLogger.WriteEventEntry(" AllStart_Service ", EventLogEntryType.SuccessAudit); |
||
223 | |||
224 | } |
||
225 | |||
226 | |||
227 | public static void ConvertDep() |
||
228 | { |
||
229 | |||
230 | try |
||
231 | { |
||
232 | |||
233 | var convertMapper = new ModelToTableMapper<CONVERTER_DOC>(); |
||
234 | convertMapper.AddMapping(s => s.CURRENT_PAGE, "CURRENT_PAGE"); |
||
235 | |||
236 | _convertTableDependency = new SqlTableDependency<CONVERTER_DOC>(entityConnString, "CONVERTER_DOC", convertMapper); |
||
237 | |||
238 | // Registration Dependency Event |
||
239 | _convertTableDependency.OnError += SqlTableDependency_OnError; |
||
240 | _convertTableDependency.OnChanged += Convert_SqlTableDependency_Changed; |
||
241 | |||
242 | _convertTableDependency.Start(); |
||
243 | |||
244 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name + " start", EventLogEntryType.SuccessAudit); |
||
245 | |||
246 | } |
||
247 | catch (Exception ex) |
||
248 | { |
||
249 | _serviceEventLogger.WriteEventEntry("SQL Dependency ERROR " + MethodBase.GetCurrentMethod().Name + " : " + ex.Message, EventLogEntryType.Error); |
||
250 | DependencyRestart(); |
||
251 | throw; |
||
252 | } |
||
253 | |||
254 | } |
||
255 | |||
256 | public static void FinalDep() |
||
257 | { |
||
258 | |||
259 | try |
||
260 | { |
||
261 | var finalMapper = new ModelToTableMapper<FINAL_PDF>(); |
||
262 | finalMapper.AddMapping(s => s.CURRENT_PAGE, "CURRENT_PAGE"); |
||
263 | |||
264 | _finalTableDependency = new SqlTableDependency<FINAL_PDF>(entityConnString, "FINAL_PDF", finalMapper); |
||
265 | |||
266 | _finalTableDependency.OnError += SqlTableDependency_OnError; |
||
267 | _finalTableDependency.OnChanged += Final_SqlTableDependency_Changed; |
||
268 | |||
269 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name + " start", EventLogEntryType.SuccessAudit); |
||
270 | |||
271 | _finalTableDependency.Start(); |
||
272 | } |
||
273 | catch (Exception ex) |
||
274 | { |
||
275 | _serviceEventLogger.WriteEventEntry("SQL Dependency ERROR " + MethodBase.GetCurrentMethod().Name + " : " + ex.Message, EventLogEntryType.Error); |
||
276 | DependencyRestart(); |
||
277 | throw; |
||
278 | } |
||
279 | |||
280 | } |
||
281 | |||
282 | public static void SqlTableDependency_OnError(object sender, ErrorEventArgs e) |
||
283 | { |
||
284 | _serviceEventLogger.WriteEventEntry(" SQL Dependency ERROR " + MethodBase.GetCurrentMethod().Name + " :: " + e.Message, EventLogEntryType.Error); |
||
285 | DependencyRestart(); |
||
286 | throw e.Error; |
||
287 | } |
||
288 | |||
289 | #endregion |
||
290 | |||
291 | #region Detection change data |
||
292 | |||
293 | /// <summary> |
||
294 | /// 컨버팅 테이블 데이터 변경에 대한 이벤트 감지 메서드 |
||
295 | /// </summary> |
||
296 | /// <param name="sender"></param> |
||
297 | /// <param name="e"></param> |
||
298 | public static void Convert_SqlTableDependency_Changed(object sender, RecordChangedEventArgs<CONVERTER_DOC> e) |
||
299 | { |
||
300 | try |
||
301 | { |
||
302 | |||
303 | string changeType = e.ChangeType.ToString(); |
||
304 | DetectedMakrusConvertTable(e.ChangeType.ToString(), e.Entity); |
||
305 | |||
306 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name + " changeType : " + changeType, EventLogEntryType.SuccessAudit); |
||
307 | |||
308 | } |
||
309 | catch (Exception ex) |
||
310 | { |
||
311 | _serviceEventLogger.WriteEventEntry("Convert_SqlTableDependency_Changed Error : " + ex, EventLogEntryType.Error); |
||
312 | throw; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /// <summary> |
||
317 | /// 파이널 테이블 데이터 변경에 대한 이벤트 감지 메서드 |
||
318 | /// </summary> |
||
319 | /// <param name="sender"></param> |
||
320 | /// <param name="e"></param> |
||
321 | public static void Final_SqlTableDependency_Changed(object sender, RecordChangedEventArgs<FINAL_PDF> e) |
||
322 | { |
||
323 | try |
||
324 | { |
||
325 | string changeType = e.ChangeType.ToString(); |
||
326 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name + " changeType : " + changeType, EventLogEntryType.SuccessAudit); |
||
327 | |||
328 | DetectedMakrusFinalTable(changeType, e.Entity); |
||
329 | |||
330 | } |
||
331 | catch (Exception ex) |
||
332 | { |
||
333 | _serviceEventLogger.WriteEventEntry("Final_SqlTableDependency_Changed Error : " + ex, EventLogEntryType.Error); |
||
334 | throw; |
||
335 | } |
||
336 | } |
||
337 | |||
338 | #endregion |
||
339 | |||
340 | #region Markus hub invoke method |
||
341 | |||
342 | /// <summary> |
||
343 | /// 컨버팅 테이블에 대한 허브 invoke 메서드 |
||
344 | /// </summary> |
||
345 | /// <param name="type"></param> |
||
346 | /// <param name="data"></param> |
||
347 | public static void DetectedMakrusConvertTable(string type, CONVERTER_DOC data) |
||
348 | { |
||
349 | |||
350 | using (var hubConn = new HubConnection(hubBaseURL)) |
||
351 | { |
||
352 | |||
353 | var hub = hubConn.CreateHubProxy(hubName); |
||
354 | |||
355 | hubConn.Start().Wait(); |
||
356 | |||
357 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name, EventLogEntryType.SuccessAudit); |
||
358 | |||
359 | if (type == detectedType.DataInsert) |
||
360 | { |
||
361 | hub.Invoke(convSetting.InvokeInsert , data); |
||
362 | } |
||
363 | else if (type == detectedType.DataUpdate) |
||
364 | { |
||
365 | hub.Invoke(convSetting.InvokeChange, data); |
||
366 | } |
||
367 | else if (type == detectedType.DataDelete) |
||
368 | { |
||
369 | hub.Invoke(convSetting.InvokeDelete, data); |
||
370 | } |
||
371 | |||
372 | } |
||
373 | |||
374 | } |
||
375 | |||
376 | /// <summary> |
||
377 | /// 파이널 테이블에 대한 허브 invoke 메서드 |
||
378 | /// </summary> |
||
379 | /// <param name="type"></param> |
||
380 | /// <param name="data"></param> |
||
381 | public static void DetectedMakrusFinalTable(string type, FINAL_PDF data) |
||
382 | { |
||
383 | |||
384 | using (var hubConn = new HubConnection(hubBaseURL)) |
||
385 | { |
||
386 | |||
387 | var hub = hubConn.CreateHubProxy(hubName); |
||
388 | |||
389 | hubConn.Start().Wait(); |
||
390 | |||
391 | _serviceEventLogger.WriteEventEntry(MethodBase.GetCurrentMethod().Name, EventLogEntryType.SuccessAudit); |
||
392 | |||
393 | if (type == detectedType.DataInsert) |
||
394 | { |
||
395 | hub.Invoke(finalSetting.InvokeInsert, data); |
||
396 | } |
||
397 | else if (type == detectedType.DataUpdate) |
||
398 | { |
||
399 | hub.Invoke(finalSetting.InvokeChange, data); |
||
400 | } |
||
401 | else if (type == detectedType.DataDelete) |
||
402 | { |
||
403 | hub.Invoke(finalSetting.InvokeDelete, data); |
||
404 | } |
||
405 | |||
406 | } |
||
407 | |||
408 | } |
||
409 | |||
410 | #endregion |
||
411 | } |
||
412 | } |