コード例 #1
0
ファイル: JobTask.cs プロジェクト: xxyss/Swift
        /// <summary>
        /// 加载需求
        /// </summary>
        public void LoadRequirement()
        {
            var physicalRequirementPath = SwiftConfiguration.GetJobTaskRequirementPath(CurrentTaskPath);

            if (!File.Exists(physicalRequirementPath))
            {
                throw new Exception(string.Format("任务需求文件不存在:{0}", physicalRequirementPath));
            }

            var taskRequirementJson = string.Empty;

            try
            {
                taskRequirementJson = File.ReadAllText(physicalRequirementPath, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("读取任务需求文件异常:{0}", ex.Message));
            }

            if (string.IsNullOrWhiteSpace(taskRequirementJson))
            {
                throw new Exception(string.Format("任务需求文件为空:{0}", physicalRequirementPath));
            }

            Requirement = taskRequirementJson;
        }
コード例 #2
0
        /// <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;
            }
        }
コード例 #3
0
ファイル: SwiftProcess.cs プロジェクト: luwanchao915/Swift
        public SwiftProcess(string method, JobBase job, Process process)
        {
            _method = method;
            switch (_method)
            {
            case "SplitJob":
                _timeout = job.JobSplitTimeout;
                break;

            case "CollectTaskResult":
                _timeout = job.TaskResultCollectTimeout;
                break;

            default:
                _timeout = 0;
                break;
            }

            _job             = job;
            _businessId      = job.BusinessId;
            _eventActions    = null;
            _process         = process;
            _jobTask.Process = this;
            _filePath        = SwiftConfiguration.GetSwiftProcessPath(_method, _businessId);
            _job.RelateProcess(method, this);
        }
コード例 #4
0
ファイル: JobTask.cs プロジェクト: xxyss/Swift
        /// <summary>
        /// 将任务配置写到文件
        /// </summary>
        public void WriteConfig(CancellationToken cancellationToken = default(CancellationToken))
        {
            EnsureJobTaskDirectory(cancellationToken);

            var    taskJson     = JsonConvert.SerializeObject(this);
            string taskJsonPath = SwiftConfiguration.GetJobTaskConfigPath(CurrentTaskPath);

            File.WriteAllTextAsync(taskJsonPath, taskJson, Encoding.UTF8, cancellationToken).Wait();
        }
コード例 #5
0
        /// <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));
        }
コード例 #6
0
ファイル: JobTask.cs プロジェクト: xxyss/Swift
        /// <summary>
        /// 获取任务执行状态
        /// </summary>
        /// <returns>The task execute status.</returns>
        public CommonResult GetTaskExecuteStatus()
        {
            // 读取任务创建状态
            string physicalPath = SwiftConfiguration.GetJobTaskExecuteStatusPath(Job.Name, Job.Id, Id);

            if (!File.Exists(physicalPath))
            {
                return(new CommonResult()
                {
                    ErrCode = 1,
                    ErrMessage = "任务执行等待中..."
                });
            }

            var taskExecuteStatus = "";

            try
            {
                taskExecuteStatus = File.ReadAllText(physicalPath);
                LogWriter.Write(string.Format("任务执行状态文件内容:{0},{1}", physicalPath, taskExecuteStatus), LogLevel.Debug);
            }
            catch (Exception ex)
            {
                return(new CommonResult()
                {
                    ErrCode = 2,
                    ErrMessage = "读取任务执行状态异常:" + ex.Message
                });
            }

            // 正在写入
            if (taskExecuteStatus == "1")
            {
                return(new CommonResult()
                {
                    ErrCode = 3,
                    ErrMessage = "任务执行中..."
                });
            }

            // 任务处理出错
            if (taskExecuteStatus.StartsWith("-1", StringComparison.Ordinal))
            {
                return(new CommonResult()
                {
                    ErrCode = 4,
                    ErrMessage = "任务执行出错:" + taskExecuteStatus
                });
            }

            return(new CommonResult()
            {
                ErrCode = 0,
                ErrMessage = "任务执行完毕"
            });
        }
コード例 #7
0
ファイル: SwiftProcess.cs プロジェクト: luwanchao915/Swift
 public SwiftProcess(string method, JobTask task, Process process)
 {
     _method          = method;
     _jobTask         = task;
     _timeout         = _jobTask.ExecuteTimeout;
     _job             = task.Job;
     _businessId      = task.BusinessId;
     _eventActions    = null;
     _process         = process;
     _jobTask.Process = this;
     _filePath        = SwiftConfiguration.GetSwiftProcessPath(_method, _businessId);
 }
コード例 #8
0
ファイル: JobTask.cs プロジェクト: xxyss/Swift
        /// <summary>
        /// 保存任务结果
        /// </summary>
        public void WriteResult()
        {
            var taskPath = CurrentTaskPath;

            if (!Directory.Exists(taskPath))
            {
                Directory.CreateDirectory(taskPath);
            }

            string taskRequirementPath = SwiftConfiguration.GetJobTaskResultPath(taskPath);

            File.WriteAllText(taskRequirementPath, Result, Encoding.UTF8);
        }
コード例 #9
0
ファイル: JobTask.cs プロジェクト: xxyss/Swift
 /// <summary>
 /// 移除进程文件
 /// </summary>
 public void RemoveProcessFile()
 {
     try
     {
         var processFilePath = SwiftConfiguration.GetSwiftProcessPath("ExecuteTask", BusinessId);
         File.Delete(processFilePath);
         LogWriter.Write("has remove task execute process file");
     }
     catch (Exception ex)
     {
         LogWriter.Write("remove task execute process file go exception", ex);
     }
 }
コード例 #10
0
ファイル: JobTask.cs プロジェクト: xxyss/Swift
        /// <summary>
        /// 创建进程文件
        /// </summary>
        public void CreateProcessFile()
        {
            var processDirPath = SwiftConfiguration.AllSwiftProcessRootPath;

            if (!Directory.Exists(processDirPath))
            {
                Directory.CreateDirectory(processDirPath);
            }

            // 在进程内部记录进程Id,方便跟踪Swift启动的进程
            var processFilePath = SwiftConfiguration.GetSwiftProcessPath("ExecuteTask", BusinessId);

            File.WriteAllText(processFilePath, Process.Id.ToString());
        }
コード例 #11
0
ファイル: JobTask.cs プロジェクト: xxyss/Swift
        /// <summary>
        /// 获取进程Id
        /// </summary>
        /// <returns>The process identifier.</returns>
        public int GetProcessId(CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            int processId       = -1;
            var processFilePath = SwiftConfiguration.GetSwiftProcessPath("ExecuteTask", BusinessId);

            if (File.Exists(processFilePath))
            {
                processId = int.Parse(File.ReadAllTextAsync(processFilePath, cancellationToken).Result);
            }

            return(processId);
        }
コード例 #12
0
ファイル: SwiftProcess.cs プロジェクト: luwanchao915/Swift
        /// <summary>
        /// Kills the abandoned collect task result process.
        /// </summary>
        /// <param name="processId">Process identifier.</param>
        /// <param name="jobName">Job name.</param>
        /// <param name="jobId">Job identifier.</param>
        public static void KillAbandonedCollectTaskResultProcess(int processId, string jobName, string jobId)
        {
            var businessId = JobBase.FormatBusinessId(jobName, jobId);

            Process osProcess = null;

            try
            {
                osProcess = Process.GetProcessById(processId);
            }
            catch (Exception ex)
            {
                LogWriter.Write(string.Format("根据进程Id查找进程异常,进程可能已经关闭了:{0},{1}", businessId, processId), ex, LogLevel.Info);
            }

            bool canDeleteProcessFile = true;

            if (osProcess != null)
            {
                if (SwiftProcess.CheckJobAndProcessMatch(osProcess, jobName, jobId, "CollectTaskResult"))
                {
                    try
                    {
                        osProcess.Kill();
                        osProcess.WaitForExit();
                        LogWriter.Write(string.Format("已关闭废弃的任务合并进程:{0},{1}", businessId, processId), LogLevel.Info);
                    }
                    catch (Exception ex)
                    {
                        canDeleteProcessFile = false;
                        LogWriter.Write(string.Format("关闭废弃的任务合并进程异常:{0},{1}", businessId, processId), ex, LogLevel.Error);
                    }
                }
            }

            if (canDeleteProcessFile)
            {
                var processPath = SwiftConfiguration.GetSwiftProcessPath("CollectTaskResult", JobBase.FormatBusinessId(jobName, jobId));
                try
                {
                    File.Delete(processPath);
                    LogWriter.Write(string.Format("进程文件已删除:{0}", processPath), LogLevel.Info);
                }
                catch (Exception ex)
                {
                    LogWriter.Write(string.Format("删除废弃的任务合并进程文件异常:{0},{1}", businessId, processId), ex, LogLevel.Error);
                }
            }
        }
コード例 #13
0
        /// <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);
        }
コード例 #14
0
        /// <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);
        }