/// <summary> /// 确保作业包存在,如不存在则下载 /// </summary> /// <param name="jobName">Job name.</param> /// <param name="jobVersion">Job version.</param> public void EnsureJobPackage(string jobName, string jobVersion, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); try { var manager = Cluster.Manager; if (manager == null) { LogWriter.Write("EnsureJobPackage->当前没有管理员", LogLevel.Warn); return; } var jobPkgPath = SwiftConfiguration.GetJobPackagePath(jobName, jobVersion); if (!File.Exists(jobPkgPath)) { Download(manager, "download/job/package", new Dictionary <string, string> { { "jobName", jobName }, { "jobVersion", jobVersion } }, cancellationToken); } } catch (Exception ex) { LogWriter.Write("执行任务前确保作业包存在时发生异常", ex); throw; } }
/// <summary> /// 处理下载请求 /// </summary> private byte[] ProcessDownloadRequest(string requestUrl, byte[] inputData, NameValueCollection paras) { string filePath = string.Empty; // 如果指定了文件名,则使用文件名 string fileName = paras["fileName"]; if (!string.IsNullOrWhiteSpace(fileName)) { fileName = HttpUtility.UrlDecode(fileName); filePath = Path.Combine(SwiftConfiguration.BaseDirectory, fileName.Replace('/', Path.DirectorySeparatorChar)); } // 如果未指定文件名,则根据业务参数计算 if (string.IsNullOrWhiteSpace(filePath)) { if (requestUrl == "/download/task/requirement") { var jobName = paras["jobName"]; var jobId = paras["jobId"]; var taskId = paras["taskId"]; filePath = SwiftConfiguration.GetJobTaskRequirementPath(jobName, jobId, int.Parse(taskId)); } if (requestUrl == "/download/job/package") { var jobName = paras["jobName"]; var jobVersion = paras["jobVersion"]; filePath = SwiftConfiguration.GetJobPackagePath(jobName, jobVersion); } if (requestUrl == "/download/task/result") { var jobName = paras["jobName"]; var jobId = paras["jobId"]; var taskId = paras["taskId"]; filePath = SwiftConfiguration.GetJobTaskResultPath(jobName, jobId, int.Parse(taskId)); } if (requestUrl == "/download/job/result") { var jobName = paras["jobName"]; var jobId = paras["jobId"]; filePath = SwiftConfiguration.GetJobResultPackagePath(jobName, jobId); } } LogWriter.Write(string.Format("处理下载请求:{0}", filePath)); if (!File.Exists(filePath)) { throw new FileNotFoundException("文件不存在:" + filePath); } return(File.ReadAllBytes(filePath)); }
/// <summary> /// 处理上传请求 /// </summary> /// <param name="requestUrl">Request URL.</param> /// <param name="inputData">Input data.</param> /// <param name="paras">Paras.</param> private void ProcessUploadRequest(string requestUrl, byte[] inputData, NameValueCollection paras) { string filePath = string.Empty; // 如果指定了文件名,则使用文件名 string fileName = paras["fileName"]; if (!string.IsNullOrWhiteSpace(fileName)) { fileName = HttpUtility.UrlDecode(fileName); filePath = Path.Combine(SwiftConfiguration.BaseDirectory, fileName.Replace('/', Path.DirectorySeparatorChar)); } // 如果未指定文件名,则根据业务参数计算 if (string.IsNullOrWhiteSpace(filePath)) { if (requestUrl == "/upload/job/package") { var jobName = paras["jobName"]; var jobVersion = paras["jobVersion"]; filePath = SwiftConfiguration.GetJobPackagePath(jobName, jobVersion); } } LogWriter.Write(string.Format("处理上传请求:{0}", filePath)); string direcotryPath = filePath.Substring(0, filePath.LastIndexOf(Path.DirectorySeparatorChar)); if (!Directory.Exists(direcotryPath)) { Directory.CreateDirectory(direcotryPath); } var fileLockName = SwiftConfiguration.GetFileOperateLockName(filePath); lock (string.Intern(fileLockName)) { File.WriteAllBytes(filePath, inputData); } LogWriter.Write(string.Format("文件已经保存成功:{0}", filePath)); ProcessAfterUploadRequest(requestUrl, filePath); }
/// <summary> /// 下载数据处理 /// </summary> /// <param name="msgType"></param> /// <param name="paras"></param> /// <param name="data"></param> private void ProcessResponse(string msgType, Dictionary <string, string> paras, byte[] data, CancellationToken cancellationToken = default(CancellationToken)) { string filePath = string.Empty; paras.TryGetValue("fileName", out string fileName); if (!string.IsNullOrWhiteSpace(fileName)) { filePath = Path.Combine(SwiftConfiguration.BaseDirectory, fileName.Replace('/', Path.DirectorySeparatorChar)); } if (string.IsNullOrWhiteSpace(filePath)) { if (msgType == "download/job/package") { var jobName = paras["jobName"]; var jobVersion = paras["jobVersion"]; filePath = SwiftConfiguration.GetJobPackagePath(jobName, jobVersion); } if (msgType == "download/task/requirement") { var jobName = paras["jobName"]; var jobId = paras["jobId"]; var taskId = int.Parse(paras["taskId"]); filePath = SwiftConfiguration.GetJobTaskRequirementPath(jobName, jobId, taskId); } if (msgType == "download/task/result") { var jobName = paras["jobName"]; var jobId = paras["jobId"]; var taskId = int.Parse(paras["taskId"]); filePath = SwiftConfiguration.GetJobTaskResultPath(jobName, jobId, taskId); } } LogWriter.Write(string.Format("收到下载数据:{0},{1}", msgType, filePath)); SaveDownloadFile(filePath, data, cancellationToken); }