/// <summary>
 /// 获取配置文件中所有的任务
 /// </summary>
 /// <returns></returns>
 private List<JobObject> GetJobs()
 {
     List<JobObject> result = new List<JobObject>();
     var jobs = doc.Element("Jobs").Elements("Job");
     foreach (var p in jobs)
     {
         string assembly = p.Attribute("type").Value.Split(',')[1];
         string className = p.Attribute("type").Value.Split(',')[0];
         JobObject job = new JobObject();
         job.Name = p.Attribute("name").Value;
         job.Instance = Assembly.Load(assembly).CreateInstance(className) as IJob;
         job.CornExpress = p.Attribute("cornExpress").Value;
         result.Add(job);
     }
     return result;
 }
        /// <summary>
        /// 获取配置文件中所有的任务
        /// </summary>
        /// <returns></returns>
        private List <JobObject> GetJobs()
        {
            List <JobObject> result = new List <JobObject>();
            var jobs = doc.Element("Jobs").Elements("Job");

            foreach (var p in jobs)
            {
                JobObject job = new JobObject();
                if (p.Attributes().Any(o => o.Name.ToString() == "type") && p.Attributes().Any(o => o.Name.ToString() == "exePath"))
                {
                    throw new Exception("job中不能同时配制“type”与“exePath”");
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "type"))
                {
                    job.JobType = JobTypeEnum.Assembly;
                    string assembly  = p.Attribute("type").Value.Split(',')[1];
                    string className = p.Attribute("type").Value.Split(',')[0];
                    job.Instance = Assembly.Load(assembly).CreateInstance(className) as IJob;
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "ExpireSecond"))
                {
                    job.ExpireSecond = int.Parse(p.Attribute("ExpireSecond").Value);
                }
                else if (p.Attributes().Any(o => o.Name.ToString() == "exePath"))
                {
                    job.JobType = JobTypeEnum.Exe;
                    job.ExePath = p.Attribute("exePath").Value.Replace("${basedir}", AppDomain.CurrentDomain.BaseDirectory);
                    if (p.Attributes().Any(o => o.Name.ToString() == "arguments"))
                    {
                        job.Arguments = p.Attribute("arguments").Value;
                    }
                }

                job.Name        = p.Attribute("name").Value;
                job.CornExpress = p.Attribute("cornExpress").Value;
                if (!CronExpression.IsValidExpression(job.CornExpress))
                {
                    throw new Exception(string.Format("corn表达式:{0}不正确。", job.CornExpress));
                }
                result.Add(job);
            }
            return(result);
        }
 /// <summary>
 /// 执行任务
 /// </summary>
 /// <param name="job">要执行的任务</param>
 private void RunJob(JobObject job)
 {
     try
     {
         if (CornUtility.Trigger(job.CornExpress, DateTime.Parse(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"))))
         {
             if (!job.Running && !job.Triggering)
             {
                 job.Triggering = true;
                 Task.Factory.StartNew(() =>
                 {
                     try
                     {
                         job.Running = true;
                         job.Instance.Init();
                         job.Instance.Excute();
                     }
                     finally { job.Running = false; }
                 });
             }
         }
         else
         {
             job.Triggering = false;
         }
     }
     catch (Exception ex)
     {
         try
         {
             job.Instance.OnError(ex);
         }
         catch { }
         logger.Error(string.Format("执行任务:{0}时出错.", job.Name), ex);
     }
 }
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="job">要执行的任务</param>
        private void RunJob(JobObject job)
        {
            try
            {
                if (CornUtility.Trigger(job.CornExpress, DateTime.Parse(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"))))
                {
                    if (!job.Triggering)
                    {
                        job.Triggering = true;
                        switch (job.JobType)
                        {
                        case JobTypeEnum.Assembly:
                            job.Instance.Init();
                            job.Instance.Excute();
                            break;

                        case JobTypeEnum.Exe:
                            using (var process = new System.Diagnostics.Process())
                            {
                                bool hasValue = job.ExpireSecond.HasValue;
                                if (string.IsNullOrWhiteSpace(job.Arguments))
                                {
                                    process.StartInfo = new System.Diagnostics.ProcessStartInfo(job.ExePath);
                                }
                                else
                                {
                                    process.StartInfo = new System.Diagnostics.ProcessStartInfo(job.ExePath, job.Arguments);
                                }
                                process.Start();
                                if (hasValue)     //如果设置了最长运行时间,到达时间时,自动中止进程
                                {
                                    bool result = process.WaitForExit(job.ExpireSecond.Value * 1000);
                                    if (!result)
                                    {
                                        Logger.Info(string.Format("任务【{0}】因长时间:{1}秒未返回运行状态,程序已自动将其Kill.", job.Name, job.ExpireSecond));
                                        process.Kill();
                                    }
                                }
                                else
                                {
                                    process.WaitForExit();
                                }
                            }
                            break;
                        }
                    }
                }
                else
                {
                    job.Triggering = false;
                }
            }
            catch (Exception ex) //不处理错误,防止日志爆长
            {
                try
                {
                    if (job.JobType == JobTypeEnum.Assembly)
                    {
                        job.Instance.OnError(ex);
                    }
                }
                catch { }
            }
        }
        /// <summary>
        /// 解析配置文件
        /// </summary>
        /// <returns></returns>
        private List <JobObject> GetJobs()
        {
            List <JobObject> result = new List <JobObject>();
            var jobs = Doc.Element("Jobs").Elements("Job");

            foreach (var p in jobs)
            {
                JobObject job = new JobObject();
                if (p.Attributes().Any(o => o.Name.ToString() == "type") && p.Attributes().Any(o => o.Name.ToString() == "exePath"))
                {
                    throw new Exception("job中不能同时配制“type”与“exePath”");
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "type"))
                {
                    job.JobType = JobTypeEnum.Assembly;
                    string assembly  = p.Attribute("type").Value.Split(',')[1];
                    string className = p.Attribute("type").Value.Split(',')[0];

                    string runInSandbox = "false";
                    if (p.Attributes().Any(o => o.Name.ToString() == "runInSandbox"))
                    {
                        runInSandbox = p.Attribute("runInSandbox").Value;
                    }
                    if (runInSandbox.ToLower() == "true")
                    {
                        Random r    = new Random();
                        var    name = p.Attribute("name").Value + r.Next(1000);
                        //创建sandbox
                        job.Sandbox      = Sandbox.Create(name);
                        job.AssemblyName = assembly;
                        job.TypeName     = className;
                        job.RunInSandbox = true;
                    }
                    else
                    {
                        var targetAssembly = Assembly.Load(assembly);
                        job.Instance     = targetAssembly.CreateInstance(className) as IJob;
                        job.RunInSandbox = false;
                    }
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "expireSecond"))
                {
                    job.ExpireSecond = int.Parse(p.Attribute("expireSecond").Value);
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "exePath"))
                {
                    job.JobType = JobTypeEnum.Exe;
                    job.ExePath = p.Attribute("exePath").Value.Replace("${basedir}", AppDomain.CurrentDomain.BaseDirectory);
                    if (p.Attributes().Any(o => o.Name.ToString() == "arguments"))
                    {
                        job.Arguments = p.Attribute("arguments").Value;
                    }
                }

                job.Name        = p.Attribute("name").Value;
                job.CornExpress = p.Attribute("cornExpress").Value;
                if (!CronExpression.IsValidExpression(job.CornExpress))
                {
                    throw new Exception(string.Format("corn表达式:{0}不正确。", job.CornExpress));
                }
                result.Add(job);
            }
            return(result);
        }
Beispiel #6
0
        /// <summary>
        /// 解析配置文件
        /// </summary>
        /// <returns></returns>
        private List <JobObject> GetJobs()
        {
            List <JobObject> result = new List <JobObject>();
            var jobs = Doc.Element("Jobs").Elements("Job");

            foreach (var p in jobs)
            {
                JobObject job = new JobObject();
                if (p.Attributes().Any(o => o.Name.ToString() == "type") && p.Attributes().Any(o => o.Name.ToString() == "exePath"))
                {
                    throw new Exception("job中不能同时配制“type”与“exePath”");
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "type"))
                {
                    job.JobType = JobTypeEnum.Assembly;
                    string assembly  = p.Attribute("type").Value.Split(',')[1];
                    string className = p.Attribute("type").Value.Split(',')[0];

                    string runInSandbox = "false";
                    if (p.Attributes().Any(o => o.Name.ToString() == "runInSandbox"))
                    {
                        runInSandbox = p.Attribute("runInSandbox").Value;
                    }

                    Random r       = new Random();
                    string config  = assembly + ".dll.config";
                    string workDir = "Bin";
                    if (p.Attributes().Any(o => o.Name.ToString() == "workDir"))
                    {
                        workDir = p.Attribute("workDir").Value;
                        if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, workDir, config)))
                        {
                            config = null;
                        }
                    }

                    //创建sandbox
                    job.Sandbox      = Sandbox.Create(config, workDir);
                    job.AssemblyName = assembly;
                    job.TypeName     = className;
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "expireSecond"))
                {
                    job.ExpireSecond = int.Parse(p.Attribute("expireSecond").Value);
                }
                if (p.Attributes().Any(o => o.Name.ToString() == "exePath"))
                {
                    job.JobType = JobTypeEnum.Exe;
                    job.ExePath = p.Attribute("exePath").Value.Replace("${basedir}", AppDomain.CurrentDomain.BaseDirectory);
                    if (p.Attributes().Any(o => o.Name.ToString() == "arguments"))
                    {
                        job.Arguments = p.Attribute("arguments").Value;
                    }
                }

                job.Name        = p.Attribute("name").Value;
                job.CornExpress = p.Attribute("cornExpress").Value;
                if (!CronExpression.IsValidExpression(job.CornExpress))
                {
                    throw new Exception(string.Format("corn表达式:{0}不正确。", job.CornExpress));
                }
                result.Add(job);
            }
            return(result);
        }