markus / ConvertService / ServiceBase / Markus.Service.Station / StationService / ServiceStationWCF.cs @ 2c629eec
이력 | 보기 | 이력해설 | 다운로드 (12.1 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.PerCall, 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 |
var convertItem = database.GetConvertItembyDocumentID(ProjectNo, DocumentID); |
67 |
|
68 |
if(convertItem != null) |
69 |
{ |
70 |
var aliveItems = AliveConvertQueue.Where(x => x.ConvertID == convertItem.ConvertID); |
71 |
} |
72 |
|
73 |
if (!database.SetConvertDoc(ProjectNo, convertID, originfilePath, DocumentID)) |
74 |
{ |
75 |
logger.Error($"Set Convert Document Error - ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}"); |
76 |
} |
77 |
else |
78 |
{ |
79 |
result = ConvertProcessAdd(ProjectNo, convertID); |
80 |
} |
81 |
} |
82 |
} |
83 |
catch (Exception ex) |
84 |
{ |
85 |
result = "DataBase SetConvert Error"; |
86 |
logger.Error($"ConvertMenualAdd Error- database.SetConvertDoc ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}", ex); |
87 |
} |
88 |
|
89 |
return result; |
90 |
} |
91 |
|
92 |
/// <summary> |
93 |
/// ConvertItem의 상태 |
94 |
/// </summary> |
95 |
/// <param name="ProjectNo"></param> |
96 |
/// <param name="DocumentID"></param> |
97 |
/// <returns></returns> |
98 |
public Item GetConvertItem(string ProjectNo, string DocumentID) |
99 |
{ |
100 |
Item result = null; |
101 |
|
102 |
try |
103 |
{ |
104 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
105 |
{ |
106 |
var item = database.GetConvertItembyDocumentID(ProjectNo, DocumentID); |
107 |
|
108 |
if (item != null) |
109 |
{ |
110 |
result = |
111 |
new Item |
112 |
{ |
113 |
DocumentID = item.UniqueKey, |
114 |
Status = item.ConvertState, |
115 |
ProjectNo = item.ProjectNumber, |
116 |
TotalPage = item.TotalPage, |
117 |
CurrentPageNo = item.CurrentPageNo, |
118 |
PdfPath = item.OriginfilePath, |
119 |
ConvertPath = item.ConvertPath |
120 |
}; |
121 |
|
122 |
} |
123 |
} |
124 |
} |
125 |
catch (Exception ex) |
126 |
{ |
127 |
throw new FaultException("Get ConvertItem Error"); |
128 |
} |
129 |
|
130 |
return result; |
131 |
} |
132 |
|
133 |
/// <summary> |
134 |
/// Convert Item을 DB에 추가 하고 프로세스 실행시 호출 |
135 |
/// </summary> |
136 |
/// <param name="ProjectNo"></param> |
137 |
/// <param name="convertID"></param> |
138 |
/// <returns></returns> |
139 |
private string ConvertProcessAdd(string ProjectNo, string convertID) |
140 |
{ |
141 |
string result = false.ToString(); |
142 |
|
143 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
144 |
{ |
145 |
var convertItem = database.GetConvertItem(ProjectNo, convertID); |
146 |
|
147 |
/// 순서별 Convert Item 처리 |
148 |
if (convertItem != null) |
149 |
{ |
150 |
try |
151 |
{ |
152 |
if (AliveConvertQueue.Count(c => c.UniqueKey == convertItem.UniqueKey) == 0) |
153 |
{ |
154 |
if (ServiceStation.AliveConvertQueue.Count() < ServiceProperty.PROCESS_COUNT) |
155 |
{ |
156 |
convertItem.ServiceID = this.ServiceID; |
157 |
ConvertProcessStart(convertItem); |
158 |
result = true.ToString(); |
159 |
} |
160 |
else |
161 |
{ |
162 |
PassConvertItem(ProjectNo, convertID, convertItem.UniqueKey); |
163 |
|
164 |
logger.Info($"ConvertAdd ConvertProcess Wait ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} "); |
165 |
result = true.ToString(); |
166 |
} |
167 |
} |
168 |
else |
169 |
{ |
170 |
result = true.ToString(); |
171 |
} |
172 |
} |
173 |
catch (Exception ex) |
174 |
{ |
175 |
result = "Convert Process Start Error"; |
176 |
logger.Error($"ConvertAdd ConvertProcess call Error ProjectNo:{ProjectNo} convertID:{convertID} ", ex); |
177 |
} |
178 |
} |
179 |
else |
180 |
{ |
181 |
logger.Error($"ConvertAdd ConvertProcess call Error ProjectNo:{ProjectNo} convertID:{convertID} item Null "); |
182 |
result = "Get ConvertItem Error"; |
183 |
} |
184 |
} |
185 |
|
186 |
return result; |
187 |
} |
188 |
|
189 |
/// <summary> |
190 |
/// 각 ConvertProcess의 상태값을 저장 |
191 |
/// </summary> |
192 |
/// <param name="ConvertID"></param> |
193 |
/// <param name="status"></param> |
194 |
/// <param name="CurrentPage"></param> |
195 |
/// <param name="TotalPage"></param> |
196 |
/// <param name="Error"></param> |
197 |
/// <returns></returns> |
198 |
public bool ConvertProcessState(string ConvertID, int status, int CurrentPage, int TotalPage, string Error) |
199 |
{ |
200 |
var items = AliveConvertQueue.Where(x => x.ConvertID == ConvertID); |
201 |
|
202 |
if (items.Count() > 0) |
203 |
{ |
204 |
var item = items.First(); |
205 |
|
206 |
item.TotalPage = TotalPage; |
207 |
item.CurrentPageNo = CurrentPage; |
208 |
item.ConvertState = ((StatusCodeType)status).ToString(); |
209 |
} |
210 |
|
211 |
if (CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty()) |
212 |
{ |
213 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
214 |
{ |
215 |
if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error)) |
216 |
{ |
217 |
logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})"); |
218 |
} |
219 |
} |
220 |
} |
221 |
|
222 |
return true; |
223 |
} |
224 |
|
225 |
/// <summary> |
226 |
/// Convert Process 완료시 호출 |
227 |
/// </summary> |
228 |
/// <param name="ConvertID"></param> |
229 |
/// <param name="status"></param> |
230 |
/// <param name="CurrentPage"></param> |
231 |
/// <param name="TotalPage"></param> |
232 |
/// <param name="Error"></param> |
233 |
/// <returns></returns> |
234 |
public bool ConvertFinish(string ConvertID, int status, int CurrentPage, int TotalPage, string Error) |
235 |
{ |
236 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
237 |
{ |
238 |
if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error)) |
239 |
{ |
240 |
logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})"); |
241 |
} |
242 |
} |
243 |
|
244 |
var _convertItem = ServiceStation.AliveConvertQueue.Where(f => f.ConvertID == ConvertID); |
245 |
|
246 |
if (_convertItem.Count() > 0) |
247 |
{ |
248 |
ConvertFinish(_convertItem.First()); |
249 |
} |
250 |
|
251 |
return true; |
252 |
} |
253 |
|
254 |
/// <summary> |
255 |
/// 대기중인 Convert Item을 가져옴 |
256 |
/// </summary> |
257 |
/// <returns></returns> |
258 |
public List<ConvertItem> WaitConvertList() |
259 |
{ |
260 |
List<ConvertItem> result = new List<ConvertItem>(); |
261 |
|
262 |
using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString)) |
263 |
{ |
264 |
result = database.GetConvertItems(Message.StatusCodeType.Wait).ToList(); |
265 |
} |
266 |
|
267 |
return result; |
268 |
} |
269 |
|
270 |
/// <summary> |
271 |
/// 현재 AliveConvertQueue에 있는 컨버터 리스트 |
272 |
/// </summary> |
273 |
/// <returns></returns> |
274 |
public List<ConvertItem> AliveConvertList() |
275 |
{ |
276 |
return ServiceStation.AliveConvertQueue; |
277 |
} |
278 |
|
279 |
/// <summary> |
280 |
/// WCF 시작 |
281 |
/// </summary> |
282 |
/// <returns></returns> |
283 |
public CommunicationState StartWcfService() |
284 |
{ |
285 |
CommunicationState result = CommunicationState.Faulted; |
286 |
|
287 |
try |
288 |
{ |
289 |
if (gServiceHostAddress != null) |
290 |
{ |
291 |
gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress); |
292 |
} |
293 |
|
294 |
if (gWcfServiceHost?.State != CommunicationState.Faulted) |
295 |
{ |
296 |
result = gWcfServiceHost.State; |
297 |
} |
298 |
} |
299 |
catch (Exception ex) |
300 |
{ |
301 |
logger.Error("Start WCF Service Error ", ex); |
302 |
} |
303 |
|
304 |
return result; |
305 |
} |
306 |
|
307 |
/// <summary> |
308 |
/// WCF 중단 |
309 |
/// </summary> |
310 |
/// <returns></returns> |
311 |
private bool StopWcfService() |
312 |
{ |
313 |
bool result = false; |
314 |
try |
315 |
{ |
316 |
gWcfServiceHost.Abort(); |
317 |
result = true; |
318 |
} |
319 |
catch (Exception ex) |
320 |
{ |
321 |
logger.Error("Stop WCF Service Error ", ex); |
322 |
} |
323 |
|
324 |
return result; |
325 |
} |
326 |
|
327 |
public bool ReleaseConvertItems() |
328 |
{ |
329 |
System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems"); |
330 |
if (!IsReleaseItems) |
331 |
{ |
332 |
ReleaseItems(); |
333 |
} |
334 |
|
335 |
System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems"); |
336 |
return true; |
337 |
} |
338 |
} |
339 |
} |