프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (16.2 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,DBMSType))
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,DBMSType))
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,DBMSType))
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,DBMSType))
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,DBMSType))
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
                                else
241
                                {
242
                                    if(Environment.UserInteractive)
243
                                    {
244
                                        Console.WriteLine($"run Process {convertItem.ID}");
245
                                    }
246
                                }
247

    
248
                                result = true.ToString();
249
                            }
250
                            else
251
                            {
252
                                logger.Warn($"ConvertProcessAdd  PassConvertItem ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} ");
253
                                /// sub service (다른ip의 서비스로 보낸다) 이중화
254
                                PassConvertItem(ProjectNo, convertID, true);
255

    
256

    
257
                                result = true.ToString();
258
                            }
259
                        }
260
                        else
261
                        {
262
                            result = true.ToString();
263
                        }
264
                    }
265
                    catch (Exception ex)
266
                    {
267
                        result = "Convert Process Start Error";
268
                        logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} ", ex);
269
                    }
270
                }
271
                else
272
                {
273
                    logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} item Null ");
274
                    result = "Get ConvertItem Error";
275
                }
276
            }
277

    
278
            return result;
279
        }
280

    
281
        /// <summary>
282
        /// 각 ConvertProcess의 상태값을 저장
283
        /// </summary>
284
        /// <param name="ConvertID"></param>
285
        /// <param name="status"></param>
286
        /// <param name="CurrentPage"></param>
287
        /// <param name="TotalPage"></param>
288
        /// <param name="Error"></param>
289
        /// <returns></returns>
290
        public bool ConvertProcessState(string ConvertDocID, int status, int CurrentPage, int TotalPage, string Error)
291
        {
292
            bool isStatusChange = false;
293

    
294
            if (AliveConvertQueue.Count() > 0)
295
            {
296
                var items = AliveConvertQueue.Where(x => x.ID == ConvertDocID);
297

    
298
                if (items.Count() > 0)
299
                {
300
                    var item = items.First();
301

    
302
                    item.TOTAL_PAGE = TotalPage;
303
                    item.CURRENT_PAGE = CurrentPage;
304

    
305
                    if (item.STATUS != status)
306
                    {
307
                        item.STATUS = status;
308
                        isStatusChange = true;
309
                    }
310
                }
311
            }
312
            
313
            if (isStatusChange || CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty())
314
            {
315
                using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString,DBMSType))
316
                {
317
                    var result = database.UpdateStatusAsync(this.ServiceID, ConvertDocID, status, CurrentPage, TotalPage, Error).GetAwaiter().GetResult();
318

    
319
                    if (result < 1)
320
                    {
321
                        logger.Error($"DataBase Error database.SetConvertState({ConvertDocID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
322
                    }
323
                }
324
            }
325

    
326
            return true;
327
        }
328

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

    
350
            var _convertItem = AliveConvertQueue.Where(f => f.ID == ConvertDocID);
351

    
352
            if (_convertItem.Count() > 0)
353
            {
354
                ConvertFinish(_convertItem.First());
355
            }
356

    
357
            return true;
358
        }
359

    
360
        /// <summary>
361
        /// 대기중인 Convert Item을 가져옴
362
        /// </summary>
363
        /// <returns></returns>
364
        public List<ConvertDoc> WaitConvertList()
365
        {
366
            List<ConvertDoc> result = new List<ConvertDoc>();
367
            
368
            using (ConvertDocRepository database = new ConvertDocRepository(MarkusDBConnectionString,DBMSType))
369
            {
370
                result = database.GetConvertDocAsync((int)Message.StatusCodeType.Wait).GetAwaiter().GetResult().ToList();
371
            }
372

    
373
            return result;
374
        }
375

    
376
        /// <summary>
377
        /// 현재 AliveConvertQueue에 있는 컨버터 리스트
378
        /// </summary>
379
        /// <returns></returns>
380
        public List<ConvertDoc> AliveConvertList()
381
        {
382
            return ServiceStation.AliveConvertQueue;
383
        }
384

    
385
        /// <summary>
386
        /// WCF 시작
387
        /// </summary>
388
        /// <returns></returns>
389
        public CommunicationState StartWcfService()
390
        {
391
            CommunicationState result = CommunicationState.Faulted;
392

    
393
            try
394
            {
395
                if (gServiceHostAddress != null)
396
                {
397
                    gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress);
398
                }
399

    
400
                if (gWcfServiceHost?.State != CommunicationState.Faulted)
401
                {
402
                    result = gWcfServiceHost.State;
403
                }
404
            }
405
            catch (Exception ex)
406
            {
407
                logger.Error("Start WCF Service Error ", ex);
408
            }
409

    
410
            return result;
411
        }
412

    
413
        /// <summary>
414
        /// WCF 중단
415
        /// </summary>
416
        /// <returns></returns>
417
        private bool StopWcfService()
418
        {
419
            bool result = false;
420
            try
421
            {
422
                if (gWcfServiceHost != null)
423
                {
424
                    gWcfServiceHost.Abort();
425
                }
426

    
427
                result = true;
428
            }
429
            catch (Exception ex)
430
            {
431
                logger.Error("Stop WCF Service Error ", ex);
432
            }
433

    
434
            return result;
435
        }
436

    
437
        public bool ReleaseConvertItems()
438
        {
439
            System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems");
440
            if (!IsReleaseItems)
441
            {
442
                ReleaseItems();
443
            }
444

    
445
            System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems");
446
            return true;
447
        }
448
    }
449
}
클립보드 이미지 추가 (최대 크기: 500 MB)