yg 4 months ago
parent
commit
4be8f5fd55

+ 37 - 4
AdService/Controllers/AdController.cs

@@ -1,5 +1,4 @@
-using DW5S.WebApi;
-using Microsoft.AspNetCore.Mvc;
+using AdService.Service;
 
 namespace AdService.Controllers
 {
@@ -10,16 +9,50 @@ namespace AdService.Controllers
     public class AdController : BaseController
     {
 
+        ILogger logger { get; set; }
+
+        private AdcService AdcService { get; set; }
 
         /// <summary>
         /// 开始采集
         /// </summary>
         /// <returns></returns>
         [HttpPost]
-        public string StartAD()
+        public async Task<AjaxResult<AdcResultDto>> StartAd(List<AdCard> dtos)
         {
-            return "aaaaaaaaaaa";
+            try
+            {
+                var res = await AdcService.Instance.StartTestAsync(dtos);
+
+                return Success(res);
+
+            }
+            catch (Exception ex)
+            {
+                return Error<AdcResultDto>("开始采集异常:" + ex.Message);
+            }
         }
 
+
+        /// <summary>
+        /// 停止采集
+        /// </summary>
+        [HttpPost]
+        public async Task<AjaxResult> StopAdc()
+        {
+            var res = await Task.Run(() =>
+            {
+                try
+                {
+                    AdcService.Instance.StopAd();
+                    return Success();
+                }
+                catch (Exception ex)
+                {
+                    return Error("停止采集异常:" + ex.Message);
+                }
+            });
+            return res;
+        }
     }
 }

+ 5 - 6
AdService/Controllers/AdFileController.cs

@@ -1,6 +1,4 @@
-using DW5S.WebApi;
-using Microsoft.AspNetCore.Mvc;
-
+
 namespace AdService.Controller.Controllers
 {
 
@@ -10,12 +8,13 @@ namespace AdService.Controller.Controllers
     public class AdFileController : BaseController
     {
         /// <summary>
-        /// 下载采集文件
+        /// 下载采集文件
         /// </summary>
         /// <param name="fileName">采集返回的名称</param>
+        /// <param name="path">采集返回的文件路径</param>
         /// <returns></returns>
         [HttpGet]
-        public IActionResult Download(string fileName)
+        public IActionResult Download(string path, string fileName)
         {
             if (string.IsNullOrWhiteSpace(fileName))
             {
@@ -23,7 +22,7 @@ namespace AdService.Controller.Controllers
             }
             if (fileName.StartsWith("/"))
                 fileName = fileName.Substring(1);
-            string localFile = Path.Combine("D:\\work", fileName);
+            string localFile = Path.Combine(path, fileName);
             try
             {
                 if (!System.IO.File.Exists(localFile))

+ 5 - 0
AdService/Dto/AdConfig.cs

@@ -25,5 +25,10 @@
         /// 删除{ClearHours}小时的数据
         /// </summary>
         public int ClearHours { get; set; }
+
+        /// <summary>
+        /// 上报地址
+        /// </summary>
+        public string AdcReportAddr { get; set; }
     }
 }

+ 0 - 9
AdService/Service/AdService.cs

@@ -1,9 +0,0 @@
-namespace AdService.Service
-{
-    /// <summary>
-    /// 采集服务
-    /// </summary>
-    public class AdService
-    {
-    }
-}

+ 157 - 9
AdService/Service/CearDirService.cs

@@ -1,4 +1,5 @@
-using DW5S.DTO;
+using AdService.Controller.Dto;
+using Newtonsoft.Json;
 
 namespace AdService.Service
 {
@@ -14,14 +15,161 @@ namespace AdService.Service
         protected override async Task ExecuteAsync(CancellationToken stoppingToken)
         {
 
-            await Task.Run(async () =>
-            {
-                while (!stoppingToken.IsCancellationRequested)
-                {
-                   
-                    await Task.Delay(3 * 1000, stoppingToken);
-                }
-            }, stoppingToken);
+            await Task.Delay(5000, stoppingToken);//延迟一会儿让webbapi先启动
+                                                  //IpsLogger.Info($"启动采集专用目录数据清理功能...");
+            /* await Task.Run(async () =>
+             {
+                 #region 读取清理目录配置信息
+                 StreamReader sr = new StreamReader(Directory.GetCurrentDirectory()+"appsettings.json");
+                 string json = sr.ReadToEnd();
+                 // 解析 JSON 字符串
+                 var adConfig = JsonConvert.DeserializeObject<AdConfig>(json);
+                 string dataClearStr = adConfig.ClearDir;
+                 var dataClears = dataClearStr.Split(new string[] { ";", ";" }, StringSplitOptions.RemoveEmptyEntries)
+                 .Select(p => p.ToLower()).ToList();
+                 if (!dataClears.Contains(adConfig.DataDir))
+                     dataClears.Add(adConfig.DataDir);
+                 #endregion
+
+                 #region 检测有效的清理目录并读取清理条件
+                 List<string> list = new List<string>();
+                 foreach (var item in dataClears)
+                 {
+                     var root = Path.GetPathRoot(item);
+                     DriveInfo drive = new DriveInfo(root);
+                     if (!drive.IsReady)
+                     {
+                         continue;
+                     }
+                     if (Directory.Exists(item))
+                     {
+                         list.Add(item);
+                     }
+                 }
+                 if (!list.Any())
+                 {
+                     //IpsLogger.Warn($"配置文件中的数据清理目录无效");
+                     return;
+                 }
+                 float maxPercent = adConfig.ClearCondition;
+                 var clearHours = adConfig.ClearHours;
+                 if (clearHours < 1)
+                 {
+                     clearHours = 1;
+                 }
+                 //IpsLogger.Info($"已启动目录[{string.Join(";", list)}]自动清理,清理条件=可用空间不足{(int)(maxPercent * 100)}%");
+                 #endregion
+
+
+                 //缓存每个目录上次清理的文件最大的一个写入时间
+                 var prevClearTime = new Dictionary<string, DateTime>();
+                 foreach (var item in list)
+                 {
+                     prevClearTime[item] = DateTime.MinValue;
+
+                 }
+                 while (!stoppingToken.IsCancellationRequested)
+                 {
+                    // IpsLogger.Info($"开始数据清理...");
+
+                     #region 从上次开始每个目录清理一定的数据(ClearHours)
+                     int delFileCount = 0;
+                     try
+                     {
+                         var driveFolders = list.GroupBy(m => Path.GetPathRoot(m).ToUpper());
+                         foreach (var driveFolder in driveFolders)//like D:\ E:\
+                         {
+                             if (stoppingToken.IsCancellationRequested) break;
+                             DriveInfo drive = new DriveInfo(driveFolder.Key);
+
+                             while (drive.TotalFreeSpace / (drive.TotalSize * 1.0f) <= maxPercent)
+                             {
+                                 foreach (var item in driveFolder)//like D:\data E:\data
+                                 {
+                                     //IpsLogger.Info($"正在清理[{item}]中的数据...");
+                                     var fsList = Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories).Select(p => new FileInfo(p));
+                                     if (!fsList.Any())
+                                     {
+                                      // IpsLogger.Warn($"{drive.ToString().Substring(0, 1)}盘空间不足,但目录{item}没有数据,跳过此目录");
+                                     }
+                                     else
+                                     {
+                                         if (prevClearTime[item] == DateTime.MinValue)
+                                             prevClearTime[item] = fsList.Min(p => p.LastWriteTime);
+                                         var maxTime = prevClearTime[item].AddHours(clearHours);
+                                         if (maxTime <= prevClearTime[item])
+                                         {
+                                             //clearHours配置为0或负数时一次删所有文件
+                                             maxTime = DateTime.MaxValue;
+                                         }
+                                         //每次清理从上次清理结束时间开始,防止有些删不掉的文件阻止删除文件
+                                         var clearFiles = fsList.Where(m => m.LastWriteTime >= prevClearTime[item] && m.LastWriteTime <= maxTime).OrderBy(m => m.LastWriteTime);
+                                         foreach (var fsItem in clearFiles)
+                                         {
+                                             try
+                                             {
+                                                 if (stoppingToken.IsCancellationRequested) break;
+                                                 fsItem.Delete();
+                                                 delFileCount++;
+                                             }
+                                             catch (Exception ex)
+                                             {
+                                                // IpsLogger.Error($"删除文件{fsItem.Name}出错", ex);
+                                             }
+                                         }
+                                         prevClearTime[item] = maxTime;
+
+                                     }
+                                 }
+                             }
+                         }
+                     }
+                     catch (Exception ex)
+                     {
+                         //IpsLogger.Error($"清理数据异常", ex);
+                     }
+                     #endregion
+
+                     #region 清理空目录
+                     int delDirCount = 0;
+                     foreach (var name in list)
+                     {
+                         string clearDir = Path.Combine(AppContext.BaseDirectory, name);
+                         try
+                         {
+                             var dirs = Directory.EnumerateDirectories(clearDir, "*", SearchOption.AllDirectories);
+                             foreach (var item in dirs)
+                             {
+                                 DirectoryInfo dir = new DirectoryInfo(item);
+                                 if (dir.EnumerateFiles().Any()) continue;
+                                 //删除5分钟以前创建的空目录
+                                 if ((DateTime.Now - dir.CreationTime).TotalMinutes > 5)
+                                 {
+
+                                     try
+                                     {
+                                         dir.Delete();
+                                         delDirCount++;
+                                     }
+                                     catch (Exception ex)
+                                     {
+                                         //IpsLogger.Error($"删除空目录{dir.Name}异常", ex);
+                                     }
+                                 }
+                             }
+                         }
+                         catch (Exception ex)
+                         {
+                             //IpsLogger.Error($"删除{name}目录中的空目录出错", ex);
+                         }
+                     }
+                     #endregion
+
+                    // IpsLogger.Info($"本次清理数据结束,删除{delFileCount}个文件,删除{delDirCount}个空目录");
+
+                     await Task.Delay(5 * 60 * 1000, stoppingToken);
+                 }
+             }, stoppingToken);*/
         }