프로젝트

일반

사용자정보

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

markus / ConvertService / ServiceBase / Markus.Service.Station / StationService / ServiceStationWCF.cs @ 0a64fa85

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

1
using Markus.Message;
2
using Markus;
3
using Markus.Service.DataBase;
4
using Markus.Service.DataBase.Repositories;
5
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
using Markus.Service.DataBase.Entities;
17

    
18
namespace Markus.Service
19
{
20
    /// <summary>
21
    /// web service 목록
22
    /// </summary>
23
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
24
    //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]
25
    public partial class ServiceStation : IStationService
26
    {
27
        /// <summary>
28
        /// DB에 Convrert Item을 추가 하고 호출 
29
        /// CovnertWebService에서 호출시 사용
30
        /// </summary>
31
        /// <param name="ProjectNo"></param>
32
        /// <param name="ConvertID"></param>
33
        /// <returns></returns>
34
        public string ConvertAdd(string ProjectNo, string ConvertID)
35
        {
36
            string result = false.ToString();
37

    
38
            try
39
            {
40
                result = ConvertProcessAdd(ProjectNo, ConvertID);
41

    
42
                logger.Info($"ConvertAdd Call Item ProjectNo : {ProjectNo}  ConvertID : {ConvertID}  result : {result}");
43
            }
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

    
54
        /// <summary>
55
        /// Main Station에서 전달받은 convert item
56
        /// </summary>
57
        /// <param name="ProjectNo"></param>
58
        /// <param name="ConvertDocID"></param>
59
        /// <returns></returns>
60
        public string PassConvert(string ProjectNo, string ConvertDocID)
61
        {
62
            string result = false.ToString();
63

    
64
            try
65
            {
66
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
67
                {
68
                    var convertItem = database.GetConvertDocSingleAsync(ProjectNo:ProjectNo,ConvertID:ConvertDocID).GetAwaiter().GetResult();
69

    
70
                    /// 순서별 Convert Item 처리 
71
                    if (convertItem != null)
72
                    {
73
                        if (AliveConvertQueue.Count(c => c.ID == convertItem.ID) == 0)
74
                        {
75
                            result = ConvertProcessAdd(ProjectNo, ConvertDocID);
76
                            logger.Info($"PassConvert Item ProjectNo : {ProjectNo}  ConvertDocID : {ConvertDocID}  result : {result}");
77
                        }
78
                        else
79
                        {
80

    
81
                        }
82
                    }
83
                }
84
            }
85
            catch (Exception ex)
86
            {
87
                result = $"PassConvert Error {ConvertDocID}";
88
                logger.Error($"PassConvert Error- {ConvertDocID}", ex);
89
            }
90

    
91
            return result;
92
        }
93

    
94

    
95
        /// <summary>
96
        /// 별도의 호출 사용 
97
        /// </summary>
98
        /// <param name="ProjectNo"></param>
99
        /// <param name="originfilePath"></param>
100
        /// <param name="DocumentID"></param>
101
        /// <returns></returns>
102
        public string ConvertMenualAdd(string ProjectNo, string originfilePath, string DocumentID)
103
        {
104
            string result = false.ToString();
105

    
106
            try
107
            {
108
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
109
                {
110
                    //var convertItem = database.GetConvertDocSingleAsync(ProjectNo: ProjectNo, DocumentID: DocumentID).GetAwaiter().GetResult();
111

    
112
                    //if (convertItem != null)
113
                    //{
114
                    var newConvertDocId = database.CreateAsync(ProjectNo, originfilePath, DocumentID).GetAwaiter().GetResult();
115

    
116
                    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 == DocumentID);
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
                    //}
136
                }
137
            }
138
            catch (Exception ex)
139
            {
140
                result = "ConvertMenualAdd Error";
141
                logger.Error($"ConvertMenualAdd Error-  database.SetConvertDoc ProjectNo:{ProjectNo} originfilePath:{originfilePath} DocumentID:{DocumentID}", ex);
142
            }
143

    
144
            return result;
145
        }
146

    
147
        /// <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
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
160
                {
161

    
162
                    var convertItem = database.GetConvertDocSingleAsync(ProjectNo: ProjectNo, DocumentID: DocumentID).GetAwaiter().GetResult();
163

    
164
                    if (convertItem != null)
165
                    {
166
                        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
                        result =
185
                        new Item
186
                        {
187
                            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
                        };
195

    
196
                    }
197
                }
198
            }
199
            catch (Exception ex)
200
            {
201
                throw new FaultException("Get ConvertItem Error");
202
            }
203

    
204
            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
        private string ConvertProcessAdd(string ProjectNo, string convertID)
214
        {
215
            string result = false.ToString();
216
            logger.Warn($"WCF ConvertProcessAdd Call ProjectNo:{ProjectNo} convertID:{convertID} ");
217

    
218
            using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
219
            {
220
                var convertItem = database.GetConvertDocSingleAsync(ProjectNo:ProjectNo,ConvertID:convertID).GetAwaiter().GetResult();
221

    
222
                /// 순서별 Convert Item 처리 
223
                if (convertItem != null)
224
                {
225
                    try
226
                    {
227
                        if (AliveConvertQueue.Find(c => c.ID == convertItem.ID) == null)
228
                        {
229
                            if (AliveConvertQueue.Count() < ServiceProperty.PROCESS_COUNT)
230
                            {
231
                                convertItem.SERVICE_ID = this.ServiceID;
232
                                AliveConvertQueue.Add(convertItem);
233
                                 
234
                                var startResult = ConvertProcessStart(convertItem);
235

    
236
                                if(!startResult)
237
                                {
238
                                    AliveConvertQueue.Remove(convertItem);
239
                                }
240

    
241
                                result = true.ToString();
242
                            }
243
                            else
244
                            {
245
                                logger.Warn($"ConvertProcessAdd  PassConvertItem ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} ");
246
                                /// sub service (다른ip의 서비스로 보낸다) 이중화
247
                                PassConvertItem(ProjectNo, convertID, true);
248

    
249

    
250
                                result = true.ToString();
251
                            }
252
                        }
253
                        else
254
                        {
255
                            result = true.ToString();
256
                        }
257
                    }
258
                    catch (Exception ex)
259
                    {
260
                        result = "Convert Process Start Error";
261
                        logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} ", ex);
262
                    }
263
                }
264
                else
265
                {
266
                    logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} item Null ");
267
                    result = "Get ConvertItem Error";
268
                }
269
            }
270

    
271
            return result;
272
        }
273

    
274
        /// <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
        public bool ConvertProcessState(string ConvertDocID, int status, int CurrentPage, int TotalPage, string Error)
284
        {
285
            bool isStatusChange = false;
286

    
287
            if (AliveConvertQueue.Count() > 0)
288
            {
289
                var items = AliveConvertQueue.Where(x => x.ID == ConvertDocID);
290

    
291
                if (items.Count() > 0)
292
                {
293
                    var item = items.First();
294

    
295
                    item.TOTAL_PAGE = TotalPage;
296
                    item.CURRENT_PAGE = CurrentPage;
297

    
298
                    if (item.STATUS != status)
299
                    {
300
                        item.STATUS = status;
301
                        isStatusChange = true;
302
                    }
303
                }
304
            }
305
            
306
            if (isStatusChange || CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty())
307
            {
308
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
309
                {
310
                    var result = database.UpdateStatusAsync(this.ServiceID, ConvertDocID, status, CurrentPage, TotalPage, Error).GetAwaiter().GetResult();
311

    
312
                    if (result < 1)
313
                    {
314
                        logger.Error($"DataBase Error database.SetConvertState({ConvertDocID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
315
                    }
316
                }
317
            }
318

    
319
            return true;
320
        }
321

    
322
        /// <summary>
323
        /// Convert Process 완료시 호출
324
        /// </summary>
325
        /// <param name="ConvertID"></param>
326
        /// <param name="status"></param>
327
        /// <param name="CurrentPage"></param>
328
        /// <param name="TotalPage"></param>
329
        /// <param name="Error"></param>
330
        /// <returns></returns>
331
        public bool ConvertFinish(string ConvertDocID, int status, int CurrentPage, int TotalPage, string Error)
332
        {
333
            using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
334
            {
335
                var result = database.UpdateStatusAsync(this.ServiceID, ConvertDocID, status, CurrentPage, TotalPage, Error).GetAwaiter().GetResult();
336
                
337
                if (result < 1)
338
                {
339
                    logger.Error($"DataBase Error database.SetConvertState({ConvertDocID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
340
                }
341
            }
342

    
343
            var _convertItem = AliveConvertQueue.Where(f => f.ID == ConvertDocID);
344

    
345
            if (_convertItem.Count() > 0)
346
            {
347
                ConvertFinish(_convertItem.First());
348
            }
349

    
350
            return true;
351
        }
352

    
353
        /// <summary>
354
        /// 대기중인 Convert Item을 가져옴
355
        /// </summary>
356
        /// <returns></returns>
357
        public List<ConvertDoc> WaitConvertList()
358
        {
359
            List<ConvertDoc> result = new List<ConvertDoc>();
360
            
361
            using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString))
362
            {
363
                result = database.GetConvertDocAsync((int)Message.StatusCodeType.Wait).GetAwaiter().GetResult().ToList();
364
            }
365

    
366
            return result;
367
        }
368

    
369
        /// <summary>
370
        /// 현재 AliveConvertQueue에 있는 컨버터 리스트
371
        /// </summary>
372
        /// <returns></returns>
373
        public List<ConvertDoc> AliveConvertList()
374
        {
375
            return ServiceStation.AliveConvertQueue;
376
        }
377

    
378
        /// <summary>
379
        /// WCF 시작
380
        /// </summary>
381
        /// <returns></returns>
382
        public CommunicationState StartWcfService()
383
        {
384
            CommunicationState result = CommunicationState.Faulted;
385

    
386
            try
387
            {
388
                if (gServiceHostAddress != null)
389
                {
390
                    gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress);
391
                }
392

    
393
                if (gWcfServiceHost?.State != CommunicationState.Faulted)
394
                {
395
                    result = gWcfServiceHost.State;
396
                }
397
            }
398
            catch (Exception ex)
399
            {
400
                logger.Error("Start WCF Service Error ", ex);
401
            }
402

    
403
            return result;
404
        }
405

    
406
        /// <summary>
407
        /// WCF 중단
408
        /// </summary>
409
        /// <returns></returns>
410
        private bool StopWcfService()
411
        {
412
            bool result = false;
413
            try
414
            {
415
                if (gWcfServiceHost != null)
416
                {
417
                    gWcfServiceHost.Abort();
418
                }
419

    
420
                result = true;
421
            }
422
            catch (Exception ex)
423
            {
424
                logger.Error("Stop WCF Service Error ", ex);
425
            }
426

    
427
            return result;
428
        }
429

    
430
        public bool ReleaseConvertItems()
431
        {
432
            System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems");
433
            if (!IsReleaseItems)
434
            {
435
                ReleaseItems();
436
            }
437

    
438
            System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems");
439
            return true;
440
        }
441
    }
442
}
클립보드 이미지 추가 (최대 크기: 500 MB)