프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.Station / StationService / ServiceStationWCF.cs @ 4b33593a

이력 | 보기 | 이력해설 | 다운로드 (15.6 KB)

1 53c9637d taeseongkim
using Markus.Message;
2 a5e5fff6 taeseongkim
using Markus;
3
using Markus.Service.DataBase;
4
using Markus.Service.DataBase.Repositories;
5 53c9637d taeseongkim
using Markus.Service.Extensions;
6
using Markus.Service.IWcfService;
7
using System;
8
using System.Collections.Generic;
9
using System.Diagnostics;
10
using System.Linq;
11
using System.ServiceModel;
12
using System.ServiceModel.Activation;
13
using System.ServiceModel.Web;
14
using System.Text;
15
using System.Threading.Tasks;
16 a5e5fff6 taeseongkim
using Markus.Service.DataBase.Entities;
17 53c9637d taeseongkim
18
namespace Markus.Service
19
{
20 a53dfe45 taeseongkim
    /// <summary>
21
    /// web service 목록
22
    /// </summary>
23 53c9637d taeseongkim
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
24 1ae729e4 taeseongkim
    //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]
25 53c9637d taeseongkim
    public partial class ServiceStation : IStationService
26
    {
27 a53dfe45 taeseongkim
        /// <summary>
28
        /// DB에 Convrert Item을 추가 하고 호출 
29
        /// CovnertWebService에서 호출시 사용
30
        /// </summary>
31
        /// <param name="ProjectNo"></param>
32
        /// <param name="ConvertID"></param>
33
        /// <returns></returns>
34 53c9637d taeseongkim
        public string ConvertAdd(string ProjectNo, string ConvertID)
35
        {
36
            string result = false.ToString();
37
38
            try
39
            {
40 06f13e11 taeseongkim
                result = ConvertProcessAdd(ProjectNo, ConvertID);
41 949d5058 taeseongkim
42
                logger.Info($"ConvertAdd Call Item ProjectNo : {ProjectNo}  ConvertID : {ConvertID}  result : {result}");
43 53c9637d taeseongkim
            }
44
            catch (Exception ex)
45
            {
46
                result = $"ConvertAdd Error {ConvertID}";
47
                logger.Error($"ConvertAdd Error- {ConvertID}", ex);
48
            }
49
50
            return result;
51
        }
52
53 1d79913e taeseongkim
54
        /// <summary>
55
        /// Main Station에서 전달받은 convert item
56
        /// </summary>
57
        /// <param name="ProjectNo"></param>
58 a5e5fff6 taeseongkim
        /// <param name="ConvertDocID"></param>
59 1d79913e taeseongkim
        /// <returns></returns>
60 a5e5fff6 taeseongkim
        public string PassConvert(string ProjectNo, string ConvertDocID)
61 1d79913e taeseongkim
        {
62
            string result = false.ToString();
63
64
            try
65
            {
66 a5e5fff6 taeseongkim
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
67 1d79913e taeseongkim
                {
68 a5e5fff6 taeseongkim
                    var convertItem = database.GetConvertDocSingleAsync(ProjectNo:ProjectNo,ConvertID:ConvertDocID).GetAwaiter().GetResult();
69 1d79913e taeseongkim
70
                    /// 순서별 Convert Item 처리 
71
                    if (convertItem != null)
72
                    {
73 a5e5fff6 taeseongkim
                        if (AliveConvertQueue.Count(c => c.ID == convertItem.ID) == 0)
74 1d79913e taeseongkim
                        {
75 a5e5fff6 taeseongkim
                            result = ConvertProcessAdd(ProjectNo, ConvertDocID);
76
                            logger.Info($"PassConvert Item ProjectNo : {ProjectNo}  ConvertDocID : {ConvertDocID}  result : {result}");
77 1d79913e taeseongkim
                        }
78
                        else
79
                        {
80
81
                        }
82
                    }
83
                }
84
            }
85
            catch (Exception ex)
86
            {
87 a5e5fff6 taeseongkim
                result = $"PassConvert Error {ConvertDocID}";
88
                logger.Error($"PassConvert Error- {ConvertDocID}", ex);
89 1d79913e taeseongkim
            }
90
91
            return result;
92
        }
93
94
95 a53dfe45 taeseongkim
        /// <summary>
96
        /// 별도의 호출 사용 
97
        /// </summary>
98
        /// <param name="ProjectNo"></param>
99
        /// <param name="originfilePath"></param>
100
        /// <param name="DocumentID"></param>
101
        /// <returns></returns>
102 53c9637d taeseongkim
        public string ConvertMenualAdd(string ProjectNo, string originfilePath, string DocumentID)
103
        {
104
            string result = false.ToString();
105
106
            try
107
            {
108 a5e5fff6 taeseongkim
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
109 53c9637d taeseongkim
                {
110 a5e5fff6 taeseongkim
                    var convertItem = database.GetConvertDocSingleAsync(ProjectNo: ProjectNo, DocumentID: DocumentID).GetAwaiter().GetResult();
111 5d863c06 taeseongkim
112 a5e5fff6 taeseongkim
                    if (convertItem != null)
113 5d863c06 taeseongkim
                    {
114 a5e5fff6 taeseongkim
                        var newConvertDocId = database.CreateAsync(ProjectNo, originfilePath, DocumentID).GetAwaiter().GetResult();
115 5d863c06 taeseongkim
116 a5e5fff6 taeseongkim
                        if (newConvertDocId == null)
117
                        {
118
                            logger.Error($"Set Convert document Insert Error - createId is Null. ProjectNo:{ProjectNo} originfilePath:{originfilePath} DocumentID:{DocumentID}");
119
                        }
120
                        else
121
                        {
122
                            var aliveItems = AliveConvertQueue.Where(x => x.DOCUMENT_ID == convertItem.DOCUMENT_ID);
123
124
                            if (aliveItems.Count() == 0)
125
                            {
126
                                result = ConvertProcessAdd(ProjectNo, newConvertDocId);
127
                            }
128
                            else
129
                            {
130
                                logger.Info("ConvertMenualAdd Wait ProjectNo:{ ProjectNo} originfilePath: { originfilePath} DocumentID: { DocumentID}");
131
                            }
132
133
                            result = true.ToString();
134
                        }
135 53c9637d taeseongkim
                    }
136
                }
137
            }
138
            catch (Exception ex)
139
            {
140 a5e5fff6 taeseongkim
                result = "ConvertMenualAdd Error";
141
                logger.Error($"ConvertMenualAdd Error-  database.SetConvertDoc ProjectNo:{ProjectNo} originfilePath:{originfilePath} DocumentID:{DocumentID}", ex);
142 53c9637d taeseongkim
            }
143 06f13e11 taeseongkim
144 53c9637d taeseongkim
            return result;
145
        }
146
147 a53dfe45 taeseongkim
        /// <summary>
148
        /// ConvertItem의 상태
149
        /// </summary>
150
        /// <param name="ProjectNo"></param>
151
        /// <param name="DocumentID"></param>
152
        /// <returns></returns>
153
        public Item GetConvertItem(string ProjectNo, string DocumentID)
154
        {
155
            Item result = null;
156
157
            try
158
            {
159 a5e5fff6 taeseongkim
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
160 a53dfe45 taeseongkim
                {
161
162 a5e5fff6 taeseongkim
                    var convertItem = database.GetConvertDocSingleAsync(ProjectNo: ProjectNo, DocumentID: DocumentID).GetAwaiter().GetResult();
163
164
                    if (convertItem != null)
165 a53dfe45 taeseongkim
                    {
166 a5e5fff6 taeseongkim
                        string baseStoragePath = null;
167
168
                        using(PROPERTIESRepository repository = new PROPERTIESRepository(MarkusDBConnectionString))
169
                        {
170
                            var propeties = repository.GetFirstAsync(ProjectNo, PropertiesTypeDefine.TILE_SOURCE_STORAGE).GetAwaiter().GetResult();
171
172
                            if(propeties != null)
173
                            {
174
                                baseStoragePath = propeties.VALUE;
175
                            }
176
                            else
177
                            {
178
                                throw new FaultException($"Get {ProjectNo} is {PropertiesTypeDefine.TILE_SOURCE_STORAGE} null Error");
179
                            }
180
                        }
181
182
                        var convertPath = CreateConvertPath.Get(baseStoragePath, ProjectNo, DocumentID);
183
184 a53dfe45 taeseongkim
                        result =
185
                        new Item
186
                        {
187 a5e5fff6 taeseongkim
                            DocumentID = convertItem.ID,
188
                            Status = convertItem.STATUS.ToString(),
189
                            ProjectNo = convertItem.PROJECT_NO,
190
                            TotalPage = convertItem.TOTAL_PAGE,
191
                            CurrentPageNo = convertItem.CURRENT_PAGE,
192
                            PdfPath = convertItem.DOCUMENT_URL,
193
                            ConvertPath = convertPath
194 a53dfe45 taeseongkim
                        };
195 53c9637d taeseongkim
196 a53dfe45 taeseongkim
                    }
197
                }
198
            }
199
            catch (Exception ex)
200
            {
201
                throw new FaultException("Get ConvertItem Error");
202
            }
203 53c9637d taeseongkim
204 a53dfe45 taeseongkim
            return result;
205
        }
206
207
        /// <summary>
208
        /// Convert Item을 DB에 추가 하고 프로세스 실행시 호출
209
        /// </summary>
210
        /// <param name="ProjectNo"></param>
211
        /// <param name="convertID"></param>
212
        /// <returns></returns>
213 53c9637d taeseongkim
        private string ConvertProcessAdd(string ProjectNo, string convertID)
214
        {
215
            string result = false.ToString();
216 43e1d368 taeseongkim
            logger.Warn($"WCF ConvertProcessAdd Call ProjectNo:{ProjectNo} convertID:{convertID} ");
217 53c9637d taeseongkim
218 a5e5fff6 taeseongkim
            using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
219 53c9637d taeseongkim
            {
220 a5e5fff6 taeseongkim
                var convertItem = database.GetConvertDocSingleAsync(ProjectNo:ProjectNo,ConvertID:convertID).GetAwaiter().GetResult();
221 53c9637d taeseongkim
222
                /// 순서별 Convert Item 처리 
223
                if (convertItem != null)
224
                {
225
                    try
226
                    {
227 a5e5fff6 taeseongkim
                        if (AliveConvertQueue.Find(c => c.ID == convertItem.ID) == null)
228 cdfb57ff taeseongkim
                        {
229 43e1d368 taeseongkim
                            if (AliveConvertQueue.Count() < ServiceProperty.PROCESS_COUNT)
230 cdfb57ff taeseongkim
                            {
231 a5e5fff6 taeseongkim
                                convertItem.SERVICE_ID = this.ServiceID;
232 43e1d368 taeseongkim
                                AliveConvertQueue.Add(convertItem);
233 1d79913e taeseongkim
                                 
234 43e1d368 taeseongkim
                                var startResult = ConvertProcessStart(convertItem);
235
236
                                if(!startResult)
237
                                {
238
                                    AliveConvertQueue.Remove(convertItem);
239
                                }
240
241 cdfb57ff taeseongkim
                                result = true.ToString();
242
                            }
243 949d5058 taeseongkim
                            else
244
                            {
245 1d79913e taeseongkim
                                logger.Warn($"ConvertProcessAdd  PassConvertItem ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} ");
246
                                /// sub service (다른ip의 서비스로 보낸다) 이중화
247 a5e5fff6 taeseongkim
                                PassConvertItem(ProjectNo, convertID, true);
248 1d79913e taeseongkim
249
250 949d5058 taeseongkim
                                result = true.ToString();
251
                            }
252 cdfb57ff taeseongkim
                        }
253
                        else
254 53c9637d taeseongkim
                        {
255
                            result = true.ToString();
256
                        }
257
                    }
258
                    catch (Exception ex)
259
                    {
260
                        result = "Convert Process Start Error";
261 949d5058 taeseongkim
                        logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} ", ex);
262 53c9637d taeseongkim
                    }
263
                }
264
                else
265
                {
266 949d5058 taeseongkim
                    logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} item Null ");
267 53c9637d taeseongkim
                    result = "Get ConvertItem Error";
268
                }
269
            }
270
271
            return result;
272
        }
273
274 a53dfe45 taeseongkim
        /// <summary>
275
        /// 각 ConvertProcess의 상태값을 저장
276
        /// </summary>
277
        /// <param name="ConvertID"></param>
278
        /// <param name="status"></param>
279
        /// <param name="CurrentPage"></param>
280
        /// <param name="TotalPage"></param>
281
        /// <param name="Error"></param>
282
        /// <returns></returns>
283 a5e5fff6 taeseongkim
        public bool ConvertProcessState(string ConvertDocID, int status, int CurrentPage, int TotalPage, string Error)
284 53c9637d taeseongkim
        {
285 1d79913e taeseongkim
            if (AliveConvertQueue.Count() > 0)
286 53c9637d taeseongkim
            {
287 a5e5fff6 taeseongkim
                var items = AliveConvertQueue.Where(x => x.ID == ConvertDocID);
288 53c9637d taeseongkim
289 1d79913e taeseongkim
                if (items.Count() > 0)
290
                {
291
                    var item = items.First();
292 53c9637d taeseongkim
293 a5e5fff6 taeseongkim
                    item.TOTAL_PAGE = TotalPage;
294
                    item.CURRENT_PAGE = CurrentPage;
295
                    item.STATUS = status;
296 1d79913e taeseongkim
                }
297
            }
298
            
299 53c9637d taeseongkim
            if (CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty())
300
            {
301 a5e5fff6 taeseongkim
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
302 53c9637d taeseongkim
                {
303 a5e5fff6 taeseongkim
                    var result = database.UpdateStatusAsync(this.ServiceID, ConvertDocID, status, CurrentPage, TotalPage, Error).GetAwaiter().GetResult();
304
305
                    if (result < 1)
306 53c9637d taeseongkim
                    {
307 a5e5fff6 taeseongkim
                        logger.Error($"DataBase Error database.SetConvertState({ConvertDocID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
308 53c9637d taeseongkim
                    }
309
                }
310
            }
311
312
            return true;
313
        }
314
315 a53dfe45 taeseongkim
        /// <summary>
316
        /// Convert Process 완료시 호출
317
        /// </summary>
318
        /// <param name="ConvertID"></param>
319
        /// <param name="status"></param>
320
        /// <param name="CurrentPage"></param>
321
        /// <param name="TotalPage"></param>
322
        /// <param name="Error"></param>
323
        /// <returns></returns>
324 a5e5fff6 taeseongkim
        public bool ConvertFinish(string ConvertDocID, int status, int CurrentPage, int TotalPage, string Error)
325 53c9637d taeseongkim
        {
326 a5e5fff6 taeseongkim
            using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
327 53c9637d taeseongkim
            {
328 a5e5fff6 taeseongkim
                var result = database.UpdateStatusAsync(this.ServiceID, ConvertDocID, status, CurrentPage, TotalPage, Error).GetAwaiter().GetResult();
329
                
330
                if (result < 1)
331 53c9637d taeseongkim
                {
332 a5e5fff6 taeseongkim
                    logger.Error($"DataBase Error database.SetConvertState({ConvertDocID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
333 53c9637d taeseongkim
                }
334
            }
335
336 a5e5fff6 taeseongkim
            var _convertItem = AliveConvertQueue.Where(f => f.ID == ConvertDocID);
337 53c9637d taeseongkim
338
            if (_convertItem.Count() > 0)
339 06f13e11 taeseongkim
            {
340 53c9637d taeseongkim
                ConvertFinish(_convertItem.First());
341 06f13e11 taeseongkim
            }
342 53c9637d taeseongkim
343
            return true;
344
        }
345
346 a53dfe45 taeseongkim
        /// <summary>
347
        /// 대기중인 Convert Item을 가져옴
348
        /// </summary>
349
        /// <returns></returns>
350 a5e5fff6 taeseongkim
        public List<ConvertDoc> WaitConvertList()
351 53c9637d taeseongkim
        {
352 a5e5fff6 taeseongkim
            List<ConvertDoc> result = new List<ConvertDoc>();
353
            
354
            using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
355 53c9637d taeseongkim
            {
356 a5e5fff6 taeseongkim
                result = database.GetConvertDocAsync((int)Message.StatusCodeType.Wait).GetAwaiter().GetResult().ToList();
357 53c9637d taeseongkim
            }
358
359
            return result;
360
        }
361
362 a53dfe45 taeseongkim
        /// <summary>
363
        /// 현재 AliveConvertQueue에 있는 컨버터 리스트
364
        /// </summary>
365
        /// <returns></returns>
366 a5e5fff6 taeseongkim
        public List<ConvertDoc> AliveConvertList()
367 53c9637d taeseongkim
        {
368 06f13e11 taeseongkim
            return ServiceStation.AliveConvertQueue;
369 53c9637d taeseongkim
        }
370
371 a53dfe45 taeseongkim
        /// <summary>
372
        /// WCF 시작
373
        /// </summary>
374
        /// <returns></returns>
375 53c9637d taeseongkim
        public CommunicationState StartWcfService()
376
        {
377
            CommunicationState result = CommunicationState.Faulted;
378
379
            try
380
            {
381
                if (gServiceHostAddress != null)
382
                {
383
                    gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress);
384
                }
385
386
                if (gWcfServiceHost?.State != CommunicationState.Faulted)
387
                {
388
                    result = gWcfServiceHost.State;
389
                }
390
            }
391
            catch (Exception ex)
392
            {
393
                logger.Error("Start WCF Service Error ", ex);
394
            }
395
396
            return result;
397
        }
398
399 a53dfe45 taeseongkim
        /// <summary>
400
        /// WCF 중단
401
        /// </summary>
402
        /// <returns></returns>
403 53c9637d taeseongkim
        private bool StopWcfService()
404
        {
405
            bool result = false;
406
            try
407
            {
408
                gWcfServiceHost.Abort();
409
                result = true;
410
            }
411
            catch (Exception ex)
412
            {
413
                logger.Error("Stop WCF Service Error ", ex);
414
            }
415
416
            return result;
417
        }
418
419 06f13e11 taeseongkim
        public bool ReleaseConvertItems()
420
        {
421
            System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems");
422 60723dc9 taeseongkim
            if (!IsReleaseItems)
423
            {
424
                ReleaseItems();
425
            }
426
427 06f13e11 taeseongkim
            System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems");
428
            return true;
429
        }
430 53c9637d taeseongkim
    }
431
}
클립보드 이미지 추가 (최대 크기: 500 MB)