/// <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);
        }
Exemplo n.º 2
0
        public static List <JobDetail> GeneralJobs()
        {
            List <JobDetail> jobList = new List <JobDetail>();

            XDocument jobxml = XDocument.Load(ConfigPath);
            var       jobs   = jobxml.Element("Jobs").Elements("Job");

            foreach (var job in jobs)
            {
                JobDetail jobDetail = new JobDetail();

                if (job.Attributes().Any(o => o.Name.ToString() == "name"))
                {
                    job.Name = job.Attribute("name").Value;
                }

                if (job.Attributes().Any(o => o.Name.ToString() == "type"))
                {
                    jobDetail.JobType = (JobType)Enum.Parse(typeof(JobType), job.Attribute("type").Value, true);
                }

                if (job.Attributes().Any(o => o.Name.ToString() == "path"))
                {
                    string path = job.Attribute("path").Value;
                    switch (jobDetail.JobType)
                    {
                    case JobType.Assembly:
                    {
                        string className = path.Split(',')[0];
                        string assembly  = path.Split(',')[1];

                        bool runInSandbox = false;
                        if (job.Attributes().Any(o => o.Name.ToString() == "runInSandbox"))
                        {
                            runInSandbox = Convert.ToBoolean(job.Attribute("runInSandbox").Value);
                        }

                        if (runInSandbox)
                        {
                            Random r    = new Random();
                            var    name = jobDetail.Name + r.Next(1000);

                            //创建sandbox
                            jobDetail.Sandbox      = Sandbox.Create(name);
                            jobDetail.AssemblyName = assembly;
                            jobDetail.TypeName     = className;
                            jobDetail.RunInSandbox = true;
                        }
                        else
                        {
                            var targetAssembly = Assembly.Load(assembly);
                            jobDetail.Instance     = targetAssembly.CreateInstance(className) as IJob;
                            jobDetail.RunInSandbox = false;
                        }
                    }
                    break;

                    case JobType.Exe:
                    {
                        jobDetail.Path = path.Replace("${basedir}", AppDomain.CurrentDomain.BaseDirectory);
                        if (job.Attributes().Any(o => o.Name.ToString() == "arguments"))
                        {
                            jobDetail.Arguments = job.Attribute("arguments").Value;
                        }

                        if (job.Attributes().Any(o => o.Name.ToString() == "expireSecond"))
                        {
                            jobDetail.ExpireSecond = int.Parse(job.Attribute("expireSecond").Value);
                        }
                    }
                    break;
                    }
                }

                if (job.Attributes().Any(o => o.Name.ToString() == "cornExpress"))
                {
                    jobDetail.CornExpress = job.Attribute("cornExpress").Value;
                    if (!CronExpression.IsValidExpression(jobDetail.CornExpress))
                    {
                        throw new Exception(string.Format("corn表达式:{0}不正确。", jobDetail.CornExpress));
                    }
                }

                jobList.Add(jobDetail);
            }
            return(jobList);
        }
Exemplo n.º 3
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);
        }