프로젝트

일반

사용자정보

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

markus / ConvertService / ServiceBase / Markus.Service.Station / ServiceStation.cs @ 53c9637d

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

1
using log4net;
2
using Markus.Service.Extensions;
3
using Markus.Service.Helper;
4
using Markus.Service.IWcfService;
5
using Markus.Service.WcfService;
6
using Salaros.Configuration;
7
using System;
8
using System.Collections.Generic;
9
using System.ComponentModel;
10
using System.Data;
11
using System.Diagnostics;
12
using System.Globalization;
13
using System.IO;
14
using System.Linq;
15
using System.Runtime.InteropServices;
16
using System.ServiceModel;
17
using System.ServiceProcess;
18
using System.Text;
19
using System.Threading.Tasks;
20
using System.Timers;
21
using static Markus.Service.Extensions.Encrypt;
22

    
23
namespace Markus.Service
24
{
25
    public partial class ServiceStation : ServiceBase
26
    {
27
        protected ILog logger = LogManager.GetLogger(typeof(ServiceStation));
28
        protected ServiceHost gWcfServiceHost;
29

    
30
        private int MultiProcessCount = 1;
31

    
32
        private Uri gServiceHostAddress;
33

    
34
        private string MarkusDBConnectionString;
35
        private string DownloadTempFolder;
36
        private int MultiThreadMaxPages;
37
        private int MinFontSize;
38
        private bool CreateProcessWindow;
39

    
40
        private int SaveStatusInterval;
41

    
42
        private List<string> RunProjectList = new List<string>();
43

    
44
        /// <summary>
45
        /// 프로세스 카운터 자주 람다식을 사용해서 list<int>로 함.
46
        /// </summary>
47
        private List<Int64> ProcessorAffinityList;
48

    
49
        private string configFileName;
50

    
51
        public ServiceStation()
52
        {
53
            InitializeComponent();
54
        }
55

    
56
        public void GetApplicationConfig()
57
        {
58
            try
59
            {
60
                ConfigParser config = null;
61

    
62
                try
63
                {
64
                    configFileName = $"{typeof(ServiceStation).Name}.ini";
65
                    config = ConfigHelper.AppConfig(this.configFileName);
66
                }
67
                catch (Exception)
68
                {
69
                    throw new Exception("Config Read Error.");
70
                }
71

    
72
                if (config != null)
73
                {
74
                    var processCount = config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.CONVERT_SERVICE_PROCESS);
75

    
76
                    if (!string.IsNullOrWhiteSpace(processCount))
77
                    {
78
                        int outValue = 1;
79

    
80
                        if (int.TryParse(processCount, out outValue))
81
                        {
82
                            MultiProcessCount = outValue;
83
                        }
84
                    }
85

    
86
                    CreateProcessWindow = System.Convert.ToBoolean(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.CREATE_WINDOW, "false"));
87

    
88
                    // PDF 임시 다운로드 폴더
89
                    DownloadTempFolder = config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.DOWNLOAD_TEMP_FOLDER);
90

    
91
                    MultiThreadMaxPages = System.Convert.ToInt16(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.MULTI_TRHEAD_MAX_PAGE, "500"));
92

    
93
                    SaveStatusInterval = System.Convert.ToInt16(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.SAVE_STATUS_INTERVAL, "5"));
94

    
95
                    MinFontSize = System.Convert.ToInt16(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.MIN_FONT_SIZE, "10"));
96

    
97
                    // CONVERT DATABASE 연결 문자열
98
                    MarkusDBConnectionString = AESEncrypter.Decrypt(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.MARKUS_CONNECTION_STRING));
99

    
100
                    var projectList = config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.FITER_PROCECT);
101

    
102
                    if(!projectList.IsNullOrEmpty())
103
                    {
104
                        RunProjectList = projectList.Split(',').ToList();
105
                    }
106

    
107
                    // 서비스 ENDPOINT
108
                    // http://localhost/
109
                    var endpointName = config.GetValue(CONFIG_DEFINE.WCF_ENDPOINT, CONFIG_DEFINE.STATION_SERVICE_NAME);
110
                    var port = config.GetValue(CONFIG_DEFINE.WCF_ENDPOINT, CONFIG_DEFINE.STATION_PORT);
111

    
112
                    if (!string.IsNullOrWhiteSpace(endpointName) && port.IsNumber())
113
                    {
114
                        gServiceHostAddress = UriHelper.UriCreate($"http://localhost:{port}/{endpointName}");
115
                    }
116
                }
117
            }
118
            catch (Exception ex)
119
            {
120
                throw new Exception("ApplicationConfig ", ex);
121
            }
122
        }
123

    
124
        protected override void OnStart(string[] args)
125
        {
126
            try
127
            {
128
                StartService();
129
            }
130
            catch (Exception ex)
131
            {
132
                logger.Error("ServiceStation Start Error - ", ex);
133
            }
134
        }
135

    
136
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
137
        {
138
            timer.Stop();
139

    
140
            var process = Process.GetProcessesByName("Markus.Service.ConvertProcess");
141

    
142
            for (int i = 0; i < process.Count(); i++)
143
            {
144
                System.Diagnostics.Debug.WriteLine($"Command [{i}]: " , string.Join(" ", process[i].Arguments().CommandLine));
145
            }
146

    
147
            for (int i = 0; i < ServiceStation.AliveConvertQueue.Count; i++)
148
            {
149
                System.Diagnostics.Debug.WriteLine($"AliveConvertItems  [{i}]: ", ServiceStation.AliveConvertQueue[i].ConvertID);
150
            }
151

    
152
            System.Threading.Thread.Sleep(1000);
153
            timer.Start();
154
        }
155

    
156
        System.Timers.Timer timer;
157

    
158
        private void ProcessorAffinityInit_temp()
159
        {
160
            ProcessorAffinityList = new List<long>();
161

    
162
            int processCount = Environment.ProcessorCount;
163
            bool[] bits = new bool[processCount];
164

    
165
            for (int i = 0; i < bits.Count(); i+=3)
166
            {
167
                bits = new bool[processCount];
168
                bits[i] = true;
169
                bits[i+1] = true;
170
                bits[i + 2] = true;
171

    
172
                bits = bits.ToArray();
173

    
174
                var affinity = System.Convert.ToInt64(string.Join("", bits.Select(bit => (bit ? 1.ToString() : 0.ToString()))), 2);
175

    
176
                ProcessorAffinityList.Add(affinity);
177
            }
178
        }
179

    
180
        private void ProcessorAffinityInit()
181
        {
182
            ProcessorAffinityList = new List<long>();
183

    
184
            int processCount = Environment.ProcessorCount;
185
            int AffinityScope = 1;
186

    
187
            if (processCount > MultiProcessCount)
188
            {
189
                AffinityScope = processCount / MultiProcessCount;
190
            }
191

    
192
            for (int i = 0; i < processCount - AffinityScope; i += AffinityScope)
193
            {
194
                var bits = new int[processCount];
195

    
196
                for (int j = i; j < i + AffinityScope; j++)
197
                {
198
                    bits[j] = 1;
199
                }
200

    
201
                var affinity = System.Convert.ToInt64(string.Join("", bits), 2);
202

    
203
                ProcessorAffinityList.Add(affinity);
204
            }
205
        }
206

    
207
        public bool StartService()
208
        {
209
          
210
            try
211
            {
212
                this.GetApplicationConfig();
213
                logger.Info("Read Config");
214

    
215
                ProcessorAffinityInit();
216

    
217
            }
218
            catch (Exception e)
219
            {
220
                throw new Exception("Stop StartService Error. ", e);
221
            }
222
            try
223
            {
224
                if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libpthread.dll")))
225
                {
226
                    Markus.Library.Installer.Install();
227
                    logger.Info("Markus.Library.Installer Install");
228
                }
229
                else
230
                {
231
                    logger.Info("Markus.Library.Installer Exists.");
232
                }
233
                
234
            }
235
            catch (Exception e)
236
            {
237
                throw new Exception("Stop Installer Error. ", e);
238
            }
239

    
240
            try
241
            {
242
                Stopprocess();
243
                logger.Info("Stopprocess");
244
            }
245
            catch (Exception e)
246
            {
247
                throw new Exception("Stop Process Error. ", e);
248
            }
249

    
250
            try
251
            {
252
                StartWcfService();
253

    
254
                if (gWcfServiceHost.BaseAddresses?.Count() > 0)
255
                {
256
                    gServiceHostAddress = gWcfServiceHost.BaseAddresses.First();
257
                }
258

    
259
                logger.Info($"StartWcfService {gServiceHostAddress}");
260
            }
261
            catch (Exception e)
262
            {
263
                throw new Exception("start Wcf Service Error. ", e);
264
            }
265

    
266
            try
267
            {
268
                setDataBaseWaitingList();
269
                logger.Info("setDataBaseWaitingList");
270
            }
271
            catch (Exception e)
272
            {
273
                throw new Exception("Database Waiting List Error . ", e);
274
            }
275

    
276

    
277
            logger.Info("Start ServiceStation");
278
            //timer = new System.Timers.Timer(10000);
279
            //timer.Elapsed += Timer_Elapsed;
280
            //timer.AutoReset = true;
281
            //timer.Start();
282

    
283
            return true;
284
        }
285

    
286

    
287
        protected override void OnStop()
288
        {
289
            try
290
            {
291
                StopWcfService();
292
                Stopprocess();
293

    
294
                logger.Info("ServiceStation Stop");
295
            }
296
            catch (Exception e)
297
            {
298
                logger.Error("OnStop Error . ", e);
299
            }
300
        }
301

    
302
        #region Sleep 방지
303
        //[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
304
        //static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
305
        //[FlagsAttribute]
306
        //public enum EXECUTION_STATE : uint
307
        //{
308
        //    ES_AWAYMODE_REQUIRED = 0x00000040,
309
        //    ES_CONTINUOUS = 0x80000000,
310
        //    ES_DISPLAY_REQUIRED = 0x00000002,
311
        //    ES_SYSTEM_REQUIRED = 0x00000001
312
        //    // Legacy flag, should not be used.
313
        //    // ES_USER_PRESENT = 0x00000004
314
        //}
315
        //public static void PreventScreenAndSleep()
316
        //{
317
        //    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS |
318
        //                            EXECUTION_STATE.ES_SYSTEM_REQUIRED |
319
        //                            EXECUTION_STATE.ES_AWAYMODE_REQUIRED |
320
        //                            EXECUTION_STATE.ES_DISPLAY_REQUIRED);
321
        //} 
322
        #endregion
323
    }
324
}
클립보드 이미지 추가 (최대 크기: 500 MB)