markus / ConvertService / ServiceBase / Markus.Service.Station / StationService / ServiceStationWCF.cs @ 0a89a17f
이력 | 보기 | 이력해설 | 다운로드 (12.4 KB)
1 |
using Markus.Message; |
---|---|
2 |
using Markus.Service.Extensions; |
3 |
using Markus.Service.Interface; |
4 |
using Markus.Service.IWcfService; |
5 |
using System; |
6 |
using System.Collections.Generic; |
7 |
using System.Diagnostics; |
8 |
using System.Linq; |
9 |
using System.ServiceModel; |
10 |
using System.ServiceModel.Activation; |
11 |
using System.ServiceModel.Web; |
12 |
using System.Text; |
13 |
using System.Threading.Tasks; |
14 |
|
15 |
namespace Markus.Service |
16 |
{ |
17 |
/// <summary> |
18 |
/// web service 목록 |
19 |
/// </summary> |
20 |
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] |
21 |
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)] |
22 |
public partial class ServiceStation : IStationService |
23 |
{ |
24 |
/// <summary> |
25 |
/// DB에 Convrert Item을 추가 하고 호출 |
26 |
/// CovnertWebService에서 호출시 사용 |
27 |
/// </summary> |
28 |
/// <param name="ProjectNo"></param> |
29 |
/// <param name="ConvertID"></param> |
30 |
/// <returns></returns> |
31 |
public string ConvertAdd(string ProjectNo, string ConvertID) |
32 |
{ |
33 |
string result = false.ToString(); |
34 |
|
35 |
try |
36 |
{ |
37 |
result = ConvertProcessAdd(ProjectNo, ConvertID); |
38 |
|
39 |
logger.Info($"ConvertAdd Call Item ProjectNo : {ProjectNo} ConvertID : {ConvertID} result : {result}"); |
40 |
} |
41 |
catch (Exception ex) |
42 |
{ |
43 |
result = $"ConvertAdd Error {ConvertID}"; |
44 |
logger.Error($"ConvertAdd Error- {ConvertID}", ex); |
45 |
} |
46 |
|
47 |
return result; |
48 |
} |
49 |
|
50 |
/// <summary> |
51 |
/// 별도의 호출 사용 |
52 |
/// </summary> |
53 |
/// <param name="ProjectNo"></param> |
54 |
/// <param name="originfilePath"></param> |
55 |
/// <param name="DocumentID"></param> |
56 |
/// <returns></returns> |
57 |
public string ConvertMenualAdd(string ProjectNo, string originfilePath, string DocumentID) |
58 |
{ |
59 |
string result = false.ToString(); |
60 |
string convertID = new Guid().CreateUniqueGuid().ToString().Replace("-", ""); |
61 |
|
62 |
try |
63 |
{ |
64 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
65 |
{ |
66 |
if (!database.SetConvertDoc(ProjectNo, convertID, originfilePath, DocumentID)) |
67 |
{ |
68 |
logger.Error($"Set Convert Document Error - ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}"); |
69 |
} |
70 |
else |
71 |
{ |
72 |
result = ConvertProcessAdd(ProjectNo, convertID); |
73 |
} |
74 |
} |
75 |
} |
76 |
catch (Exception ex) |
77 |
{ |
78 |
result = "DataBase SetConvert Error"; |
79 |
logger.Error($"ConvertMenualAdd Error- database.SetConvertDoc ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}", ex); |
80 |
} |
81 |
|
82 |
return result; |
83 |
} |
84 |
|
85 |
/// <summary> |
86 |
/// ConvertItem의 상태 |
87 |
/// </summary> |
88 |
/// <param name="ProjectNo"></param> |
89 |
/// <param name="DocumentID"></param> |
90 |
/// <returns></returns> |
91 |
public Item GetConvertItem(string ProjectNo, string DocumentID) |
92 |
{ |
93 |
Item result = null; |
94 |
|
95 |
try |
96 |
{ |
97 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
98 |
{ |
99 |
var item = database.GetConvertItembyDocumentID(ProjectNo, DocumentID); |
100 |
|
101 |
if (item != null) |
102 |
{ |
103 |
result = |
104 |
new Item |
105 |
{ |
106 |
DocumentID = item.UniqueKey, |
107 |
Status = item.ConvertState, |
108 |
ProjectNo = item.ProjectNumber, |
109 |
TotalPage = item.TotalPage, |
110 |
CurrentPageNo = item.CurrentPageNo, |
111 |
PdfPath = item.OriginfilePath, |
112 |
ConvertPath = item.ConvertPath |
113 |
}; |
114 |
|
115 |
} |
116 |
} |
117 |
} |
118 |
catch (Exception ex) |
119 |
{ |
120 |
throw new FaultException("Get ConvertItem Error"); |
121 |
} |
122 |
|
123 |
return result; |
124 |
} |
125 |
|
126 |
/// <summary> |
127 |
/// Convert Item을 DB에 추가 하고 프로세스 실행시 호출 |
128 |
/// </summary> |
129 |
/// <param name="ProjectNo"></param> |
130 |
/// <param name="convertID"></param> |
131 |
/// <returns></returns> |
132 |
private string ConvertProcessAdd(string ProjectNo, string convertID) |
133 |
{ |
134 |
string result = false.ToString(); |
135 |
|
136 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
137 |
{ |
138 |
var convertItem = database.GetConvertItem(ProjectNo, convertID); |
139 |
|
140 |
/// 순서별 Convert Item 처리 |
141 |
if (convertItem != null) |
142 |
{ |
143 |
try |
144 |
{ |
145 |
if (AliveConvertQueue.Count(c => c.UniqueKey == convertItem.UniqueKey) == 0) |
146 |
{ |
147 |
if (ServiceStation.AliveConvertQueue.Count() < MultiProcessCount) |
148 |
{ |
149 |
convertItem.ServiceID = this.ServiceID; |
150 |
ConvertProcessStart(convertItem); |
151 |
result = true.ToString(); |
152 |
} |
153 |
else |
154 |
{ |
155 |
logger.Info($"ConvertAdd ConvertProcess Wait ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} "); |
156 |
result = true.ToString(); |
157 |
} |
158 |
} |
159 |
else |
160 |
{ |
161 |
PassConvertItem(ProjectNo, convertID, convertItem.UniqueKey); |
162 |
result = true.ToString(); |
163 |
} |
164 |
} |
165 |
catch (Exception ex) |
166 |
{ |
167 |
result = "Convert Process Start Error"; |
168 |
logger.Error($"ConvertAdd ConvertProcess call Error ProjectNo:{ProjectNo} convertID:{convertID} ", ex); |
169 |
} |
170 |
} |
171 |
else |
172 |
{ |
173 |
logger.Error($"ConvertAdd ConvertProcess call Error ProjectNo:{ProjectNo} convertID:{convertID} item Null "); |
174 |
result = "Get ConvertItem Error"; |
175 |
} |
176 |
} |
177 |
|
178 |
return result; |
179 |
} |
180 |
|
181 |
/// <summary> |
182 |
/// 각 ConvertProcess의 상태값을 저장 |
183 |
/// </summary> |
184 |
/// <param name="ConvertID"></param> |
185 |
/// <param name="status"></param> |
186 |
/// <param name="CurrentPage"></param> |
187 |
/// <param name="TotalPage"></param> |
188 |
/// <param name="Error"></param> |
189 |
/// <returns></returns> |
190 |
public bool ConvertProcessState(string ConvertID, int status, int CurrentPage, int TotalPage, string Error) |
191 |
{ |
192 |
var items = AliveConvertQueue.Where(x => x.ConvertID == ConvertID); |
193 |
|
194 |
if (items.Count() > 0) |
195 |
{ |
196 |
var item = items.First(); |
197 |
|
198 |
item.TotalPage = TotalPage; |
199 |
item.CurrentPageNo = CurrentPage; |
200 |
item.ConvertState = ((StatusCodeType)status).ToString(); |
201 |
} |
202 |
|
203 |
if (CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty()) |
204 |
{ |
205 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
206 |
{ |
207 |
if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error)) |
208 |
{ |
209 |
logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})"); |
210 |
} |
211 |
} |
212 |
} |
213 |
|
214 |
return true; |
215 |
} |
216 |
|
217 |
/// <summary> |
218 |
/// Convert Process 완료시 호출 |
219 |
/// </summary> |
220 |
/// <param name="ConvertID"></param> |
221 |
/// <param name="status"></param> |
222 |
/// <param name="CurrentPage"></param> |
223 |
/// <param name="TotalPage"></param> |
224 |
/// <param name="Error"></param> |
225 |
/// <returns></returns> |
226 |
public bool ConvertFinish(string ConvertID, int status, int CurrentPage, int TotalPage, string Error) |
227 |
{ |
228 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
229 |
{ |
230 |
if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error)) |
231 |
{ |
232 |
logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})"); |
233 |
} |
234 |
} |
235 |
|
236 |
var _convertItem = ServiceStation.AliveConvertQueue.Where(f => f.ConvertID == ConvertID); |
237 |
|
238 |
if (_convertItem.Count() > 0) |
239 |
{ |
240 |
ConvertFinish(_convertItem.First()); |
241 |
} |
242 |
|
243 |
return true; |
244 |
} |
245 |
|
246 |
/// <summary> |
247 |
/// 대기중인 Convert Item을 가져옴 |
248 |
/// </summary> |
249 |
/// <returns></returns> |
250 |
public List<ConvertItem> WaitConvertList() |
251 |
{ |
252 |
List<ConvertItem> result = new List<ConvertItem>(); |
253 |
|
254 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
255 |
{ |
256 |
result = database.GetConvertItems(Message.StatusCodeType.Wait).ToList(); |
257 |
} |
258 |
|
259 |
return result; |
260 |
} |
261 |
|
262 |
/// <summary> |
263 |
/// 현재 AliveConvertQueue에 있는 컨버터 리스트 |
264 |
/// </summary> |
265 |
/// <returns></returns> |
266 |
public List<ConvertItem> AliveConvertList() |
267 |
{ |
268 |
return ServiceStation.AliveConvertQueue; |
269 |
} |
270 |
|
271 |
/// <summary> |
272 |
/// 동시 작업 프로세스 갯수 변경 |
273 |
/// </summary> |
274 |
/// <param name="Value"></param> |
275 |
/// <returns></returns> |
276 |
public bool SettingMultiProcess(int Value) |
277 |
{ |
278 |
bool result = false; |
279 |
|
280 |
try |
281 |
{ |
282 |
MultiProcessCount = Value; |
283 |
|
284 |
var config = ConfigHelper.AppConfig(configFileName); |
285 |
|
286 |
config.SetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.CONVERT_SERVICE_PROCESS, Value.ToString()); |
287 |
config.Save(configFileName); |
288 |
|
289 |
result = true; |
290 |
} |
291 |
catch (Exception ex) |
292 |
{ |
293 |
logger.Error("SettingMultiProcess Error ", ex); |
294 |
} |
295 |
|
296 |
return result; |
297 |
} |
298 |
|
299 |
/// <summary> |
300 |
/// WCF 시작 |
301 |
/// </summary> |
302 |
/// <returns></returns> |
303 |
public CommunicationState StartWcfService() |
304 |
{ |
305 |
CommunicationState result = CommunicationState.Faulted; |
306 |
|
307 |
try |
308 |
{ |
309 |
if (gServiceHostAddress != null) |
310 |
{ |
311 |
gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress); |
312 |
} |
313 |
|
314 |
if (gWcfServiceHost?.State != CommunicationState.Faulted) |
315 |
{ |
316 |
result = gWcfServiceHost.State; |
317 |
} |
318 |
} |
319 |
catch (Exception ex) |
320 |
{ |
321 |
logger.Error("Start WCF Service Error ", ex); |
322 |
} |
323 |
|
324 |
return result; |
325 |
} |
326 |
|
327 |
/// <summary> |
328 |
/// WCF 중단 |
329 |
/// </summary> |
330 |
/// <returns></returns> |
331 |
private bool StopWcfService() |
332 |
{ |
333 |
bool result = false; |
334 |
try |
335 |
{ |
336 |
gWcfServiceHost.Abort(); |
337 |
result = true; |
338 |
} |
339 |
catch (Exception ex) |
340 |
{ |
341 |
logger.Error("Stop WCF Service Error ", ex); |
342 |
} |
343 |
|
344 |
return result; |
345 |
} |
346 |
|
347 |
public bool ReleaseConvertItems() |
348 |
{ |
349 |
System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems"); |
350 |
setDataBaseWaitingList(); |
351 |
System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems"); |
352 |
return true; |
353 |
} |
354 |
} |
355 |
} |