프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (14.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

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

    
61
            try
62
            {
63
                using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
64
                {
65
                    var convertItem = database.GetConvertItem(ProjectNo, ConvertID);
66

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

    
78
                        }
79
                    }
80
                }
81
            }
82
            catch (Exception ex)
83
            {
84
                result = $"PassConvert Error {ConvertID}";
85
                logger.Error($"PassConvert Error- {ConvertID}", ex);
86
            }
87

    
88
            return result;
89
        }
90

    
91

    
92
        /// <summary>
93
        /// 별도의 호출 사용 
94
        /// </summary>
95
        /// <param name="ProjectNo"></param>
96
        /// <param name="originfilePath"></param>
97
        /// <param name="DocumentID"></param>
98
        /// <returns></returns>
99
        public string ConvertMenualAdd(string ProjectNo, string originfilePath, string DocumentID)
100
        {
101
            string result = false.ToString();
102
            string convertID = new Guid().CreateUniqueGuid().ToString().Replace("-", "");
103

    
104
            try
105
            {
106
                using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
107
                {
108
                    var convertItem = database.GetConvertItembyDocumentID(ProjectNo, DocumentID);
109

    
110
                    if(convertItem != null)
111
                    {
112
                        var aliveItems = AliveConvertQueue.Where(x => x.UniqueKey == convertItem.UniqueKey);
113
                    }
114

    
115
                    if (!database.SetConvertDoc(ProjectNo, convertID, originfilePath, DocumentID))
116
                    {
117
                        logger.Error($"Set Convert Document Error - ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}");
118
                    }
119
                    else
120
                    {
121
                        result = ConvertProcessAdd(ProjectNo, convertID);
122
                    }
123
                }
124
            }
125
            catch (Exception ex)
126
            {
127
                result = "DataBase SetConvert Error";
128
                logger.Error($"ConvertMenualAdd Error-  database.SetConvertDoc ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}", ex);
129
            }
130

    
131
            return result;
132
        }
133

    
134
        /// <summary>
135
        /// ConvertItem의 상태
136
        /// </summary>
137
        /// <param name="ProjectNo"></param>
138
        /// <param name="DocumentID"></param>
139
        /// <returns></returns>
140
        public Item GetConvertItem(string ProjectNo, string DocumentID)
141
        {
142
            Item result = null;
143

    
144
            try
145
            {
146
                using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
147
                {
148
                    var item = database.GetConvertItembyDocumentID(ProjectNo, DocumentID);
149

    
150
                    if (item != null)
151
                    {
152
                        result =
153
                        new Item
154
                        {
155
                            DocumentID = item.UniqueKey,
156
                            Status = item.ConvertState.ToString(),
157
                            ProjectNo = item.ProjectNumber,
158
                            TotalPage = item.TotalPage,
159
                            CurrentPageNo = item.CurrentPageNo,
160
                            PdfPath = item.OriginfilePath,
161
                            ConvertPath = item.ConvertPath
162
                        };
163

    
164
                    }
165
                }
166
            }
167
            catch (Exception ex)
168
            {
169
                throw new FaultException("Get ConvertItem Error");
170
            }
171

    
172
            return result;
173
        }
174

    
175
        /// <summary>
176
        /// Convert Item을 DB에 추가 하고 프로세스 실행시 호출
177
        /// </summary>
178
        /// <param name="ProjectNo"></param>
179
        /// <param name="convertID"></param>
180
        /// <returns></returns>
181
        private string ConvertProcessAdd(string ProjectNo, string convertID)
182
        {
183
            string result = false.ToString();
184
            logger.Warn($"WCF ConvertProcessAdd Call ProjectNo:{ProjectNo} convertID:{convertID} ");
185

    
186
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
187
            {
188
                var convertItem = database.GetConvertItem(ProjectNo, convertID);
189

    
190
                /// 순서별 Convert Item 처리 
191
                if (convertItem != null)
192
                {
193
                    try
194
                    {
195
                        if (AliveConvertQueue.Find(c => c.UniqueKey == convertItem.UniqueKey) == null)
196
                        {
197
                            if (AliveConvertQueue.Count() < ServiceProperty.PROCESS_COUNT)
198
                            {
199
                                convertItem.ServiceID = this.ServiceID;
200
                                AliveConvertQueue.Add(convertItem);
201
                                 
202
                                var startResult = ConvertProcessStart(convertItem);
203

    
204
                                if(!startResult)
205
                                {
206
                                    AliveConvertQueue.Remove(convertItem);
207
                                }
208

    
209
                                result = true.ToString();
210
                            }
211
                            else
212
                            {
213
                                logger.Warn($"ConvertProcessAdd  PassConvertItem ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} ");
214
                                /// sub service (다른ip의 서비스로 보낸다) 이중화
215
                                PassConvertItem(ProjectNo, convertID, convertItem.UniqueKey, true);
216

    
217

    
218
                                result = true.ToString();
219
                            }
220
                        }
221
                        else
222
                        {
223
                            result = true.ToString();
224
                        }
225
                    }
226
                    catch (Exception ex)
227
                    {
228
                        result = "Convert Process Start Error";
229
                        logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} ", ex);
230
                    }
231
                }
232
                else
233
                {
234
                    logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} item Null ");
235
                    result = "Get ConvertItem Error";
236
                }
237
            }
238

    
239
            return result;
240
        }
241

    
242
        /// <summary>
243
        /// 각 ConvertProcess의 상태값을 저장
244
        /// </summary>
245
        /// <param name="ConvertID"></param>
246
        /// <param name="status"></param>
247
        /// <param name="CurrentPage"></param>
248
        /// <param name="TotalPage"></param>
249
        /// <param name="Error"></param>
250
        /// <returns></returns>
251
        public bool ConvertProcessState(string ConvertID, int status, int CurrentPage, int TotalPage, string Error)
252
        {
253
            if (AliveConvertQueue.Count() > 0)
254
            {
255
                var items = AliveConvertQueue.Where(x => x.ConvertID == ConvertID);
256

    
257
                if (items.Count() > 0)
258
                {
259
                    var item = items.First();
260

    
261
                    item.TotalPage = TotalPage;
262
                    item.CurrentPageNo = CurrentPage;
263
                    item.ConvertState = (StatusCodeType)status;
264
                }
265
            }
266
            
267
            if (CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty())
268
            {
269
                using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
270
                {
271
                    if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error))
272
                    {
273
                        logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
274
                    }
275
                }
276
            }
277

    
278
            return true;
279
        }
280

    
281
        /// <summary>
282
        /// Convert Process 완료시 호출
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 ConvertFinish(string ConvertID, int status, int CurrentPage, int TotalPage, string Error)
291
        {
292
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
293
            {
294
                if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error))
295
                {
296
                    logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
297
                }
298
            }
299

    
300
            var _convertItem = AliveConvertQueue.Where(f => f.ConvertID == ConvertID);
301

    
302
            if (_convertItem.Count() > 0)
303
            {
304
                ConvertFinish(_convertItem.First());
305
            }
306

    
307
            return true;
308
        }
309

    
310
        /// <summary>
311
        /// 대기중인 Convert Item을 가져옴
312
        /// </summary>
313
        /// <returns></returns>
314
        public List<ConvertItem> WaitConvertList()
315
        {
316
            List<ConvertItem> result = new List<ConvertItem>();
317

    
318
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
319
            {
320
                result = database.GetConvertItems(Message.StatusCodeType.Wait).ToList();
321
            }
322

    
323
            return result;
324
        }
325

    
326
        /// <summary>
327
        /// 현재 AliveConvertQueue에 있는 컨버터 리스트
328
        /// </summary>
329
        /// <returns></returns>
330
        public List<ConvertItem> AliveConvertList()
331
        {
332
            return ServiceStation.AliveConvertQueue;
333
        }
334

    
335
        /// <summary>
336
        /// WCF 시작
337
        /// </summary>
338
        /// <returns></returns>
339
        public CommunicationState StartWcfService()
340
        {
341
            CommunicationState result = CommunicationState.Faulted;
342

    
343
            try
344
            {
345
                if (gServiceHostAddress != null)
346
                {
347
                    gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress);
348
                }
349

    
350
                if (gWcfServiceHost?.State != CommunicationState.Faulted)
351
                {
352
                    result = gWcfServiceHost.State;
353
                }
354
            }
355
            catch (Exception ex)
356
            {
357
                logger.Error("Start WCF Service Error ", ex);
358
            }
359

    
360
            return result;
361
        }
362

    
363
        /// <summary>
364
        /// WCF 중단
365
        /// </summary>
366
        /// <returns></returns>
367
        private bool StopWcfService()
368
        {
369
            bool result = false;
370
            try
371
            {
372
                gWcfServiceHost.Abort();
373
                result = true;
374
            }
375
            catch (Exception ex)
376
            {
377
                logger.Error("Stop WCF Service Error ", ex);
378
            }
379

    
380
            return result;
381
        }
382

    
383
        public bool ReleaseConvertItems()
384
        {
385
            System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems");
386
            if (!IsReleaseItems)
387
            {
388
                ReleaseItems();
389
            }
390

    
391
            System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems");
392
            return true;
393
        }
394
    }
395
}
클립보드 이미지 추가 (최대 크기: 500 MB)