프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (12.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.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
        /// <summary>
51
        /// 별도의 호출 사용 
52
        /// </summary>
53
        /// <param name="ProjectNo"></param>
54
        /// <param name="originfilePath"></param>
55
        /// <param name="DocumentID"></param>
56
        /// <returns></returns>
57
        public string ConvertMenualAdd(string ProjectNo, string originfilePath, string DocumentID)
58
        {
59
            string result = false.ToString();
60
            string convertID = new Guid().CreateUniqueGuid().ToString().Replace("-", "");
61

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

    
68
                    if(convertItem != null)
69
                    {
70
                        var aliveItems = AliveConvertQueue.Where(x => x.UniqueKey == convertItem.UniqueKey);
71
                    }
72

    
73
                    if (!database.SetConvertDoc(ProjectNo, convertID, originfilePath, DocumentID))
74
                    {
75
                        logger.Error($"Set Convert Document Error - ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}");
76
                    }
77
                    else
78
                    {
79
                        result = ConvertProcessAdd(ProjectNo, convertID);
80
                    }
81
                }
82
            }
83
            catch (Exception ex)
84
            {
85
                result = "DataBase SetConvert Error";
86
                logger.Error($"ConvertMenualAdd Error-  database.SetConvertDoc ProjectNo:{ProjectNo} UniqueID:{convertID} originfilePath:{originfilePath} DocumentID:{DocumentID}", ex);
87
            }
88

    
89
            return result;
90
        }
91

    
92
        /// <summary>
93
        /// ConvertItem의 상태
94
        /// </summary>
95
        /// <param name="ProjectNo"></param>
96
        /// <param name="DocumentID"></param>
97
        /// <returns></returns>
98
        public Item GetConvertItem(string ProjectNo, string DocumentID)
99
        {
100
            Item result = null;
101

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

    
108
                    if (item != null)
109
                    {
110
                        result =
111
                        new Item
112
                        {
113
                            DocumentID = item.UniqueKey,
114
                            Status = item.ConvertState.ToString(),
115
                            ProjectNo = item.ProjectNumber,
116
                            TotalPage = item.TotalPage,
117
                            CurrentPageNo = item.CurrentPageNo,
118
                            PdfPath = item.OriginfilePath,
119
                            ConvertPath = item.ConvertPath
120
                        };
121

    
122
                    }
123
                }
124
            }
125
            catch (Exception ex)
126
            {
127
                throw new FaultException("Get ConvertItem Error");
128
            }
129

    
130
            return result;
131
        }
132

    
133
        /// <summary>
134
        /// Convert Item을 DB에 추가 하고 프로세스 실행시 호출
135
        /// </summary>
136
        /// <param name="ProjectNo"></param>
137
        /// <param name="convertID"></param>
138
        /// <returns></returns>
139
        private string ConvertProcessAdd(string ProjectNo, string convertID)
140
        {
141
            string result = false.ToString();
142
            logger.Warn($"WCF ConvertProcessAdd Call ProjectNo:{ProjectNo} convertID:{convertID} ");
143

    
144
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
145
            {
146
                var convertItem = database.GetConvertItem(ProjectNo, convertID);
147

    
148
                /// 순서별 Convert Item 처리 
149
                if (convertItem != null)
150
                {
151
                    try
152
                    {
153
                        if (AliveConvertQueue.Count(c => c.UniqueKey == convertItem.UniqueKey) == 0)
154
                        {
155
                            if (AliveConvertQueue.Count() < ServiceProperty.PROCESS_COUNT)
156
                            {
157
                                convertItem.ServiceID = this.ServiceID;
158
                                AliveConvertQueue.Add(convertItem);
159

    
160
                                var startResult = ConvertProcessStart(convertItem);
161

    
162
                                if(!startResult)
163
                                {
164
                                    AliveConvertQueue.Remove(convertItem);
165
                                }
166

    
167
                                result = true.ToString();
168
                            }
169
                            else
170
                            {
171
                                //logger.Warn($"ConvertProcessAdd  PassConvertItem ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} ");
172
                                ///// sub service (다른ip의 서비스로 보낸다) 이중화
173
                                //PassConvertItem(ProjectNo, convertID, convertItem.UniqueKey,true);
174
                     
175
                                
176
                                result = true.ToString();
177
                            }
178
                        }
179
                        else
180
                        {
181
                            result = true.ToString();
182
                        }
183
                    }
184
                    catch (Exception ex)
185
                    {
186
                        result = "Convert Process Start Error";
187
                        logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} ", ex);
188
                    }
189
                }
190
                else
191
                {
192
                    logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} item Null ");
193
                    result = "Get ConvertItem Error";
194
                }
195
            }
196

    
197
            return result;
198
        }
199

    
200
        /// <summary>
201
        /// 각 ConvertProcess의 상태값을 저장
202
        /// </summary>
203
        /// <param name="ConvertID"></param>
204
        /// <param name="status"></param>
205
        /// <param name="CurrentPage"></param>
206
        /// <param name="TotalPage"></param>
207
        /// <param name="Error"></param>
208
        /// <returns></returns>
209
        public bool ConvertProcessState(string ConvertID, int status, int CurrentPage, int TotalPage, string Error)
210
        {
211
            var items = AliveConvertQueue.Where(x => x.ConvertID == ConvertID);
212

    
213
            if (items.Count() > 0)
214
            {
215
                var item = items.First();
216

    
217
                item.TotalPage = TotalPage;
218
                item.CurrentPageNo = CurrentPage;
219
                item.ConvertState = (StatusCodeType)status;
220
            }
221

    
222
            if (CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty())
223
            {
224
                using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
225
                {
226
                    if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error))
227
                    {
228
                        logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
229
                    }
230
                }
231
            }
232

    
233
            return true;
234
        }
235

    
236
        /// <summary>
237
        /// Convert Process 완료시 호출
238
        /// </summary>
239
        /// <param name="ConvertID"></param>
240
        /// <param name="status"></param>
241
        /// <param name="CurrentPage"></param>
242
        /// <param name="TotalPage"></param>
243
        /// <param name="Error"></param>
244
        /// <returns></returns>
245
        public bool ConvertFinish(string ConvertID, int status, int CurrentPage, int TotalPage, string Error)
246
        {
247
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
248
            {
249
                if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error))
250
                {
251
                    logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
252
                }
253
            }
254

    
255
            var _convertItem = AliveConvertQueue.Where(f => f.ConvertID == ConvertID);
256

    
257
            if (_convertItem.Count() > 0)
258
            {
259
                ConvertFinish(_convertItem.First());
260
            }
261

    
262
            return true;
263
        }
264

    
265
        /// <summary>
266
        /// 대기중인 Convert Item을 가져옴
267
        /// </summary>
268
        /// <returns></returns>
269
        public List<ConvertItem> WaitConvertList()
270
        {
271
            List<ConvertItem> result = new List<ConvertItem>();
272

    
273
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
274
            {
275
                result = database.GetConvertItems(Message.StatusCodeType.Wait).ToList();
276
            }
277

    
278
            return result;
279
        }
280

    
281
        /// <summary>
282
        /// 현재 AliveConvertQueue에 있는 컨버터 리스트
283
        /// </summary>
284
        /// <returns></returns>
285
        public List<ConvertItem> AliveConvertList()
286
        {
287
            return ServiceStation.AliveConvertQueue;
288
        }
289

    
290
        /// <summary>
291
        /// WCF 시작
292
        /// </summary>
293
        /// <returns></returns>
294
        public CommunicationState StartWcfService()
295
        {
296
            CommunicationState result = CommunicationState.Faulted;
297

    
298
            try
299
            {
300
                if (gServiceHostAddress != null)
301
                {
302
                    gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress);
303
                }
304

    
305
                if (gWcfServiceHost?.State != CommunicationState.Faulted)
306
                {
307
                    result = gWcfServiceHost.State;
308
                }
309
            }
310
            catch (Exception ex)
311
            {
312
                logger.Error("Start WCF Service Error ", ex);
313
            }
314

    
315
            return result;
316
        }
317

    
318
        /// <summary>
319
        /// WCF 중단
320
        /// </summary>
321
        /// <returns></returns>
322
        private bool StopWcfService()
323
        {
324
            bool result = false;
325
            try
326
            {
327
                gWcfServiceHost.Abort();
328
                result = true;
329
            }
330
            catch (Exception ex)
331
            {
332
                logger.Error("Stop WCF Service Error ", ex);
333
            }
334

    
335
            return result;
336
        }
337

    
338
        public bool ReleaseConvertItems()
339
        {
340
            System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems");
341
            if (!IsReleaseItems)
342
            {
343
                ReleaseItems();
344
            }
345

    
346
            System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems");
347
            return true;
348
        }
349
    }
350
}
클립보드 이미지 추가 (최대 크기: 500 MB)