Пример #1
0
        /// <summary>
        /// Kills the abandoned task process.
        /// </summary>
        /// <param name="processId">Process identifier.</param>
        /// <param name="jobName">Job name.</param>
        /// <param name="jobId">Job identifier.</param>
        /// <param name="taskId">Task identifier.</param>
        public static void KillAbandonedTaskProcess(int processId, string jobName, string jobId, int taskId)
        {
            var businessId = JobTask.FormatBusinessId(jobName, jobId, taskId);

            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.CheckTaskAndProcessMatch(osProcess, jobName, jobId, taskId))
                {
                    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("ExecuteTask", JobTask.FormatBusinessId(jobName, jobId, taskId));
                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);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 检查任务和进程是否匹配
        /// </summary>
        /// <returns><c>true</c>, if task and process match was checked, <c>false</c> otherwise.</returns>
        /// <param name="processId">Process identifier.</param>
        /// <param name="jobName">Job name.</param>
        /// <param name="jobId">Job identifier.</param>
        /// <param name="taskId">Task identifier.</param>
        public static bool CheckTaskAndProcessMatch(int processId, string jobName, string jobId, int taskId)
        {
            var businessId = JobTask.FormatBusinessId(jobName, jobId, taskId);

            Process osProcess = null;

            try
            {
                osProcess = Process.GetProcessById(processId);
            }
            catch (Exception ex)
            {
                LogWriter.Write(string.Format("根据进程Id查找进程失败:{0},{1}", businessId, processId), ex, LogLevel.Info);
            }

            if (osProcess == null)
            {
                return(false);
            }

            return(CheckTaskAndProcessMatch(osProcess, jobName, jobId, taskId));
        }