using Estsh.Core.Quartz.Enum; using Estsh.Core.Quartz.Extensions; using Estsh.Core.Quartz.Model; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Text; namespace Estsh.Core.Quartz.Tools { public class QuartzFileHelper { private string _rootPath { get; set; } private string _logPath { get; set; } public string QuartzSettingsFolder { get; set; } = "QuartzSettings"; public string Logs { get; set; }="logs"; public string TaskJobFileName { get; set; } = "task_job.json"; private IWebHostEnvironment _env; private IConfiguration _configuration; public QuartzFileHelper(IWebHostEnvironment env, IConfiguration configuration) { _env = env; _configuration = configuration; CreateQuartzRootPath(); } /// /// 初始化 /// 创建作业所在根目录及日志文件夹 /// /// public string CreateQuartzRootPath() { if (!string.IsNullOrEmpty(_rootPath)) return _rootPath; _rootPath = $"{Directory.GetParent(_env.ContentRootPath).FullName}\\{QuartzSettingsFolder}\\"; _rootPath = _rootPath.ReplacePath(); if (!Directory.Exists(_rootPath)) { Directory.CreateDirectory(_rootPath); } _logPath = _rootPath + Logs + "\\"; _logPath = _logPath.ReplacePath(); //生成日志文件夹 if (!Directory.Exists(_logPath)) { Directory.CreateDirectory(_logPath); } return _rootPath; } /// /// 获取jobs /// /// /// public List GetJobs(Expression> where) { string path = $"{_rootPath}\\{TaskJobFileName}"; List list = new List(); path = path.ReplacePath(); if (!File.Exists(path)) return list; var tasks = ReadFile(path); if (string.IsNullOrEmpty(tasks)) { return null; } var _taskList = JsonConvert.DeserializeObject>(tasks); return _taskList.Where(where.Compile()).ToList(); } /// /// 读取任务日志 /// /// /// /// /// /// public List GetJobRunLog( string taskName, string groupName, int page, int pageSize = 100) { string path = $"{_logPath}{groupName}\\{taskName}"; List list = new List(); path = path.ReplacePath(); if (!File.Exists(path)) return list; var logs = ReadPageLine(path, page, pageSize, true); foreach (string item in logs) { string[] arr = item?.Split('_'); if (item == "" || arr == null || arr.Length == 0) continue; if (arr.Length != 3) { list.Add(new tab_quarz_tasklog() { Msg = item }); continue; } list.Add(new tab_quarz_tasklog() { BeginDate = Convert.ToDateTime(arr[0]), EndDate = Convert.ToDateTime(arr[1]), Msg = arr[2] }); } return list.OrderByDescending(x => x.BeginDate).ToList(); } /// /// 写入任务(全量) /// /// public void WriteJobConfig(List taskList) { string jobs = JsonConvert.SerializeObject(taskList); //写入配置文件 WriteFile(_rootPath, TaskJobFileName, jobs); } public void WriteStartLog(string content) { content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "," + content; if (!content.EndsWith("\r\n")) { content += "\r\n"; } WriteFile(LogPath, "start.txt", content, true); } public void WriteJobLogs(tab_quarz_tasklog tab_Quarz_Tasklog) { var content = JsonConvert.SerializeObject(tab_Quarz_Tasklog)+"\r\n"; WriteFile(LogPath, "logs.txt", content, true); } public List GetJobsLog(int pageSize = 1) { string path = LogPath + "logs.txt"; path = path.ReplacePath(); if (!File.Exists(path)) return null; var listlogs = ReadPageLine(path, pageSize, 5000, true).ToList(); List listtasklogs = new List(); foreach (var item in listlogs) { listtasklogs.Add(JsonConvert.DeserializeObject(item)); } return listtasklogs; } public string RootPath { get { return _rootPath; } } public string LogPath { get { return _logPath; } } /// /// 读取本地txt日志内容 /// /// /// /// /// /// public IEnumerable ReadPageLine(string fullPath, int page, int pageSize, bool seekEnd = false) { if (page <= 0) { page = 1; } fullPath = fullPath.ReplacePath(); var lines = File.ReadLines(fullPath, Encoding.UTF8); if (seekEnd) { int lineCount = lines.Count(); int linPageCount = (int)Math.Ceiling(lineCount / (pageSize * 1.00)); //超过总页数,不处理 if (page > linPageCount) { page = 0; pageSize = 0; } else if (page == linPageCount)//最后一页,取最后一页剩下所有的行 { pageSize = lineCount - (page - 1) * pageSize; if (page == 1) { page = 0; } else { page = lines.Count() - page * pageSize; } } else { page = lines.Count() - page * pageSize; } } else { page = (page - 1) * pageSize; } lines = lines.Skip(page).Take(pageSize); var enumerator = lines.GetEnumerator(); int count = 1; IList result = new List(); while (enumerator.MoveNext() || count <= pageSize) { yield return enumerator.Current; count++; } enumerator.Dispose(); } public string ReadFile(string path) { path = path.ReplacePath(); if (!File.Exists(path)) return ""; using (StreamReader stream = new StreamReader(path)) { return stream.ReadToEnd(); // 读取文件 } } /// /// 写文件 /// /// 文件路径 /// 文件内容 public void WriteFile(string path, string fileName, string content, bool appendToLast = false) { if (!path.EndsWith("\\")) { path = path + "\\"; } path = path.ReplacePath(); if (!Directory.Exists(path))//如果不存在就创建file文件夹 { Directory.CreateDirectory(path); } // 只保留30天的日志文件,之前的日志文件删除 var di = new DirectoryInfo(path); var fis = di.GetFiles(); if (fis.Length > 30) { var delFis = fis.Where(a => (DateTime.Now - a.CreationTime).Days > 30).ToList(); foreach (var itemFi in delFis) { itemFi.Delete(); } } // 日志跨天后将原日志文件重命名,然后重新生成一个新的日志文件 var fi = new FileInfo(path + fileName); if ((DateTime.Now - fi.CreationTime).Days > 0) { fi.MoveTo(fi.FullName + fi.CreationTime.ToString("yyyy-MM-dd")); } // 写日志处理 using (FileStream stream = File.Open(path + fileName, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] by = Encoding.Default.GetBytes(content); if (appendToLast) { stream.Position = stream.Length; } else { stream.SetLength(0); } stream.Write(by, 0, by.Length); } } } }