markus / ConvertService / ServiceBase / Markus.Service.Station / StationService / ServiceStationTask.cs @ 48e1ab34
이력 | 보기 | 이력해설 | 다운로드 (9.62 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 | 0157b158 | taeseongkim | using (DataBase.ConvertDatabase database = new DataBase.ConvertDatabase(MarkusDBConnectionString)) |
131 | 53c9637d | taeseongkim | { |
132 | 0157b158 | taeseongkim | var convertItems = database.GetWaitConvertItems(this.RunProjectList, StationServiceList.Sum(f=>f.Properties.PROCESS_COUNT)); |
133 | |||
134 | foreach (var convert in convertItems) |
||
135 | 53c9637d | taeseongkim | { |
136 | 0157b158 | taeseongkim | if (convert.STATUS > (int)StatusCodeType.None) |
137 | { |
||
138 | database.SetCleanUpItem(convert.ID); |
||
139 | } |
||
140 | |||
141 | PassConvertItem(convert.PROJECT_NO, convert.ID); |
||
142 | 53c9637d | taeseongkim | } |
143 | } |
||
144 | 0157b158 | taeseongkim | } |
145 | 53c9637d | taeseongkim | |
146 | 0157b158 | taeseongkim | private void PassConvertItem(string ProjectNo,string ConvertID) |
147 | { |
||
148 | foreach (var item in StationServiceList) |
||
149 | { |
||
150 | try |
||
151 | { |
||
152 | var aliveItems = item.Service.AliveConvertList(); |
||
153 | |||
154 | if (item.Properties.PROCESS_COUNT > aliveItems.Count()) |
||
155 | { |
||
156 | |||
157 | item.Service.ConvertAdd(ProjectNo, ConvertID); |
||
158 | |||
159 | logger.Info($"PassConvertItem - Service ID : {item.Properties.ID} ConvertID : {ConvertID}"); |
||
160 | } |
||
161 | } |
||
162 | catch (Exception ex) |
||
163 | { |
||
164 | logger.Error($"PassConvertItem - Service ID : {item.Properties.ID} ConvertID : {ConvertID}",ex); |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | private void CleanUpItems() |
||
170 | { |
||
171 | 53c9637d | taeseongkim | using (DataBase.ConvertDatabase database = new DataBase.ConvertDatabase(MarkusDBConnectionString)) |
172 | { |
||
173 | 0157b158 | taeseongkim | var items = database.GetConvertingItems(RunProjectList); |
174 | 53c9637d | taeseongkim | |
175 | 0157b158 | taeseongkim | List<StationService.ConvertItem> aliveItems = new List<StationService.ConvertItem>(); |
176 | |||
177 | foreach (var item in StationServiceList) |
||
178 | 53c9637d | taeseongkim | { |
179 | 0157b158 | taeseongkim | try |
180 | { |
||
181 | aliveItems.AddRange(item.Service.AliveConvertList()); |
||
182 | } |
||
183 | catch (Exception) |
||
184 | 53c9637d | taeseongkim | { |
185 | } |
||
186 | } |
||
187 | 0157b158 | taeseongkim | |
188 | foreach (var item in items) |
||
189 | 53c9637d | taeseongkim | { |
190 | 0157b158 | taeseongkim | if(aliveItems.Count(x=>x.ConvertID == item.ID) == 0) |
191 | { |
||
192 | database.SetCleanUpItem(item.ID); |
||
193 | } |
||
194 | 53c9637d | taeseongkim | } |
195 | } |
||
196 | } |
||
197 | |||
198 | 0157b158 | taeseongkim | |
199 | 53c9637d | taeseongkim | public void Stopprocess() |
200 | { |
||
201 | var process = Process.GetProcessesByName("Markus.Service.ConvertProcess"); |
||
202 | |||
203 | for (int i = process.Count() - 1; i >= 0 ; i--) |
||
204 | { |
||
205 | try |
||
206 | { |
||
207 | Console.WriteLine($"{i} Process Kill"); |
||
208 | process[i].Kill(); |
||
209 | } |
||
210 | catch (Exception ex) |
||
211 | { |
||
212 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /// <summary> |
||
218 | /// finish가 호출되고 살아있는 프로세스라고 추정됨 |
||
219 | /// </summary> |
||
220 | public void DeadLockProcessKill() |
||
221 | { |
||
222 | var process = Process.GetProcessesByName("Markus.Service.ConvertProcess"); |
||
223 | |||
224 | for (int i = process.Count() - 1; i >= 0; i--) |
||
225 | { |
||
226 | try |
||
227 | { |
||
228 | var commandLines = process[i].Arguments().CommandLine; |
||
229 | |||
230 | if (commandLines.Count() > 0) |
||
231 | { |
||
232 | if (ServiceStation.AliveConvertQueue.Count(f => f.ConvertID == commandLines[0]) == 0) |
||
233 | { |
||
234 | process[i].Kill(); |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | catch (Exception ex) |
||
239 | { |
||
240 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | private void ConvertFinish(ConvertItem convertitem) |
||
246 | { |
||
247 | try |
||
248 | { |
||
249 | System.Diagnostics.Debug.WriteLine("Convert Finish : " + convertitem.ConvertID); |
||
250 | |||
251 | System.Diagnostics.Debug.WriteLine("ServiceStation.AliveConvertQueue.Count() : " + ServiceStation.AliveConvertQueue.Count()); |
||
252 | |||
253 | ServiceStation.AliveConvertQueue.Remove(convertitem); |
||
254 | |||
255 | System.Diagnostics.Debug.WriteLine("ServiceStation.AliveConvertQueue.Count() : " + ServiceStation.AliveConvertQueue.Count()); |
||
256 | |||
257 | 0157b158 | taeseongkim | //if (ServiceStation.AliveConvertQueue.Count() < MultiProcessCount) |
258 | //{ |
||
259 | // setDataBaseWaitingList(); |
||
260 | //} |
||
261 | 53c9637d | taeseongkim | } |
262 | catch (Exception ex) |
||
263 | { |
||
264 | logger.Error("ConvertFinish Error",ex); |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | } |