markus / ConvertService / ServiceBase / Markus.Service.Station / StationService / ServiceStationTask.cs @ 150747cb
이력 | 보기 | 이력해설 | 다운로드 (15.4 KB)
1 | 53c9637d | taeseongkim | using Markus.Service.Interface; |
---|---|---|---|
2 | using Markus.Message; |
||
3 | using System; |
||
4 | using System.Collections.Generic; |
||
5 | using System.Diagnostics; |
||
6 | using System.Linq; |
||
7 | using System.Text; |
||
8 | using System.Threading; |
||
9 | using System.Threading.Tasks; |
||
10 | using System.Management; |
||
11 | using static Markus.Service.Extensions.Encrypt; |
||
12 | using Markus.Service.Extensions; |
||
13 | using Markus.Service.Helper; |
||
14 | |||
15 | namespace Markus.Service |
||
16 | { |
||
17 | /// <summary> |
||
18 | /// 컨버터 큐 처리 |
||
19 | /// </summary> |
||
20 | public partial class ServiceStation |
||
21 | { |
||
22 | /// <summary> |
||
23 | /// 컨버터 실행중인 item |
||
24 | /// </summary> |
||
25 | private static List<ConvertItem> AliveConvertQueue = new List<ConvertItem>(); |
||
26 | |||
27 | /// <summary> |
||
28 | /// 컨버터 프로세스 실행 |
||
29 | /// </summary> |
||
30 | /// <param name="convertitem"></param> |
||
31 | public bool ConvertProcessStart(ConvertItem convertitem) |
||
32 | { |
||
33 | bool result = false; |
||
34 | try |
||
35 | { |
||
36 | |||
37 | Process ConvertProcess = new Process(); |
||
38 | |||
39 | ProcessContext processSendData = new ProcessContext |
||
40 | { |
||
41 | ConvertID = convertitem.ConvertID, |
||
42 | ConnectionString = MarkusDBConnectionString, |
||
43 | ServiceStationUri = gServiceHostAddress.ToString(), |
||
44 | OriginFilePath = convertitem.OriginfilePath, |
||
45 | SaveDirectory = convertitem.ConvertPath, |
||
46 | TempDirectory = DownloadTempFolder, |
||
47 | 8feb21df | taeseongkim | ReleaseWorkMemory = ReleaseWorkMemory, |
48 | 53c9637d | taeseongkim | MultiThreadMaxPages = MultiThreadMaxPages, |
49 | MinFontSize = MinFontSize, |
||
50 | 2091a7e5 | taeseongkim | SendStatusInterval = SaveStatusInterval, |
51 | UseResolution = UseResolution |
||
52 | 53c9637d | taeseongkim | }; |
53 | |||
54 | var sendData = ObjectToBytesStringConvert.ObjectToBytesString(processSendData); |
||
55 | |||
56 | ProcessStartInfo startInfo = new ProcessStartInfo |
||
57 | { |
||
58 | UseShellExecute = false, |
||
59 | FileName = "Markus.Service.ConvertProcess.exe", |
||
60 | WindowStyle = ProcessWindowStyle.Hidden, |
||
61 | CreateNoWindow = true, |
||
62 | ErrorDialog = false, |
||
63 | RedirectStandardError = false, |
||
64 | Arguments = $"{convertitem.ConvertID.ToString()} {AESEncrypter.Encrypt(sendData)}" |
||
65 | //Arguments = $"{convertitem.ConvertID.ToString()} {convertitem.ProjectNumber} {AESEncrypter.Encrypt(MarkusDBConnectionString)} {gServiceHostAddress} {DownloadTempFolder} {MultiThreadMaxPages}" |
||
66 | }; |
||
67 | |||
68 | ConvertProcess.StartInfo = startInfo; |
||
69 | ConvertProcess.EnableRaisingEvents = false; |
||
70 | |||
71 | System.Diagnostics.Debug.WriteLine("convert process run : " + startInfo.Arguments); |
||
72 | |||
73 | |||
74 | if (ConvertProcess.Start()) |
||
75 | { |
||
76 | try |
||
77 | { |
||
78 | var processAffinity = ProcessorAffinityList.Except(AliveConvertQueue.Select(f => (long)f.ProcessorAffinity)); |
||
79 | |||
80 | if (processAffinity.Count() > 0) |
||
81 | { |
||
82 | convertitem.ProcessorAffinity = processAffinity.First(); |
||
83 | |||
84 | //int bitMask = 1 << (convertitem.ProcessorAffinity - 1); |
||
85 | //bitMask |= 1 << (anotherUserSelection - 1); / // 프로세스 두개 이상 선택 |
||
86 | |||
87 | ConvertProcess.ProcessorAffinity = new IntPtr(convertitem.ProcessorAffinity); |
||
88 | |||
89 | } |
||
90 | else |
||
91 | { |
||
92 | // 모두 사용중일때 점유율이 작은 걸로 사용 |
||
93 | var CurrentProcessAffinity = AliveConvertQueue.Select(f =>f.ProcessorAffinity).Distinct(); |
||
94 | |||
95 | var affinity = CurrentProcessAffinity.Min(); |
||
96 | |||
97 | convertitem.ProcessorAffinity = affinity; |
||
98 | ConvertProcess.ProcessorAffinity = new IntPtr(affinity); |
||
99 | } |
||
100 | } |
||
101 | catch (Exception ex) |
||
102 | { |
||
103 | System.Diagnostics.Debug.WriteLine(ex); |
||
104 | } |
||
105 | |||
106 | |||
107 | ServiceStation.AliveConvertQueue.Add(convertitem); |
||
108 | result = true; |
||
109 | } |
||
110 | } |
||
111 | catch (Exception ex) |
||
112 | { |
||
113 | throw new Exception("ConvertThread " + $"{convertitem.ConvertID.ToString()} {convertitem.ProjectNumber} {AESEncrypter.Encrypt(MarkusDBConnectionString)} {gServiceHostAddress} {DownloadTempFolder} {MultiThreadMaxPages}", ex.InnerException); |
||
114 | } |
||
115 | finally |
||
116 | { |
||
117 | //GC.WaitForPendingFinalizers(); |
||
118 | //GC.Collect(2); |
||
119 | //GC.Collect(2); |
||
120 | } |
||
121 | |||
122 | return result; |
||
123 | } |
||
124 | 0157b158 | taeseongkim | |
125 | 53c9637d | taeseongkim | /// <summary> |
126 | /// DB에 있는 대기중인 Item을 가져온다. |
||
127 | /// </summary> |
||
128 | public void setDataBaseWaitingList() |
||
129 | { |
||
130 | 1ae729e4 | taeseongkim | List<EntityModel.CONVERTER_DOC> convertItems = new List<EntityModel.CONVERTER_DOC>(); |
131 | |||
132 | 0157b158 | taeseongkim | using (DataBase.ConvertDatabase database = new DataBase.ConvertDatabase(MarkusDBConnectionString)) |
133 | 53c9637d | taeseongkim | { |
134 | 1ae729e4 | taeseongkim | convertItems = database.GetWaitConvertItems(this.RunProjectList, StationServiceList.Where(x=>x.IsOnline).Sum(f=>f.Properties.PROCESS_COUNT)).ToList(); |
135 | } |
||
136 | 0157b158 | taeseongkim | |
137 | 1ae729e4 | taeseongkim | foreach (var convert in convertItems) |
138 | { |
||
139 | //ReflashSubService(); |
||
140 | ff4b1e6e | taeseongkim | |
141 | 1ae729e4 | taeseongkim | if (convert.STATUS > (int)StatusCodeType.None) |
142 | { |
||
143 | using (DataBase.ConvertDatabase database = new DataBase.ConvertDatabase(MarkusDBConnectionString)) |
||
144 | 0157b158 | taeseongkim | { |
145 | 1ae729e4 | taeseongkim | database.SetCleanUpItem(convert.ID,1); |
146 | 0157b158 | taeseongkim | } |
147 | 53c9637d | taeseongkim | } |
148 | 1ae729e4 | taeseongkim | |
149 | PassConvertItem(convert.PROJECT_NO, convert.ID,convert.DOCUMENT_ID); |
||
150 | 53c9637d | taeseongkim | } |
151 | 1ae729e4 | taeseongkim | |
152 | 0157b158 | taeseongkim | } |
153 | 53c9637d | taeseongkim | |
154 | ff4b1e6e | taeseongkim | public bool IsDataBaseWaitingList(int overListCount) |
155 | { |
||
156 | bool result = false; |
||
157 | |||
158 | try |
||
159 | { |
||
160 | using (DataBase.ConvertDatabase database = new DataBase.ConvertDatabase(MarkusDBConnectionString)) |
||
161 | { |
||
162 | var convertItems = database.GetWaitConvertItems(this.RunProjectList, overListCount); |
||
163 | |||
164 | if (convertItems.Count() > 0) |
||
165 | { |
||
166 | result = true; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | catch (Exception ex) |
||
171 | { |
||
172 | logger.Error($"IsDataBaseWaitingList Error",ex); |
||
173 | } |
||
174 | |||
175 | return result; |
||
176 | } |
||
177 | |||
178 | 60723dc9 | taeseongkim | private async void ReflashSubServiceAsync() |
179 | 0157b158 | taeseongkim | { |
180 | 6f6e7dbf | taeseongkim | foreach (var subservice in StationServiceList) |
181 | 0157b158 | taeseongkim | { |
182 | try |
||
183 | { |
||
184 | 1ae729e4 | taeseongkim | subservice.IsOnline = await SytemNet.PingAsync(subservice.Properties.SERVICE_ADDRESS); |
185 | 0157b158 | taeseongkim | |
186 | 06f13e11 | taeseongkim | if (subservice.IsOnline) |
187 | { |
||
188 | ff4b1e6e | taeseongkim | var result = await subservice.Service.AliveConvertListAsync(); |
189 | 06f13e11 | taeseongkim | |
190 | subservice.ConvertItems = result.ToList(); |
||
191 | subservice.AliveCount = result.Count(); |
||
192 | } |
||
193 | else |
||
194 | { |
||
195 | logger.Error($"Connection Error {subservice.Properties.SERVICE_ADDRESS}"); |
||
196 | ff4b1e6e | taeseongkim | subservice.ConvertItems = new List<WcfClient.StationServiceTask.ConvertItem>(); |
197 | 06f13e11 | taeseongkim | subservice.AliveCount = subservice.Properties.PROCESS_COUNT; |
198 | } |
||
199 | 0157b158 | taeseongkim | } |
200 | catch (Exception ex) |
||
201 | { |
||
202 | 6f6e7dbf | taeseongkim | logger.Error($"ReflashSubService error - Service ID : {subservice.Properties.ID} ", ex); |
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | 06f13e11 | taeseongkim | |
208 | 0a89a17f | taeseongkim | /// <summary> |
209 | /// |
||
210 | /// </summary> |
||
211 | /// <param name="ProjectNo"></param> |
||
212 | /// <param name="ConvertID"></param> |
||
213 | /// <param name="UniqueKey">Document ID(문서의 유일키)</param> |
||
214 | ff4b1e6e | taeseongkim | private async void PassConvertItem(string ProjectNo,string ConvertID,string UniqueKey) |
215 | 6f6e7dbf | taeseongkim | { |
216 | |||
217 | try |
||
218 | { |
||
219 | 06f13e11 | taeseongkim | var stationList = StationServiceList.Where(x => x.IsOnline); |
220 | |||
221 | 0a89a17f | taeseongkim | if (stationList.SelectMany(x => x.ConvertItems).Count(c => c.ProjectNumber == ProjectNo && c.UniqueKey == UniqueKey) == 0) |
222 | 6f6e7dbf | taeseongkim | { |
223 | 06f13e11 | taeseongkim | var station = stationList.OrderByDescending(x => x.Properties.PROCESS_COUNT - x.AliveCount).FirstOrDefault(); |
224 | 6f6e7dbf | taeseongkim | |
225 | if (station != null) |
||
226 | { |
||
227 | 60723dc9 | taeseongkim | if (station.Properties.PROCESS_COUNT - station.AliveCount > 0) |
228 | ff4b1e6e | taeseongkim | { |
229 | 60723dc9 | taeseongkim | System.Diagnostics.Debug.WriteLine($"{station.Properties.SERVICE_ADDRESS} {station.Properties.PROCESS_COUNT}/{station.AliveCount}"); |
230 | var result = await station.Service.ConvertAddAsync(ProjectNo, ConvertID); |
||
231 | |||
232 | if (!result.IsNullOrEmpty()) |
||
233 | { |
||
234 | if (result.ToLower() == true.ToString().ToLower()) |
||
235 | { |
||
236 | station.AliveCount++; |
||
237 | } |
||
238 | } |
||
239 | else |
||
240 | { |
||
241 | System.Diagnostics.Debug.WriteLine("result : Nulll;"); |
||
242 | } |
||
243 | |||
244 | logger.Info($"PassConvertItem - Service ID : {station.Properties.ID} ConvertID : {ConvertID}"); |
||
245 | ff4b1e6e | taeseongkim | } |
246 | 6f6e7dbf | taeseongkim | } |
247 | 0157b158 | taeseongkim | } |
248 | } |
||
249 | 6f6e7dbf | taeseongkim | catch (Exception ex) |
250 | { |
||
251 | logger.Error($"setDataBaseWaitingList", ex); |
||
252 | } |
||
253 | 0157b158 | taeseongkim | } |
254 | |||
255 | ff4b1e6e | taeseongkim | /// <summary> |
256 | /// 컨버터 프로세스와 AiliveItems을 비교하여 AliveItems에 없는 경우 AliveItems을 제거 |
||
257 | /// </summary> |
||
258 | 06f13e11 | taeseongkim | private void CleanUpAliveQueueItems() |
259 | { |
||
260 | ff4b1e6e | taeseongkim | if (AliveConvertQueue.Count() > 0) |
261 | 06f13e11 | taeseongkim | { |
262 | ff4b1e6e | taeseongkim | var processList = Process.GetProcessesByName("Markus.Service.ConvertProcess"); |
263 | 06f13e11 | taeseongkim | |
264 | ff4b1e6e | taeseongkim | if (processList.Length == 0) |
265 | 06f13e11 | taeseongkim | { |
266 | ff4b1e6e | taeseongkim | AliveConvertQueue.Clear(); |
267 | System.Diagnostics.Debug.WriteLine("AliveConvertQueue.Clear()"); |
||
268 | } |
||
269 | else |
||
270 | { |
||
271 | var argumentList = processList.Select(f => f.Arguments().CommandLine).SelectMany(f => f); |
||
272 | |||
273 | 60723dc9 | taeseongkim | List<ConvertItem> convertItems = AliveConvertQueue; |
274 | |||
275 | try |
||
276 | 06f13e11 | taeseongkim | { |
277 | 1ae729e4 | taeseongkim | if (convertItems.Count() > 0) |
278 | ff4b1e6e | taeseongkim | { |
279 | 1ae729e4 | taeseongkim | for (int i = convertItems.Count - 1; i >= 0; --i) |
280 | 60723dc9 | taeseongkim | { |
281 | 1ae729e4 | taeseongkim | if (argumentList.Count(x => x == convertItems[i].ConvertID) == 0) |
282 | { |
||
283 | AliveConvertQueue.RemoveAt(i); |
||
284 | } |
||
285 | 60723dc9 | taeseongkim | } |
286 | ff4b1e6e | taeseongkim | } |
287 | 06f13e11 | taeseongkim | } |
288 | 60723dc9 | taeseongkim | catch (Exception ex) |
289 | { |
||
290 | System.Diagnostics.Debug.WriteLine(ex); |
||
291 | } |
||
292 | 06f13e11 | taeseongkim | } |
293 | } |
||
294 | } |
||
295 | |||
296 | ff4b1e6e | taeseongkim | /// <summary> |
297 | 60723dc9 | taeseongkim | /// AliveConvertQueue와 db를 비교하여 AliveConvertQueue에 없는 데이터를 초기화 하여 다시 컨버팅 |
298 | ff4b1e6e | taeseongkim | /// </summary> |
299 | private async void CleanUpDataBaseItems() |
||
300 | 0157b158 | taeseongkim | { |
301 | 150747cb | taeseongkim | try |
302 | 53c9637d | taeseongkim | { |
303 | 150747cb | taeseongkim | using (DataBase.ConvertDatabase database = new DataBase.ConvertDatabase(MarkusDBConnectionString)) |
304 | { |
||
305 | var items = database.GetConvertingItems(RunProjectList); |
||
306 | 53c9637d | taeseongkim | |
307 | 150747cb | taeseongkim | List< WcfClient.StationServiceTask.ConvertItem> aliveItems = new List<WcfClient.StationServiceTask.ConvertItem>(); |
308 | 0157b158 | taeseongkim | |
309 | 60723dc9 | taeseongkim | foreach (var item in StationServiceList.Where(x => x.IsOnline)) |
310 | 0157b158 | taeseongkim | { |
311 | ff4b1e6e | taeseongkim | var serviceItems = await item.Service.AliveConvertListAsync(); |
312 | aliveItems.AddRange(serviceItems); |
||
313 | 0157b158 | taeseongkim | } |
314 | 60723dc9 | taeseongkim | |
315 | foreach (var item in items) |
||
316 | 53c9637d | taeseongkim | { |
317 | 60723dc9 | taeseongkim | if (aliveItems.Count(x => x.ConvertID == item.ID) == 0) |
318 | { |
||
319 | 1ae729e4 | taeseongkim | database.SetCleanUpItem(item.ID,0); |
320 | 60723dc9 | taeseongkim | } |
321 | 53c9637d | taeseongkim | } |
322 | 150747cb | taeseongkim | |
323 | 53c9637d | taeseongkim | } |
324 | 150747cb | taeseongkim | } |
325 | catch (Exception ex) |
||
326 | { |
||
327 | throw new Exception("CleanUpDataBaseItems Error ", ex); |
||
328 | 53c9637d | taeseongkim | } |
329 | } |
||
330 | |||
331 | 0157b158 | taeseongkim | |
332 | 53c9637d | taeseongkim | public void Stopprocess() |
333 | { |
||
334 | var process = Process.GetProcessesByName("Markus.Service.ConvertProcess"); |
||
335 | |||
336 | for (int i = process.Count() - 1; i >= 0 ; i--) |
||
337 | { |
||
338 | try |
||
339 | { |
||
340 | Console.WriteLine($"{i} Process Kill"); |
||
341 | process[i].Kill(); |
||
342 | } |
||
343 | catch (Exception ex) |
||
344 | { |
||
345 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | |||
350 | /// <summary> |
||
351 | /// finish가 호출되고 살아있는 프로세스라고 추정됨 |
||
352 | /// </summary> |
||
353 | public void DeadLockProcessKill() |
||
354 | { |
||
355 | var process = Process.GetProcessesByName("Markus.Service.ConvertProcess"); |
||
356 | |||
357 | for (int i = process.Count() - 1; i >= 0; i--) |
||
358 | { |
||
359 | try |
||
360 | { |
||
361 | var commandLines = process[i].Arguments().CommandLine; |
||
362 | |||
363 | if (commandLines.Count() > 0) |
||
364 | { |
||
365 | if (ServiceStation.AliveConvertQueue.Count(f => f.ConvertID == commandLines[0]) == 0) |
||
366 | { |
||
367 | process[i].Kill(); |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | catch (Exception ex) |
||
372 | { |
||
373 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
374 | } |
||
375 | } |
||
376 | } |
||
377 | |||
378 | 60723dc9 | taeseongkim | private async void ConvertFinish(ConvertItem convertitem) |
379 | 53c9637d | taeseongkim | { |
380 | try |
||
381 | { |
||
382 | System.Diagnostics.Debug.WriteLine("Convert Finish : " + convertitem.ConvertID); |
||
383 | |||
384 | System.Diagnostics.Debug.WriteLine("ServiceStation.AliveConvertQueue.Count() : " + ServiceStation.AliveConvertQueue.Count()); |
||
385 | |||
386 | ServiceStation.AliveConvertQueue.Remove(convertitem); |
||
387 | |||
388 | System.Diagnostics.Debug.WriteLine("ServiceStation.AliveConvertQueue.Count() : " + ServiceStation.AliveConvertQueue.Count()); |
||
389 | |||
390 | 06f13e11 | taeseongkim | if (IsStation) |
391 | { |
||
392 | 1ae729e4 | taeseongkim | //if (!IsReleaseItems) |
393 | //{ |
||
394 | // System.Diagnostics.Debug.WriteLine("ConvertFinish ReleaseItems call"); |
||
395 | // ReleaseItems(); |
||
396 | //} |
||
397 | 06f13e11 | taeseongkim | } |
398 | else |
||
399 | { |
||
400 | if (StationClient != null) |
||
401 | { |
||
402 | 60723dc9 | taeseongkim | await StationClient.ReleaseConvertItemsAsync(); |
403 | 06f13e11 | taeseongkim | } |
404 | } |
||
405 | 0157b158 | taeseongkim | //if (ServiceStation.AliveConvertQueue.Count() < MultiProcessCount) |
406 | //{ |
||
407 | // setDataBaseWaitingList(); |
||
408 | //} |
||
409 | 53c9637d | taeseongkim | } |
410 | catch (Exception ex) |
||
411 | { |
||
412 | logger.Error("ConvertFinish Error",ex); |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | } |