markus / ConvertService / ConverterService / ConverterStation / Remoting / ConverterService.cs @ 8acc4862
이력 | 보기 | 이력해설 | 다운로드 (34.6 KB)
1 | 7ca218b3 | KangIngu | using System; |
---|---|---|---|
2 | using System.ComponentModel; |
||
3 | using System.Runtime.Remoting; |
||
4 | using System.Runtime.Remoting.Channels; |
||
5 | using System.Runtime.Remoting.Channels.Tcp; |
||
6 | using System.Threading; |
||
7 | using System.Data.SqlClient; |
||
8 | using System.Linq; |
||
9 | using System.Collections.Generic; |
||
10 | using System.Diagnostics; |
||
11 | using System.Threading.Tasks; |
||
12 | using System.Collections; |
||
13 | using DeepViewDataModel.DataModel; |
||
14 | using DeepViewDataModel.Common; |
||
15 | using System.IO; |
||
16 | using System.Net; |
||
17 | using IConverterPDF; |
||
18 | using DZConverterLib; |
||
19 | using System.Text; |
||
20 | using ConverterStation.Remoting; |
||
21 | using System.Diagnostics.CodeAnalysis; |
||
22 | using System.Runtime.InteropServices; |
||
23 | using System.Security; |
||
24 | using PropertiesType; |
||
25 | using DeepView.Toolkit.GuidGenerator; |
||
26 | using DZConverterLib.Event; |
||
27 | |||
28 | namespace ConverterStation.Service |
||
29 | { |
||
30 | public class ConverterService : InterfaceConverterPDF, IDisposable |
||
31 | { |
||
32 | #region Properties |
||
33 | public TcpChannel channel { get; set; } |
||
34 | public ServiceEventLog _log { get; set; } |
||
35 | private ConverterServiceType SeriviceType { get; set; } |
||
36 | 32e95118 | KangIngu | private string ServiceID { get; set; } |
37 | 7ca218b3 | KangIngu | private string _serviceID { get; set; } |
38 | 32e95118 | KangIngu | private string _ServiceID { get; set; } |
39 | 7ca218b3 | KangIngu | private Thread _ConverterPdfThread { get; set; } |
40 | private Queue<CONVERTER_DOC> _WorkItems { get; set; } |
||
41 | private List<CONVERTER_DOC> _thread { get; set; } |
||
42 | private List<ServiceItem> LstService { get; set; } |
||
43 | public int remotingPort { get; set; } |
||
44 | public string hostName { get; set; } |
||
45 | #endregion |
||
46 | |||
47 | #region inheritance property |
||
48 | private int _MultiProcessCount; |
||
49 | |||
50 | public int MultiProcessCount |
||
51 | { |
||
52 | get |
||
53 | { |
||
54 | if (_MultiProcessCount == 0) |
||
55 | return 1; |
||
56 | else |
||
57 | return _MultiProcessCount; |
||
58 | } |
||
59 | |||
60 | set |
||
61 | { |
||
62 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
63 | { |
||
64 | _serviceID = ServiceID.ToString(); |
||
65 | var _service = _deepview.SERVICE_PROPERTIES.Where(s => s.ID == _serviceID); |
||
66 | |||
67 | if (_service.Count() > 0) |
||
68 | { |
||
69 | _service.First().PROCESS_COUNT = value; |
||
70 | _deepview.SaveChanges(); |
||
71 | _MultiProcessCount = value; |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | #endregion |
||
78 | |||
79 | #region inheritance Method |
||
80 | public void Dispose() |
||
81 | { |
||
82 | if (channel != null) |
||
83 | { |
||
84 | ChannelServices.UnregisterChannel(channel); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | 32e95118 | KangIngu | public ConverterPDFResult Notify(string ProjectNo, string ConverterID) |
89 | 7ca218b3 | KangIngu | { |
90 | try |
||
91 | { |
||
92 | using (DeepView_Entity _Entity = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
93 | { |
||
94 | _serviceID = ConverterID.ToString(); |
||
95 | var _result = from Converter in _Entity.CONVERTER_DOC |
||
96 | where Converter.ID == _serviceID |
||
97 | select Converter; |
||
98 | |||
99 | StartConverterPDFProcess(_result.ToList()); |
||
100 | } |
||
101 | } |
||
102 | catch (Exception ex) |
||
103 | { |
||
104 | SetError(ConverterID, "Notify error 서비스를 시작할 수 없습니다.", ex.ToString(), 0); |
||
105 | return new ConverterPDFResult { ConverterID = ConverterID, ProjectNo = ProjectNo, Exception = ex.ToString() }; |
||
106 | } |
||
107 | |||
108 | return new ConverterPDFResult { ConverterID = ConverterID, ProjectNo = ProjectNo }; |
||
109 | } |
||
110 | |||
111 | 32e95118 | KangIngu | public string[] RunningProcessID() |
112 | 7ca218b3 | KangIngu | { |
113 | string[] _temp = _thread.Select(t => t.ID).ToArray(); |
||
114 | 32e95118 | KangIngu | string[] _return = new string[_temp.Length]; |
115 | 7ca218b3 | KangIngu | |
116 | for (int i = 0; i < _temp.Length; i++ ) |
||
117 | { |
||
118 | 32e95118 | KangIngu | _return[i] = _temp[i]; |
119 | 7ca218b3 | KangIngu | } |
120 | |||
121 | return _return; |
||
122 | //return _thread.Select(t => Guid.Parse(t.ID)).ToArray(); |
||
123 | } |
||
124 | |||
125 | public ServiceInfo ServiceInfomation() |
||
126 | { |
||
127 | ServiceInfo _info = new ServiceInfo(); |
||
128 | StringBuilder bl = new StringBuilder(); |
||
129 | try |
||
130 | { |
||
131 | _info.ServiceUri = ServiceUri(); |
||
132 | } |
||
133 | catch (Exception ex) |
||
134 | { |
||
135 | bl.AppendLine("<ServiceUri Error>"); |
||
136 | bl.AppendLine(ex.ToString()); |
||
137 | bl.AppendLine("_________________________"); |
||
138 | } |
||
139 | |||
140 | try |
||
141 | { |
||
142 | _info.ServiceID = ServiceID; |
||
143 | } |
||
144 | catch (Exception ex) |
||
145 | { |
||
146 | bl.AppendLine("ServiceID Error :"); |
||
147 | bl.AppendLine("service ID : " + ServiceID.ToString()); |
||
148 | bl.AppendLine(ex.ToString()); |
||
149 | bl.AppendLine("_________________________"); |
||
150 | } |
||
151 | |||
152 | try |
||
153 | { |
||
154 | _info.StorageInfo = TileSorceStorageInfomation(); ; |
||
155 | } |
||
156 | catch (Exception ex) |
||
157 | { |
||
158 | bl.AppendLine("TileSorceStorageInfomation Error :"); |
||
159 | bl.AppendLine(ex.ToString()); |
||
160 | bl.AppendLine("_________________________"); |
||
161 | } |
||
162 | |||
163 | if (bl.Length > 0) |
||
164 | _info.Exception = bl.ToString(); |
||
165 | |||
166 | return _info; |
||
167 | } |
||
168 | |||
169 | public TileSorceStorageInfo[] TileSorceStorageInfomation() |
||
170 | { |
||
171 | List<TileSorceStorageInfo> _info = new List<TileSorceStorageInfo>(); |
||
172 | string _TestDir = "createTestDir"; |
||
173 | |||
174 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
175 | { |
||
176 | var _storage = (from proper in _deepview.PROPERTIES |
||
177 | where proper.TYPE == PropertiesType.PropertiesType.Const_TileSorceStorage |
||
178 | group proper by proper.VALUE into p |
||
179 | select p.Key |
||
180 | ).ToList(); |
||
181 | |||
182 | _storage.ForEach(s => |
||
183 | { |
||
184 | TileSorceStorageInfo _storageInfo = new TileSorceStorageInfo(); |
||
185 | _storageInfo.FolderName = s; |
||
186 | _storageInfo.FreeSpace = FreeSpace(s); |
||
187 | |||
188 | try |
||
189 | { |
||
190 | DirectoryInfo _Dirinfo = new DirectoryInfo(s); |
||
191 | |||
192 | var _dir = _Dirinfo.EnumerateDirectories().Where(d => d.Name == _TestDir); |
||
193 | |||
194 | if (_dir.Count() == 0) |
||
195 | _Dirinfo = _Dirinfo.CreateSubdirectory("createTestDir"); |
||
196 | else |
||
197 | _Dirinfo = _dir.First(); |
||
198 | |||
199 | if (_Dirinfo.Exists) |
||
200 | _Dirinfo.Delete(true); |
||
201 | |||
202 | _Dirinfo = new DirectoryInfo(_Dirinfo.FullName); |
||
203 | |||
204 | if (!_Dirinfo.Exists) |
||
205 | _storageInfo.Access = true; |
||
206 | } |
||
207 | catch (Exception ex) |
||
208 | { |
||
209 | throw; |
||
210 | } |
||
211 | |||
212 | _info.Add(_storageInfo); |
||
213 | }); |
||
214 | } |
||
215 | |||
216 | return _info.ToArray(); |
||
217 | } |
||
218 | |||
219 | 32e95118 | KangIngu | public bool ServiceOn(string ServiceID) |
220 | 7ca218b3 | KangIngu | { |
221 | try |
||
222 | { |
||
223 | SetSubPassService(); |
||
224 | } |
||
225 | catch (Exception ex) |
||
226 | { |
||
227 | SendError("Service On Error", ex.ToString(), 0); |
||
228 | } |
||
229 | |||
230 | return true; |
||
231 | } |
||
232 | #endregion |
||
233 | |||
234 | #region Constructors |
||
235 | |||
236 | public ConverterService(ServiceEventLog eventLog) |
||
237 | { |
||
238 | var IsServerSet = SetServerSetting(); |
||
239 | |||
240 | _log = eventLog; |
||
241 | |||
242 | if (_WorkItems == null) |
||
243 | _WorkItems = new Queue<CONVERTER_DOC>(); |
||
244 | |||
245 | if (_thread == null) |
||
246 | _thread = new List<CONVERTER_DOC>(); |
||
247 | |||
248 | if (IsServerSet) |
||
249 | { |
||
250 | InitWork(); |
||
251 | } |
||
252 | else |
||
253 | _log.Write("Converter Service 호스트 이름을 찾을 수 없습니다."); |
||
254 | |||
255 | } |
||
256 | |||
257 | #endregion |
||
258 | |||
259 | #region Event of DZConverter |
||
260 | void dz_EndConverter(object sender, EndConverterEventArgs e) |
||
261 | { |
||
262 | var _T = _thread.Where(t => t.ID == (sender as DZConverterLib.DZConverter).ConverterItem.ID); |
||
263 | |||
264 | if (_T.Count() > 0) |
||
265 | { |
||
266 | 32e95118 | KangIngu | string _id = _T.First().ID; |
267 | 7ca218b3 | KangIngu | SetConveterState(_id, IConverterPDF.ConverterStatus.Success); |
268 | _thread.Remove((sender as DZConverterLib.DZConverter).ConverterItem); |
||
269 | (sender as DZConverterLib.DZConverter).ConverterMakeError -= new EventHandler<MakeConverterErrorArgs>(dz_ConverterMakeError); |
||
270 | (sender as DZConverterLib.DZConverter).EndConverter -= new EventHandler<EndConverterEventArgs>(dz_EndConverter); |
||
271 | (sender as DZConverterLib.DZConverter).Dispose(); |
||
272 | } |
||
273 | else |
||
274 | { |
||
275 | 32e95118 | KangIngu | SetError((sender as DZConverterLib.DZConverter).ConverterItem.ID, "Thread Count 0", "Thread가 없습니다", 0); |
276 | 7ca218b3 | KangIngu | } |
277 | |||
278 | if (_WorkItems.Count() == 0 && this.SeriviceType == ConverterServiceType.Main) |
||
279 | ReAttemptConverterPDF(); |
||
280 | } |
||
281 | |||
282 | void dz_ConverterMakeError(object sender, MakeConverterErrorArgs e) |
||
283 | { |
||
284 | SetError(e.ConverterID, e.ErrorCode, e._Exception, e.Level); |
||
285 | |||
286 | try |
||
287 | { |
||
288 | if (e.ThreadStop) |
||
289 | { |
||
290 | _thread.Remove((sender as DZConverterLib.DZConverter).ConverterItem); |
||
291 | (sender as DZConverterLib.DZConverter).ConverterMakeError -= new EventHandler<MakeConverterErrorArgs>(dz_ConverterMakeError); |
||
292 | (sender as DZConverterLib.DZConverter).EndConverter -= new EventHandler<EndConverterEventArgs>(dz_EndConverter); |
||
293 | (sender as DZConverterLib.DZConverter).Dispose(); |
||
294 | } |
||
295 | } |
||
296 | catch (Exception ex) |
||
297 | { |
||
298 | SetError(e.ConverterID, "dz_ConverterMakeError에서 Thread 제거 불가", ex.ToString(), 0); |
||
299 | } |
||
300 | } |
||
301 | #endregion |
||
302 | |||
303 | #region Method |
||
304 | private void CreateServerSetting() |
||
305 | { |
||
306 | hostName = System.Environment.MachineName; |
||
307 | |||
308 | using (DeepView_Entity de = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
309 | { |
||
310 | var serviceProperties = de.SERVICE_PROPERTIES.Where(host => host.HOST_NAME == hostName); |
||
311 | |||
312 | if (serviceProperties.Count() == 0) |
||
313 | { |
||
314 | SERVICE_PROPERTIES serviceProperty = new SERVICE_PROPERTIES(); |
||
315 | serviceProperty.ID = GuidGenerator.GetUniqueGuid().ToString(); |
||
316 | serviceProperty.HOST_NAME = hostName; |
||
317 | |||
318 | IPAddress ipaddress = Dns.Resolve(Dns.GetHostName()).AddressList[0]; |
||
319 | serviceProperty.SERVICE_ADDRESS = ipaddress.ToString(); |
||
320 | serviceProperty.SERVICE_PORT = Properties.Settings.Default.RemotingPort; |
||
321 | serviceProperty.PROCESS_COUNT = Properties.Settings.Default.MultipleConverterCount; |
||
322 | serviceProperty.SERVICET_TYPE = (int)PropertiesType.ConverterServiceType.Sub; |
||
323 | |||
324 | de.SERVICE_PROPERTIES.AddObject(serviceProperty); |
||
325 | de.SaveChanges(); |
||
326 | |||
327 | } |
||
328 | } |
||
329 | } |
||
330 | |||
331 | public bool SetServerSetting() |
||
332 | { |
||
333 | bool SettingCompleted = false; |
||
334 | |||
335 | hostName = System.Environment.MachineName; |
||
336 | |||
337 | try |
||
338 | { |
||
339 | using (DeepView_Entity de = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
340 | { |
||
341 | var serviceProperties = de.SERVICE_PROPERTIES.Where(host => host.HOST_NAME == hostName); |
||
342 | |||
343 | if (serviceProperties.Count() > 0) |
||
344 | { |
||
345 | var serviceProperty = serviceProperties.First(); |
||
346 | remotingPort = int.Parse(serviceProperty.SERVICE_PORT.ToString()); |
||
347 | 32e95118 | KangIngu | ServiceID = serviceProperty.ID; |
348 | 7ca218b3 | KangIngu | MultiProcessCount = serviceProperty.PROCESS_COUNT; |
349 | SeriviceType = (ConverterServiceType)serviceProperty.SERVICET_TYPE; |
||
350 | } |
||
351 | else |
||
352 | { |
||
353 | CreateServerSetting(); |
||
354 | } |
||
355 | |||
356 | SettingCompleted = true; |
||
357 | } |
||
358 | } |
||
359 | catch (Exception ex) |
||
360 | { |
||
361 | SettingCompleted = false; |
||
362 | _log.Write("ServerSetting Error : " + ex.ToString()); |
||
363 | } |
||
364 | |||
365 | return SettingCompleted; |
||
366 | } |
||
367 | |||
368 | private static long FreeSpace(string folderName) |
||
369 | { |
||
370 | if (string.IsNullOrEmpty(folderName)) |
||
371 | { |
||
372 | throw new ArgumentNullException("folderName"); |
||
373 | } |
||
374 | |||
375 | if (!folderName.EndsWith("\\")) |
||
376 | { |
||
377 | folderName += '\\'; |
||
378 | } |
||
379 | |||
380 | long free = 0, dummy1 = 0, dummy2 = 0; |
||
381 | |||
382 | if (GetDiskFreeSpaceEx(folderName, ref free, ref dummy1, ref dummy2)) |
||
383 | { |
||
384 | return free; |
||
385 | } |
||
386 | else |
||
387 | { |
||
388 | return -1; |
||
389 | } |
||
390 | } |
||
391 | |||
392 | public void InitWork() |
||
393 | { |
||
394 | Thread _initThread = new Thread(new ThreadStart(Init)); |
||
395 | _initThread.Start(); |
||
396 | } |
||
397 | |||
398 | public void InitializeProcess() |
||
399 | { |
||
400 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
401 | { |
||
402 | _serviceID = ServiceID.ToString(); |
||
403 | var _converterItems = _deepview.CONVERTER_DOC.Where(c => c.SERVICE_ID == _serviceID && |
||
404 | c.STATUS != (int)ConverterStatus.Success && |
||
405 | c.RECONVERTER < 3); |
||
406 | foreach (var item in _converterItems) |
||
407 | { |
||
408 | item.STATUS = 0; |
||
409 | item.RECONVERTER = 1; |
||
410 | item.SERVICE_ID = null; |
||
411 | } |
||
412 | |||
413 | if (_converterItems.Count() > 0) |
||
414 | _deepview.SaveChanges(); |
||
415 | } |
||
416 | } |
||
417 | |||
418 | public void Init() |
||
419 | { |
||
420 | try |
||
421 | { |
||
422 | channel = new TcpChannel(remotingPort); |
||
423 | ChannelServices.RegisterChannel(channel, false); |
||
424 | RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemConverterPDFObject), "remConverterPDF", WellKnownObjectMode.Singleton); |
||
425 | IConverterPDF.Cache.Attach(this); |
||
426 | |||
427 | _log.Write("Notice <Service Infomation>"); |
||
428 | _log.Write("Port" + remotingPort.ToString()); |
||
429 | _log.Write("HostName" + hostName); |
||
430 | _log.Write("Service ID :" + ServiceID.ToString()); |
||
431 | _log.Write("Service Type :" + SeriviceType.ToString()); |
||
432 | _log.Write("Service Host :" + hostName); |
||
433 | |||
434 | if (hostName != null) |
||
435 | { |
||
436 | InitializeProcess(); |
||
437 | |||
438 | foreach (var item in TileSorceStorageInfomation()) |
||
439 | { |
||
440 | _log.Write("Storage(" + item.FolderName + ") " + |
||
441 | (item.FreeSpace / 1024f) / 1024f / 1024f + "GB"); |
||
442 | } |
||
443 | |||
444 | //0번일 때 |
||
445 | if (this.SeriviceType == ConverterServiceType.Main) |
||
446 | { |
||
447 | //Sub |
||
448 | _log.Write("SetSubPassService :" + SetSubPassService()); |
||
449 | ReAttemptConverterPDF(); |
||
450 | } |
||
451 | else |
||
452 | { |
||
453 | List<SERVICE_PROPERTIES> _service = new List<SERVICE_PROPERTIES>(); |
||
454 | |||
455 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
456 | { |
||
457 | _service = _deepview.SERVICE_PROPERTIES.ToList(); |
||
458 | |||
459 | _log.Write("_service :" + _service.First().ToString()); |
||
460 | var _mainService = _service.Where(s => s.HOST_NAME == hostName && |
||
461 | (ConverterServiceType)s.SERVICET_TYPE == ConverterServiceType.Main); |
||
462 | |||
463 | _log.Write("_mainService_count :" + _mainService.Count()); |
||
464 | if (_mainService.Count() > 0) |
||
465 | { |
||
466 | var mainservice = _mainService.First(); |
||
467 | |||
468 | try |
||
469 | { |
||
470 | RemConverterPDFObject rem = (RemConverterPDFObject)Activator.GetObject(typeof(RemConverterPDFObject), |
||
471 | "tcp://" + mainservice.SERVICE_ADDRESS + ":" |
||
472 | + mainservice.SERVICE_PORT + "/remConverterPDF"); |
||
473 | |||
474 | if (rem != null) |
||
475 | rem.ServiceOn(ServiceID); //SetSubPassService() 메서드 호출 |
||
476 | } |
||
477 | catch (Exception ex) |
||
478 | { |
||
479 | _log.Write("Main Connection Error : " + ex.ToString()); |
||
480 | } |
||
481 | } |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | else |
||
486 | { |
||
487 | _log.Write("Converter Service : " + hostName + "데이터베이스에서 호스트 정보를 찾을 수 없습니다."); |
||
488 | } |
||
489 | |||
490 | //SendError("Start Service Info.", _ServiceStartRpt.ToString(), 0); |
||
491 | } |
||
492 | catch (Exception ex) |
||
493 | { |
||
494 | if (_log != null) |
||
495 | _log.Write("RemConverterStation" + "02156"); |
||
496 | } |
||
497 | } |
||
498 | |||
499 | private Uri ServiceUri() |
||
500 | { |
||
501 | Uri _result = null; |
||
502 | |||
503 | try |
||
504 | { |
||
505 | if (channel.ChannelData != null) |
||
506 | if (channel.ChannelData.GetType() == typeof(ChannelDataStore)) |
||
507 | { |
||
508 | var _uris = (channel.ChannelData as ChannelDataStore).ChannelUris; |
||
509 | |||
510 | if (_uris.Count() > 0) |
||
511 | _result = new Uri(_uris[0]); |
||
512 | } |
||
513 | } |
||
514 | catch (Exception) |
||
515 | { |
||
516 | new Exception("service Uri Eorror"); |
||
517 | } |
||
518 | |||
519 | return _result; |
||
520 | } |
||
521 | |||
522 | public string SetSubPassService() |
||
523 | { |
||
524 | if (LstService == null) LstService = new List<ServiceItem>(); |
||
525 | |||
526 | StringBuilder _ServiceStartRpt = new StringBuilder(); |
||
527 | List<SERVICE_PROPERTIES> _service = new List<SERVICE_PROPERTIES>(); |
||
528 | |||
529 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
530 | { |
||
531 | //_service = _deepview.ServiceProperties.Where(f => f.HostName.ToLower() |
||
532 | // == hostName.ToLower()).ToList(); |
||
533 | |||
534 | _service = _deepview.SERVICE_PROPERTIES.ToList(); |
||
535 | } |
||
536 | _serviceID = ServiceID.ToString(); |
||
537 | //강인구 |
||
538 | _service.ForEach(s => |
||
539 | { |
||
540 | 32e95118 | KangIngu | _ServiceID = s.ID; |
541 | 7ca218b3 | KangIngu | if (s.ID != _serviceID && LstService.Where(ser => ser.ServiceID == _ServiceID).Count() == 0) |
542 | { |
||
543 | System.Runtime.Remoting.Channels.IChannel _ch = ChannelServices.GetChannel("tcp"); |
||
544 | |||
545 | if (_ch == null) |
||
546 | { |
||
547 | TcpChannel chan = new TcpChannel(); |
||
548 | ChannelServices.RegisterChannel(chan, false); |
||
549 | } |
||
550 | // Create an instance of the remote object |
||
551 | RemConverterPDFObject rem = (RemConverterPDFObject)Activator.GetObject(typeof(RemConverterPDFObject), |
||
552 | "tcp://" + s.SERVICE_ADDRESS + ":" + s.SERVICE_PORT + "/remConverterPDF"); |
||
553 | |||
554 | try |
||
555 | { |
||
556 | rem.MultiProcessCount = s.PROCESS_COUNT; |
||
557 | |||
558 | var _Info = rem.ServiceInfomation(); |
||
559 | |||
560 | _ServiceStartRpt.AppendLine(""); |
||
561 | _ServiceStartRpt.AppendLine("Service Type :" + ((ConverterServiceType)s.SERVICET_TYPE).ToString()); |
||
562 | _ServiceStartRpt.AppendLine("Service ID :" + s.ID.ToString()); |
||
563 | _ServiceStartRpt.AppendLine("Service Address :" + _Info.ServiceUri); |
||
564 | |||
565 | |||
566 | foreach (var item in _Info.StorageInfo) |
||
567 | { |
||
568 | _ServiceStartRpt.AppendLine("Storage(" + item.FolderName + ") Access:" + item.Access.ToString()); |
||
569 | } |
||
570 | |||
571 | var _runningItem = rem.RunningProcessID(); |
||
572 | |||
573 | _ServiceStartRpt.AppendLine(""); |
||
574 | _ServiceStartRpt.AppendLine("Running Item"); |
||
575 | |||
576 | _runningItem.ToList().ForEach(f => |
||
577 | { |
||
578 | _ServiceStartRpt.AppendLine(f.ToString()); |
||
579 | }); |
||
580 | //여기 |
||
581 | 32e95118 | KangIngu | LstService.Add(new ServiceItem { RemotingSerivce = rem, ServiceID = s.ID }); |
582 | 7ca218b3 | KangIngu | } |
583 | catch (Exception ex) |
||
584 | { |
||
585 | StringBuilder _bl = new StringBuilder(); |
||
586 | _bl.AppendLine("<Sub Service Infomation Error>"); |
||
587 | _bl.AppendLine("Service ID :" + s.ID.ToString()); |
||
588 | _bl.AppendLine("Service Address :" + s.SERVICE_ADDRESS); |
||
589 | _bl.AppendLine("Service Port :" + s.SERVICE_PORT); |
||
590 | _bl.AppendLine(ex.ToString()); |
||
591 | SendError("Service Not Found", _bl.ToString(), 0); |
||
592 | } |
||
593 | } |
||
594 | }); |
||
595 | |||
596 | return _ServiceStartRpt.ToString(); |
||
597 | } |
||
598 | |||
599 | public void ReAttemptConverterPDF() |
||
600 | { |
||
601 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
602 | { |
||
603 | _log.Write("_deepview in"); |
||
604 | 0157b158 | taeseongkim | var _ConverterList = _deepview.CONVERTER_DOC.Where(converter |
605 | => converter.PROJECT_NO != "130351" && |
||
606 | (converter.STATUS != (int)IConverterPDF.ConverterStatus.Success |
||
607 | 7ca218b3 | KangIngu | && converter.STATUS != (int)IConverterPDF.ConverterStatus.Error && converter.SERVICE_ID == null) |
608 | || (converter.STATUS == (int)IConverterPDF.ConverterStatus.Error && converter.RECONVERTER < 4)); |
||
609 | |||
610 | _log.Write("_ConverterList count" + _ConverterList.Count()); |
||
611 | if (_ConverterList.Count() > 0) |
||
612 | { |
||
613 | var _exceptItem = _ConverterList.Except(_WorkItems.ToList()); |
||
614 | |||
615 | List<CONVERTER_DOC> _insertItem = _exceptItem.Except(_thread).ToList(); |
||
616 | List<CONVERTER_DOC> _workingItem = new List<CONVERTER_DOC>(); |
||
617 | |||
618 | if (LstService != null) |
||
619 | { |
||
620 | for (int i = 0; i < LstService.Count(); i++) |
||
621 | { |
||
622 | //var _guid = LstService[i].RemotingSerivce.RunningProcessID(); |
||
623 | string _guid = LstService[i].RemotingSerivce.RunningProcessID().ToString(); |
||
624 | |||
625 | |||
626 | var _lstconvert = from c in _deepview.CONVERTER_DOC |
||
627 | where _guid.Contains(c.ID) |
||
628 | select c; |
||
629 | |||
630 | if (_lstconvert.Count() > 0) |
||
631 | _workingItem.AddRange(_lstconvert.ToList()); |
||
632 | } |
||
633 | } |
||
634 | |||
635 | _workingItem.ForEach(f => |
||
636 | { |
||
637 | var _items = _insertItem.Where(item => item.PROJECT_NO == f.PROJECT_NO && |
||
638 | item.DOCUMENT_ID == f.DOCUMENT_ID); |
||
639 | |||
640 | if (_items.Count() > 0) |
||
641 | { |
||
642 | CONVERTER_DOC _SubWorkingitem = _items.First(); |
||
643 | _insertItem.Remove(_SubWorkingitem); |
||
644 | } |
||
645 | }); |
||
646 | |||
647 | StartConverterPDFProcess(_insertItem); |
||
648 | } |
||
649 | } |
||
650 | } |
||
651 | |||
652 | private void StartConverterPDFProcess(List<CONVERTER_DOC> lstConverterPDF) |
||
653 | { |
||
654 | if (lstConverterPDF.Count() > 0) |
||
655 | { |
||
656 | foreach (var item in lstConverterPDF) |
||
657 | { |
||
658 | System.Diagnostics.Debug.WriteLine("ConverterPDF Item 추가" + item.ID); |
||
659 | _WorkItems.Enqueue(item); |
||
660 | } |
||
661 | |||
662 | if (_ConverterPdfThread == null) |
||
663 | { |
||
664 | _ConverterPdfThread = new Thread(new ThreadStart(ConverterPDFProcess)); |
||
665 | _ConverterPdfThread.SetApartmentState(ApartmentState.MTA); |
||
666 | _ConverterPdfThread.Start(); |
||
667 | } |
||
668 | } |
||
669 | } |
||
670 | |||
671 | private void ConverterPDFProcess() |
||
672 | { |
||
673 | while (_WorkItems.Count() > 0) |
||
674 | { |
||
675 | if (_thread.Count() < _MultiProcessCount) |
||
676 | { |
||
677 | CONVERTER_DOC _item = _WorkItems.Dequeue(); |
||
678 | var _identPathItem = _thread.Where(item => item.DOCUMENT_URL == _item.DOCUMENT_URL); |
||
679 | |||
680 | if (_identPathItem.Count() > 0) |
||
681 | { |
||
682 | _WorkItems.Enqueue(_item); |
||
683 | } |
||
684 | else |
||
685 | { |
||
686 | DZConverter dz = new DZConverter(); |
||
687 | dz.ConverterMakeError += new EventHandler<MakeConverterErrorArgs>(dz_ConverterMakeError); |
||
688 | dz.EndConverter += new EventHandler<EndConverterEventArgs>(dz_EndConverter); |
||
689 | |||
690 | Thread _stathread = new Thread(new ParameterizedThreadStart(dz.GetSingleFile)); |
||
691 | _stathread.Name = _item.ID.ToString(); |
||
692 | _stathread.SetApartmentState(ApartmentState.MTA); //multithreaded apartment(MTA) |
||
693 | _stathread.Start(_item); //Parameter 전달 |
||
694 | _thread.Add(_item); |
||
695 | |||
696 | 32e95118 | KangIngu | SetConveterState(_item.ID, IConverterPDF.ConverterStatus.Wait); |
697 | 7ca218b3 | KangIngu | System.Diagnostics.Debug.WriteLine("Status Change : " + _item.ID); |
698 | } |
||
699 | } |
||
700 | else |
||
701 | { |
||
702 | if (this.SeriviceType == ConverterServiceType.Main) |
||
703 | { |
||
704 | bool _subpass = false; |
||
705 | CONVERTER_DOC _item = _WorkItems.Dequeue(); |
||
706 | |||
707 | try |
||
708 | { |
||
709 | for (int i = 0; i < LstService.Count; i++) |
||
710 | { |
||
711 | int maxCount = LstService[i].RemotingSerivce.MultiProcessCount; |
||
712 | 32e95118 | KangIngu | string[] _id = LstService[i].RemotingSerivce.RunningProcessID(); |
713 | 7ca218b3 | KangIngu | |
714 | if (maxCount > _id.Count()) |
||
715 | { |
||
716 | 32e95118 | KangIngu | var _conveter = LstService[i].RemotingSerivce.SetConverterPDF(_item.PROJECT_NO, _item.ID); |
717 | 7ca218b3 | KangIngu | |
718 | if (_conveter.Exception == null) |
||
719 | { |
||
720 | _subpass = true; |
||
721 | break; |
||
722 | } |
||
723 | } |
||
724 | } |
||
725 | |||
726 | } |
||
727 | catch (Exception ex) |
||
728 | { |
||
729 | //SendError("subPass Error :" + ex.ToString()); |
||
730 | } |
||
731 | |||
732 | if (!_subpass) _WorkItems.Enqueue(_item); |
||
733 | } |
||
734 | } |
||
735 | |||
736 | Thread.Sleep(10); |
||
737 | } |
||
738 | _ConverterPdfThread = null; |
||
739 | } |
||
740 | |||
741 | 32e95118 | KangIngu | private void SetConveterState(string ConverterID, IConverterPDF.ConverterStatus Status) |
742 | 7ca218b3 | KangIngu | { |
743 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
744 | { |
||
745 | _serviceID = ConverterID.ToString(); |
||
746 | var converterList = _deepview.CONVERTER_DOC.Where(Converter => Converter.ID == _serviceID); |
||
747 | |||
748 | if (converterList.Count() > 0) |
||
749 | { |
||
750 | converterList.First().STATUS = (int)Status; |
||
751 | |||
752 | if (Status == ConverterStatus.Create) |
||
753 | { |
||
754 | converterList.First().START_DATETIME = DateTime.Now; |
||
755 | } |
||
756 | else if (Status == ConverterStatus.Success) |
||
757 | { |
||
758 | converterList.First().END_DATETIME = DateTime.Now; |
||
759 | } |
||
760 | |||
761 | converterList.First().SERVICE_ID = ServiceID.ToString(); |
||
762 | |||
763 | _deepview.SaveChanges(); |
||
764 | } |
||
765 | } |
||
766 | } |
||
767 | |||
768 | 32e95118 | KangIngu | private void SetError(string ConverterID, string Msg, string Ex, int level) |
769 | 7ca218b3 | KangIngu | { |
770 | try |
||
771 | { |
||
772 | using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
773 | { |
||
774 | _serviceID = ConverterID.ToString(); |
||
775 | var ConverterList = _deepview.CONVERTER_DOC.Where(converter => converter.ID == _serviceID); |
||
776 | |||
777 | if (ConverterList.Count() > 0) |
||
778 | { |
||
779 | ConverterList.First().STATUS = (int)IConverterPDF.ConverterStatus.Error; |
||
780 | ConverterList.First().EXCEPTION = Msg; |
||
781 | _deepview.SaveChanges(); |
||
782 | } |
||
783 | } |
||
784 | } |
||
785 | catch (Exception ex) |
||
786 | { |
||
787 | _log.Write("database insert error" + "100"); |
||
788 | } |
||
789 | |||
790 | try |
||
791 | { |
||
792 | StringBuilder _bl = new StringBuilder(); |
||
793 | _bl.AppendLine("ConverterPDFID : " + ConverterID.ToString()); |
||
794 | _bl.AppendLine(Msg); |
||
795 | |||
796 | //SendError(_bl.ToString(), Ex, level); |
||
797 | } |
||
798 | catch (Exception ex) |
||
799 | { |
||
800 | _log.Write("SendMail Error" + "1023"); |
||
801 | } |
||
802 | } |
||
803 | |||
804 | |||
805 | void SendError(string Msg, string ex, int Level) |
||
806 | { |
||
807 | List<EmailModel> toEmailModels = new List<EmailModel>(); |
||
808 | |||
809 | EmailModel fromEmailModel = null; |
||
810 | |||
811 | List<string> _lstID = new List<string>(); |
||
812 | |||
813 | if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.ErrorNoticeID)) |
||
814 | { |
||
815 | using (DeepView_Entity de = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString())) |
||
816 | { |
||
817 | //var errorUserList = de.ToEmail.Where(f => f.ServiceID.Value == ServiceID).ToList(); |
||
818 | ////수신자 |
||
819 | //errorUserList.ForEach(emailItem => |
||
820 | //{ |
||
821 | // toEmailModels.Add(new EmailModel() |
||
822 | // { |
||
823 | // Host = emailItem.Host, |
||
824 | // UserID = emailItem.UserID, |
||
825 | // ServiceId = emailItem.ServiceID.Value |
||
826 | // }); |
||
827 | //}); |
||
828 | |||
829 | ////발신자 |
||
830 | //var emailModel = de.FromEmail.Where(fo => fo.ServiceID == ServiceID); |
||
831 | |||
832 | //if (emailModel.Count() > 0) |
||
833 | //{ |
||
834 | // var emailItem = emailModel.First(); |
||
835 | // fromEmailModel = new EmailModel() |
||
836 | // { |
||
837 | // Host = emailItem.Host, |
||
838 | // UserID = emailItem.UserID, |
||
839 | // Smtp = emailItem.Smtp, |
||
840 | // ServiceId = emailItem.ServiceID.Value, |
||
841 | // SmtpPort = emailItem.SmtpPort.Value |
||
842 | // }; |
||
843 | //} |
||
844 | } |
||
845 | |||
846 | |||
847 | //if (fromEmailModel != null && toEmailModels.Count > 0) |
||
848 | //{ |
||
849 | // string header = "<Converter Service Error Notice>"; |
||
850 | |||
851 | // EmailClient.EmailClient _client = |
||
852 | // new EmailClient.EmailClient(fromEmailModel.UserID, fromEmailModel.Host); |
||
853 | |||
854 | // _client.Smtp = fromEmailModel.Smtp; |
||
855 | // _client.SmtpPort = fromEmailModel.SmtpPort; |
||
856 | // _client.IsSSL = true; |
||
857 | // _client.Send(toEmailModels, header, Msg, false); |
||
858 | //} |
||
859 | } |
||
860 | } |
||
861 | #endregion |
||
862 | |||
863 | #region extern method |
||
864 | [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity] |
||
865 | [DllImport("Kernel32", SetLastError = true, CharSet = CharSet.Auto)] |
||
866 | [return: MarshalAs(UnmanagedType.Bool)] |
||
867 | private static extern bool GetDiskFreeSpaceEx |
||
868 | ( |
||
869 | string lpszPath, // Must name a folder, must end with '\'. |
||
870 | ref long lpFreeBytesAvailable, |
||
871 | ref long lpTotalNumberOfBytes, |
||
872 | ref long lpTotalNumberOfFreeBytes |
||
873 | ); |
||
874 | |||
875 | #endregion |
||
876 | |||
877 | //public void Init2() |
||
878 | //{ |
||
879 | // try |
||
880 | // { |
||
881 | // StringBuilder _ServiceStartRpt = new StringBuilder(); |
||
882 | // _ServiceStartRpt.AppendLine("<Service Infomation>"); |
||
883 | |||
884 | // int _RemotingPort = Properties.Settings.Default.RemotingPort; |
||
885 | // channel = new TcpChannel(_RemotingPort); |
||
886 | // ChannelServices.RegisterChannel(channel, false); |
||
887 | // RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemConverterPDFObject), |
||
888 | // "remConverterPDF", WellKnownObjectMode.Singleton); |
||
889 | |||
890 | // IConverterPDF.Cache.Attach(this); |
||
891 | // CheckWaitItem(); |
||
892 | |||
893 | // } |
||
894 | // catch (Exception ex) |
||
895 | // { |
||
896 | // if (_log != null) |
||
897 | // _log.Write("RemConverterStation : " + ex.ToString()); |
||
898 | // } |
||
899 | //} |
||
900 | |||
901 | } |
||
902 | } |