markus / ConvertService / ServiceBase / Markus.Service.Station / StationService / ServiceStationWCF.cs @ 06f13e11
이력 | 보기 | 이력해설 | 다운로드 (12.3 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 |
ConvertProcessStart(convertItem); |
150 |
result = true.ToString(); |
151 |
} |
152 |
else |
153 |
{ |
154 |
logger.Info($"ConvertAdd ConvertProcess Wait ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} "); |
155 |
result = true.ToString(); |
156 |
} |
157 |
} |
158 |
else |
159 |
{ |
160 |
PassConvertItem(ProjectNo, convertID); |
161 |
result = true.ToString(); |
162 |
} |
163 |
} |
164 |
catch (Exception ex) |
165 |
{ |
166 |
result = "Convert Process Start Error"; |
167 |
logger.Error($"ConvertAdd ConvertProcess call Error ProjectNo:{ProjectNo} convertID:{convertID} ", ex); |
168 |
} |
169 |
} |
170 |
else |
171 |
{ |
172 |
logger.Error($"ConvertAdd ConvertProcess call Error ProjectNo:{ProjectNo} convertID:{convertID} item Null "); |
173 |
result = "Get ConvertItem Error"; |
174 |
} |
175 |
} |
176 |
|
177 |
return result; |
178 |
} |
179 |
|
180 |
/// <summary> |
181 |
/// 각 ConvertProcess의 상태값을 저장 |
182 |
/// </summary> |
183 |
/// <param name="ConvertID"></param> |
184 |
/// <param name="status"></param> |
185 |
/// <param name="CurrentPage"></param> |
186 |
/// <param name="TotalPage"></param> |
187 |
/// <param name="Error"></param> |
188 |
/// <returns></returns> |
189 |
public bool ConvertProcessState(string ConvertID, int status, int CurrentPage, int TotalPage, string Error) |
190 |
{ |
191 |
var items = AliveConvertQueue.Where(x => x.ConvertID == ConvertID); |
192 |
|
193 |
if (items.Count() > 0) |
194 |
{ |
195 |
var item = items.First(); |
196 |
|
197 |
item.TotalPage = TotalPage; |
198 |
item.CurrentPageNo = CurrentPage; |
199 |
item.ConvertState = ((StatusCodeType)status).ToString(); |
200 |
} |
201 |
|
202 |
if (CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty()) |
203 |
{ |
204 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
205 |
{ |
206 |
if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error)) |
207 |
{ |
208 |
logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})"); |
209 |
} |
210 |
} |
211 |
} |
212 |
|
213 |
return true; |
214 |
} |
215 |
|
216 |
/// <summary> |
217 |
/// Convert Process 완료시 호출 |
218 |
/// </summary> |
219 |
/// <param name="ConvertID"></param> |
220 |
/// <param name="status"></param> |
221 |
/// <param name="CurrentPage"></param> |
222 |
/// <param name="TotalPage"></param> |
223 |
/// <param name="Error"></param> |
224 |
/// <returns></returns> |
225 |
public bool ConvertFinish(string ConvertID, int status, int CurrentPage, int TotalPage, string Error) |
226 |
{ |
227 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
228 |
{ |
229 |
if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error)) |
230 |
{ |
231 |
logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})"); |
232 |
} |
233 |
} |
234 |
|
235 |
var _convertItem = ServiceStation.AliveConvertQueue.Where(f => f.ConvertID == ConvertID); |
236 |
|
237 |
if (_convertItem.Count() > 0) |
238 |
{ |
239 |
ConvertFinish(_convertItem.First()); |
240 |
} |
241 |
|
242 |
return true; |
243 |
} |
244 |
|
245 |
/// <summary> |
246 |
/// 대기중인 Convert Item을 가져옴 |
247 |
/// </summary> |
248 |
/// <returns></returns> |
249 |
public List<ConvertItem> WaitConvertList() |
250 |
{ |
251 |
List<ConvertItem> result = new List<ConvertItem>(); |
252 |
|
253 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
254 |
{ |
255 |
result = database.GetConvertItems(Message.StatusCodeType.Wait).ToList(); |
256 |
} |
257 |
|
258 |
return result; |
259 |
} |
260 |
|
261 |
/// <summary> |
262 |
/// 현재 AliveConvertQueue에 있는 컨버터 리스트 |
263 |
/// </summary> |
264 |
/// <returns></returns> |
265 |
public List<ConvertItem> AliveConvertList() |
266 |
{ |
267 |
return ServiceStation.AliveConvertQueue; |
268 |
} |
269 |
|
270 |
/// <summary> |
271 |
/// 동시 작업 프로세스 갯수 변경 |
272 |
/// </summary> |
273 |
/// <param name="Value"></param> |
274 |
/// <returns></returns> |
275 |
public bool SettingMultiProcess(int Value) |
276 |
{ |
277 |
bool result = false; |
278 |
|
279 |
try |
280 |
{ |
281 |
MultiProcessCount = Value; |
282 |
|
283 |
var config = ConfigHelper.AppConfig(configFileName); |
284 |
|
285 |
config.SetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.CONVERT_SERVICE_PROCESS, Value.ToString()); |
286 |
config.Save(configFileName); |
287 |
|
288 |
result = true; |
289 |
} |
290 |
catch (Exception ex) |
291 |
{ |
292 |
logger.Error("SettingMultiProcess Error ", ex); |
293 |
} |
294 |
|
295 |
return result; |
296 |
} |
297 |
|
298 |
/// <summary> |
299 |
/// WCF 시작 |
300 |
/// </summary> |
301 |
/// <returns></returns> |
302 |
public CommunicationState StartWcfService() |
303 |
{ |
304 |
CommunicationState result = CommunicationState.Faulted; |
305 |
|
306 |
try |
307 |
{ |
308 |
if (gServiceHostAddress != null) |
309 |
{ |
310 |
gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress); |
311 |
} |
312 |
|
313 |
if (gWcfServiceHost?.State != CommunicationState.Faulted) |
314 |
{ |
315 |
result = gWcfServiceHost.State; |
316 |
} |
317 |
} |
318 |
catch (Exception ex) |
319 |
{ |
320 |
logger.Error("Start WCF Service Error ", ex); |
321 |
} |
322 |
|
323 |
return result; |
324 |
} |
325 |
|
326 |
/// <summary> |
327 |
/// WCF 중단 |
328 |
/// </summary> |
329 |
/// <returns></returns> |
330 |
private bool StopWcfService() |
331 |
{ |
332 |
bool result = false; |
333 |
try |
334 |
{ |
335 |
gWcfServiceHost.Abort(); |
336 |
result = true; |
337 |
} |
338 |
catch (Exception ex) |
339 |
{ |
340 |
logger.Error("Stop WCF Service Error ", ex); |
341 |
} |
342 |
|
343 |
return result; |
344 |
} |
345 |
|
346 |
public bool ReleaseConvertItems() |
347 |
{ |
348 |
System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems"); |
349 |
setDataBaseWaitingList(); |
350 |
System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems"); |
351 |
return true; |
352 |
} |
353 |
} |
354 |
} |