프로젝트

일반

사용자정보

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

markus / ConvertService / ServiceBase / Markus.Service.Station / ServiceStation.cs @ 2091a7e5

이력 | 보기 | 이력해설 | 다운로드 (10.1 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 int UseResolution;
39
        private bool CreateProcessWindow;
40

    
41
        private int SaveStatusInterval;
42

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

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

    
50
        private string configFileName;
51

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

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

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

    
73
                if (config != null)
74
                {
75
                    MultiProcessCount = System.Convert.ToInt16(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.CONVERT_SERVICE_PROCESS,"5"));
76

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

    
79
                    // PDF 임시 다운로드 폴더
80
                    DownloadTempFolder = config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.DOWNLOAD_TEMP_FOLDER,"C:\\temp");
81

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

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

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

    
88
                    UseResolution = System.Convert.ToInt16(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.USE_RESOLUTION, "0"));
89

    
90
                    // CONVERT DATABASE 연결 문자열
91
                    MarkusDBConnectionString = AESEncrypter.Decrypt(config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.MARKUS_CONNECTION_STRING));
92

    
93
                    var projectList = config.GetValue(CONFIG_DEFINE.SERVICE, CONFIG_DEFINE.FITER_PROCECT,"");
94

    
95
                    if(!projectList.IsNullOrEmpty())
96
                    {
97
                        RunProjectList = projectList.Split(',').ToList();
98
                    }
99

    
100
                    // 서비스 ENDPOINT
101
                    // http://localhost/
102
                    var endpointName = config.GetValue(CONFIG_DEFINE.WCF_ENDPOINT, CONFIG_DEFINE.STATION_SERVICE_NAME);
103
                    var port = config.GetValue(CONFIG_DEFINE.WCF_ENDPOINT, CONFIG_DEFINE.STATION_PORT);
104

    
105
                    if (!string.IsNullOrWhiteSpace(endpointName) && port.IsNumber())
106
                    {
107
                        gServiceHostAddress = UriHelper.UriCreate($"http://localhost:{port}/{endpointName}");
108
                    }
109
                }
110
            }
111
            catch (Exception ex)
112
            {
113
                throw new Exception("ApplicationConfig ", ex);
114
            }
115
        }
116

    
117
        protected override void OnStart(string[] args)
118
        {
119
            try
120
            {
121
                StartService();
122
            }
123
            catch (Exception ex)
124
            {
125
                logger.Error("ServiceStation Start Error - ", ex);
126
            }
127
        }
128

    
129
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
130
        {
131
            timer.Stop();
132

    
133
            var process = Process.GetProcessesByName("Markus.Service.ConvertProcess");
134

    
135
            for (int i = 0; i < process.Count(); i++)
136
            {
137
                System.Diagnostics.Debug.WriteLine($"Command [{i}]: " , string.Join(" ", process[i].Arguments().CommandLine));
138
            }
139

    
140
            for (int i = 0; i < ServiceStation.AliveConvertQueue.Count; i++)
141
            {
142
                System.Diagnostics.Debug.WriteLine($"AliveConvertItems  [{i}]: ", ServiceStation.AliveConvertQueue[i].ConvertID);
143
            }
144

    
145
            System.Threading.Thread.Sleep(1000);
146
            timer.Start();
147
        }
148

    
149
        System.Timers.Timer timer;
150

    
151
        private void ProcessorAffinityInit_temp()
152
        {
153
            ProcessorAffinityList = new List<long>();
154

    
155
            int processCount = Environment.ProcessorCount;
156
            bool[] bits = new bool[processCount];
157

    
158
            for (int i = 0; i < bits.Count(); i+=3)
159
            {
160
                bits = new bool[processCount];
161
                bits[i] = true;
162
                bits[i+1] = true;
163
                bits[i + 2] = true;
164

    
165
                bits = bits.ToArray();
166

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

    
169
                ProcessorAffinityList.Add(affinity);
170
            }
171
        }
172

    
173
        private void ProcessorAffinityInit()
174
        {
175
            ProcessorAffinityList = new List<long>();
176

    
177
            int processCount = Environment.ProcessorCount;
178
            int AffinityScope = 1;
179

    
180
            if (processCount > MultiProcessCount)
181
            {
182
                AffinityScope = processCount / MultiProcessCount;
183
            }
184

    
185
            for (int i = 0; i < processCount - AffinityScope; i += AffinityScope)
186
            {
187
                var bits = new int[processCount];
188

    
189
                for (int j = i; j < i + AffinityScope; j++)
190
                {
191
                    bits[j] = 1;
192
                }
193

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

    
196
                ProcessorAffinityList.Add(affinity);
197
            }
198
        }
199

    
200
        public bool StartService()
201
        {
202
          
203
            try
204
            {
205
                this.GetApplicationConfig();
206
                logger.Info("Read Config");
207

    
208
                ProcessorAffinityInit();
209

    
210
            }
211
            catch (Exception e)
212
            {
213
                throw new Exception("Stop StartService Error. ", e);
214
            }
215
            try
216
            {
217
                if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libpthread.dll")))
218
                {
219
                    Markus.Library.Installer.Install();
220
                    logger.Info("Markus.Library.Installer Install");
221
                }
222
                else
223
                {
224
                    logger.Info("Markus.Library.Installer Exists.");
225
                }
226
                
227
            }
228
            catch (Exception e)
229
            {
230
                throw new Exception("Stop Installer Error. ", e);
231
            }
232

    
233
            try
234
            {
235
                Stopprocess();
236
                logger.Info("Stopprocess");
237
            }
238
            catch (Exception e)
239
            {
240
                throw new Exception("Stop Process Error. ", e);
241
            }
242

    
243
            try
244
            {
245
                StartWcfService();
246

    
247
                if (gWcfServiceHost.BaseAddresses?.Count() > 0)
248
                {
249
                    gServiceHostAddress = gWcfServiceHost.BaseAddresses.First();
250
                }
251

    
252
                logger.Info($"StartWcfService {gServiceHostAddress}");
253
            }
254
            catch (Exception e)
255
            {
256
                throw new Exception("start Wcf Service Error. ", e);
257
            }
258

    
259
            try
260
            {
261
                setDataBaseWaitingList();
262
                logger.Info("setDataBaseWaitingList");
263
            }
264
            catch (Exception e)
265
            {
266
                throw new Exception("Database Waiting List Error . ", e);
267
            }
268

    
269

    
270
            logger.Info("Start ServiceStation");
271
            //timer = new System.Timers.Timer(10000);
272
            //timer.Elapsed += Timer_Elapsed;
273
            //timer.AutoReset = true;
274
            //timer.Start();
275

    
276
            return true;
277
        }
278

    
279

    
280
        protected override void OnStop()
281
        {
282
            try
283
            {
284
                StopWcfService();
285
                Stopprocess();
286

    
287
                logger.Info("ServiceStation Stop");
288
            }
289
            catch (Exception e)
290
            {
291
                logger.Error("OnStop Error . ", e);
292
            }
293
        }
294

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