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