Service.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.ServiceProcess;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Timers;
  15. namespace AipGateway.FileJob.Scheduler
  16. {
  17. public partial class Service : ServiceBase
  18. {
  19. private const string settingDeleteMinMinute = "DeleteMinMinute";
  20. private const string settingDelete = "deleteSettings";
  21. private int DefaultDeleteMinute = 60 * 24;
  22. private string[] DeletePathsToWatchs = null;
  23. private int[] DeleteMinutes = null;
  24. private readonly ManualResetEvent stopEvent = null;
  25. private System.Timers.Timer deletePathTimer = null;
  26. public Service()
  27. {
  28. InitializeComponent();
  29. loadConfiguration();
  30. }
  31. private void loadConfiguration()
  32. {
  33. int deleteMinMinute = 30;
  34. NameValueCollection nvc = (NameValueCollection)ConfigurationManager.AppSettings;
  35. if (nvc.AllKeys.Contains(settingDeleteMinMinute))
  36. {
  37. string tmpMinute = nvc[settingDeleteMinMinute];
  38. deleteMinMinute = Helper.TryInt(tmpMinute, deleteMinMinute);
  39. if (deleteMinMinute < 30)
  40. {
  41. Console.WriteLine($"삭제할 파일 생성 시간 기준이 너무작아 [{deleteMinMinute}] 최소값인 {deleteMinMinute}분으로 다시 설정합니다.");
  42. deleteMinMinute = 30;
  43. }
  44. }
  45. try
  46. {
  47. var deleteSettings = ConfigurationManager.GetSection(settingDelete) as NameValueCollection;
  48. if (deleteSettings.Count == 0)
  49. {
  50. Console.WriteLine("등록되어 있는 작업 정보가 없습니다.");
  51. }
  52. else
  53. {
  54. int jobCnt = 0;
  55. string[] tmpDeletePathsToWatchs = new string[deleteSettings.Count];
  56. int[] tmpDeleteMinutes = new int[deleteSettings.Count];
  57. foreach (var key in deleteSettings.AllKeys)
  58. {
  59. tmpDeletePathsToWatchs[jobCnt] = key;
  60. tmpDeleteMinutes[jobCnt] = Helper.TryInt(deleteSettings[key], DefaultDeleteMinute);
  61. }
  62. DeletePathsToWatchs = tmpDeletePathsToWatchs;
  63. DeleteMinutes = tmpDeleteMinutes;
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. Console.WriteLine(ex.ToString());
  69. DeletePathsToWatchs = null;
  70. DeleteMinutes = null;
  71. }
  72. }
  73. protected override void OnStart(string[] args)
  74. {
  75. deletePathTimer = new System.Timers.Timer();
  76. deletePathTimer.Interval = 60 * 1000; // 1시간마다 실행
  77. deletePathTimer.Elapsed += Timer_Elapsed;
  78. deletePathTimer.Start();
  79. serviceLoops();
  80. }
  81. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  82. {
  83. try
  84. {
  85. serviceLoops();
  86. }
  87. catch (Exception ex)
  88. {
  89. }
  90. }
  91. private void serviceLoops()
  92. {
  93. try
  94. {
  95. if (DeletePathsToWatchs == null || DeletePathsToWatchs.Length == 0)
  96. {
  97. Console.WriteLine("등록되어 있는 작업 정보가 없습니다. 설정정보를 다시 확인합니다.");
  98. loadConfiguration();
  99. return;
  100. }
  101. for (int ii = 0; ii < DeletePathsToWatchs.Length; ii++)
  102. {
  103. Console.WriteLine($"Delete Watch Path: {DeletePathsToWatchs[ii]}");
  104. DeletePathFiles(DeletePathsToWatchs[ii], DeleteMinutes[ii]);
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. }
  110. }
  111. private void DeletePathFiles(string pathDir, int deleteMin)
  112. {
  113. if (System.IO.File.Exists(pathDir))
  114. {
  115. Console.WriteLine($"'{pathDir}'는 파일입니다.");
  116. }
  117. else if (Directory.Exists(pathDir))
  118. {
  119. Console.WriteLine($"'{pathDir}'는 디렉토리입니다.");
  120. // 디렉토리 내 모든 파일 목록 가져오기 (하위 디렉토리 포함)
  121. string[] fileNames = Directory.GetFiles(pathDir, "*", SearchOption.AllDirectories);
  122. foreach (string file in fileNames)
  123. {
  124. if (System.IO.File.Exists(file))
  125. {
  126. DeleteFile(file, deleteMin);
  127. }
  128. }
  129. }
  130. else
  131. {
  132. Console.WriteLine($"'{pathDir}'는 존재하지 않는 경로입니다.");
  133. }
  134. }
  135. private void DeleteFile(string fileName, int deleteMin)
  136. {
  137. if (System.IO.File.Exists(fileName))
  138. {
  139. Console.WriteLine($"'{fileName}'는 파일입니다.");
  140. FileInfo fileInfo = new FileInfo(fileName);
  141. if (fileInfo.CreationTime.Date < DateTime.Now.AddMinutes(-deleteMin))
  142. {
  143. System.IO.File.Delete(fileName);
  144. Console.WriteLine($"'{fileName}'을 삭제했습니다.");
  145. }
  146. }
  147. }
  148. protected override void OnStop()
  149. {
  150. deletePathTimer.Stop();
  151. stopService();
  152. }
  153. private void stopService()
  154. {
  155. stopEvent?.Set();
  156. Thread.Sleep(5000);
  157. }
  158. }
  159. }