프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (12 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.ConvertID == convertItem.ConvertID);
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,
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

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

    
147
                /// 순서별 Convert Item 처리 
148
                if (convertItem != null)
149
                {
150
                    try
151
                    {
152
                        if (AliveConvertQueue.Count(c => c.UniqueKey == convertItem.UniqueKey) == 0)
153
                        {
154
                            if (ServiceStation.AliveConvertQueue.Count() < ServiceProperty.PROCESS_COUNT)
155
                            {
156
                                convertItem.ServiceID = this.ServiceID;
157
                                ConvertProcessStart(convertItem);
158
                                result = true.ToString();
159
                            }
160
                            else
161
                            {
162
                                logger.Info($"ConvertAdd  ConvertProcess Wait ProcessCount ProjectNo:{ProjectNo} convertID:{convertID} ");
163
                                result = true.ToString();
164
                            }
165
                        }
166
                        else
167
                        {
168
                            PassConvertItem(ProjectNo, convertID, convertItem.UniqueKey);
169
                            result = true.ToString();
170
                        }
171
                    }
172
                    catch (Exception ex)
173
                    {
174
                        result = "Convert Process Start Error";
175
                        logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} ", ex);
176
                    }
177
                }
178
                else
179
                {
180
                    logger.Error($"ConvertAdd  ConvertProcess call Error  ProjectNo:{ProjectNo} convertID:{convertID} item Null ");
181
                    result = "Get ConvertItem Error";
182
                }
183
            }
184

    
185
            return result;
186
        }
187

    
188
        /// <summary>
189
        /// 각 ConvertProcess의 상태값을 저장
190
        /// </summary>
191
        /// <param name="ConvertID"></param>
192
        /// <param name="status"></param>
193
        /// <param name="CurrentPage"></param>
194
        /// <param name="TotalPage"></param>
195
        /// <param name="Error"></param>
196
        /// <returns></returns>
197
        public bool ConvertProcessState(string ConvertID, int status, int CurrentPage, int TotalPage, string Error)
198
        {
199
            var items = AliveConvertQueue.Where(x => x.ConvertID == ConvertID);
200

    
201
            if (items.Count() > 0)
202
            {
203
                var item = items.First();
204

    
205
                item.TotalPage = TotalPage;
206
                item.CurrentPageNo = CurrentPage;
207
                item.ConvertState = ((StatusCodeType)status).ToString();
208
            }
209

    
210
            if (CurrentPage % SaveStatusInterval == 0 || !Error.IsNullOrEmpty())
211
            {
212
                using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
213
                {
214
                    if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error))
215
                    {
216
                        logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
217
                    }
218
                }
219
            }
220

    
221
            return true;
222
        }
223

    
224
        /// <summary>
225
        /// Convert Process 완료시 호출
226
        /// </summary>
227
        /// <param name="ConvertID"></param>
228
        /// <param name="status"></param>
229
        /// <param name="CurrentPage"></param>
230
        /// <param name="TotalPage"></param>
231
        /// <param name="Error"></param>
232
        /// <returns></returns>
233
        public bool ConvertFinish(string ConvertID, int status, int CurrentPage, int TotalPage, string Error)
234
        {
235
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
236
            {
237
                if (!database.SetConvertState(this.ServiceID, ConvertID, status, CurrentPage, TotalPage, Error))
238
                {
239
                    logger.Error($"DataBase Error database.SetConvertState({ConvertID}, {(int)status},{CurrentPage},{TotalPage}, {Error})");
240
                }
241
            }
242

    
243
            var _convertItem = ServiceStation.AliveConvertQueue.Where(f => f.ConvertID == ConvertID);
244

    
245
            if (_convertItem.Count() > 0)
246
            {
247
                ConvertFinish(_convertItem.First());
248
            }
249

    
250
            return true;
251
        }
252

    
253
        /// <summary>
254
        /// 대기중인 Convert Item을 가져옴
255
        /// </summary>
256
        /// <returns></returns>
257
        public List<ConvertItem> WaitConvertList()
258
        {
259
            List<ConvertItem> result = new List<ConvertItem>();
260

    
261
            using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(MarkusDBConnectionString))
262
            {
263
                result = database.GetConvertItems(Message.StatusCodeType.Wait).ToList();
264
            }
265

    
266
            return result;
267
        }
268

    
269
        /// <summary>
270
        /// 현재 AliveConvertQueue에 있는 컨버터 리스트
271
        /// </summary>
272
        /// <returns></returns>
273
        public List<ConvertItem> AliveConvertList()
274
        {
275
            return ServiceStation.AliveConvertQueue;
276
        }
277

    
278
        /// <summary>
279
        /// WCF 시작
280
        /// </summary>
281
        /// <returns></returns>
282
        public CommunicationState StartWcfService()
283
        {
284
            CommunicationState result = CommunicationState.Faulted;
285

    
286
            try
287
            {
288
                if (gServiceHostAddress != null)
289
                {
290
                    gWcfServiceHost = gWcfServiceHost.WcfCreate(this, typeof(IWcfService.IStationService), gServiceHostAddress);
291
                }
292

    
293
                if (gWcfServiceHost?.State != CommunicationState.Faulted)
294
                {
295
                    result = gWcfServiceHost.State;
296
                }
297
            }
298
            catch (Exception ex)
299
            {
300
                logger.Error("Start WCF Service Error ", ex);
301
            }
302

    
303
            return result;
304
        }
305

    
306
        /// <summary>
307
        /// WCF 중단
308
        /// </summary>
309
        /// <returns></returns>
310
        private bool StopWcfService()
311
        {
312
            bool result = false;
313
            try
314
            {
315
                gWcfServiceHost.Abort();
316
                result = true;
317
            }
318
            catch (Exception ex)
319
            {
320
                logger.Error("Stop WCF Service Error ", ex);
321
            }
322

    
323
            return result;
324
        }
325

    
326
        public bool ReleaseConvertItems()
327
        {
328
            System.Diagnostics.Debug.WriteLine("Call ReleaseConvertItems");
329
            if (!IsReleaseItems)
330
            {
331
                ReleaseItems();
332
            }
333

    
334
            System.Diagnostics.Debug.WriteLine("end ReleaseConvertItems");
335
            return true;
336
        }
337
    }
338
}
클립보드 이미지 추가 (최대 크기: 500 MB)