예제 #1
0
        private XmlElement GeneJobEle(JackJob job, XmlDocument doc)
        {
            XmlElement ele = doc.CreateElement("job");

            ele.Attributes.Append(CreateAttr(doc, "name", job.Name));
            ele.Attributes.Append(CreateAttr(doc, "cron", job.Cron));
            ele.Attributes.Append(CreateAttr(doc, "logwhen", job.LogWhen));
            ele.Attributes.Append(CreateAttr(doc, "jobtype", job.JobType));
            ele.Attributes.Append(CreateAttr(doc, "searchpath", job.SearchPath));
            ele.Attributes.Append(CreateAttr(doc, "srccodepath", job.SrcCodeFilePath));
            ele.Attributes.Append(CreateAttr(doc, "typename", job.TypeName));
            ele.Attributes.Append(CreateAttr(doc, "method", job.Method));
            ele.Attributes.Append(CreateAttr(doc, "enable", job.Enable));
            ele.Attributes.Append(CreateAttr(doc, "name", job.Name));
            if (job.Paras == null || job.Paras.Count == 0)
            {
            }
            else
            {
                for (int i = 0; i < job.Paras.Count; i++)
                {
                    XmlElement   ele2  = doc.CreateElement("para");
                    XmlAttribute attr2 = doc.CreateAttribute("value");
                    attr2.Value = job.Paras[i];
                    ele2.Attributes.Append(attr2);
                    ele.AppendChild(ele2);
                }
            }
            return(ele);
        }
예제 #2
0
 public bool AddJob(JackJob job)
 {
     try
     {
         if (job == null || string.IsNullOrWhiteSpace(job.Name))
         {
             Time2RunSrv.WriteLog("管理端:【Manager】更新任务[" + job.Name + "]失败:" + "未发现有效的任务名称!");
             return(false);
         }
         lock (typeof(Time2RunSrv))
         {
             Configuration        conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
             ConfigurationSection sec  = conf.GetSection("JackTime2RunJobs");
             string      str           = sec.SectionInformation.GetRawXml();
             XmlDocument doc           = new XmlDocument();
             doc.LoadXml(str);
             XmlElement ele  = GeneJobEle(job, doc);
             XmlNode    jobs = doc.ChildNodes.OfType <XmlElement>().FirstOrDefault <XmlElement>();
             jobs.AppendChild(ele);
             sec.SectionInformation.SetRawXml(doc.InnerXml);
             conf.Save(ConfigurationSaveMode.Modified);
             return(true);
         }
     }
     catch (Exception ex)
     {
         Time2RunSrv.WriteLog("管理端:【Manager】添加任务[" + job.Name + "]失败:" + ex.ToString());
         return(false);
     }
 }
예제 #3
0
 public bool TriJob(string name)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(name))
         {
             Time2RunSrv.WriteLog("管理端:【Manager】临时执行任务[" + name + "]失败:" + "未发现有效的任务名称!");
             return(false);
         }
         JackJob job = JobHelper.Jobs.FirstOrDefault <JackJob>(i =>
         {
             return(i.Name == name);
         });
         if (job == null)
         {
             Time2RunSrv.WriteLog("管理端:【Manager】临时执行任务[" + name + "]失败:" + "未发现有效的任务名称!");
             return(false);
         }
         new FullJob().ExeJob(job);
         return(true);
     }
     catch (Exception ex)
     {
         Time2RunSrv.WriteLog("管理端:【Manager】移除任务[" + name + "]失败:" + ex.ToString());
         return(false);
     }
 }
예제 #4
0
 public void Execute(IJobExecutionContext context)
 {
     try
     {
         JackJob job = context.JobDetail.JobDataMap.Get("jobname") as JackJob;
         ExeJob(job);
     }
     catch (Exception ex)
     {
         WriteLog("【日志信息: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "】[任务执行失败]:" + ex.ToString() + "\r\n");
     }
 }
예제 #5
0
        public bool UpdateJob(JackJob job)
        {
            try
            {
                if (job == null || string.IsNullOrWhiteSpace(job.Name))
                {
                    Time2RunSrv.WriteLog("管理端:【Manager】更新任务[" + job.Name + "]失败:" + "未发现有效的任务名称!");
                    return(false);
                }

                lock (typeof(Time2RunSrv))
                {
                    Configuration        conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    ConfigurationSection sec  = conf.GetSection("JackTime2RunJobs");
                    string      str           = sec.SectionInformation.GetRawXml();
                    XmlDocument doc           = new XmlDocument();
                    doc.LoadXml(str);
                    XmlElement        ele  = GeneJobEle(job, doc);
                    List <XmlElement> jobs = doc.ChildNodes.OfType <XmlElement>().FirstOrDefault <XmlElement>().ChildNodes.OfType <XmlElement>().Where <XmlElement>((i) => i.Name == "job").ToList <XmlElement>();
                    for (int i = 0; i < jobs.Count; i++)
                    {
                        if (jobs[i].Attributes["name"].Value == job.Name)
                        {
                            XmlNode p = jobs[i].ParentNode;
                            p.ReplaceChild(ele, jobs[i]);
                            break;
                        }
                    }
                    sec.SectionInformation.SetRawXml(doc.InnerXml);
                    conf.Save(ConfigurationSaveMode.Modified);
                    Host._srv.ReloadConf();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Time2RunSrv.WriteLog("管理端:【Manager】更新任务[" + job.Name + "]失败:" + ex.ToString());
                return(false);
            }
        }
예제 #6
0
        public List <JackJob> GetAllJobs()
        {
            try
            {
                System.Configuration.ConfigurationManager.GetSection("JackTime2RunJobs");
                JackJob[] jobs = new JackJob[JobHelper.Jobs.Count];
                for (int i = 0; i < JobHelper.Jobs.Count; i++)
                {
                    jobs[i] = new JackJob()
                    {
                        Name            = JobHelper.Jobs[i].Name,
                        Cron            = JobHelper.Jobs[i].Cron,
                        LogWhen         = JobHelper.Jobs[i].LogWhen,
                        JobType         = JobHelper.Jobs[i].JobType,
                        SearchPath      = JobHelper.Jobs[i].SearchPath,
                        TypeName        = JobHelper.Jobs[i].TypeName,
                        SrcCodeFilePath = JobHelper.Jobs[i].SrcCodeFilePath,
                        Method          = JobHelper.Jobs[i].Method,
                        Enable          = JobHelper.Jobs[i].Enable,
                        LastDate        = JobHelper.Jobs[i].LastDate,
                        NextDate        = JobHelper.Jobs[i].NextDate,
                        State           = JobHelper.Jobs[i].State
                    };
                    jobs[i].Paras = new List <string>();
                    for (int j = 0; j < JobHelper.Jobs[i].Paras.Count; j++)
                    {
                        jobs[i].Paras.Add(JobHelper.Jobs[i].Paras[j]);
                    }
                }
                for (int i = 0; i < jobs.Length; i++)
                {
                    //转化日志类型
                    if (jobs[i].LogWhen == "0")
                    {
                        jobs[i].LogWhen = "一定记录";
                    }
                    else if (jobs[i].LogWhen == "1")
                    {
                        jobs[i].LogWhen = "成功时记录";
                    }
                    else if (jobs[i].LogWhen == "2")
                    {
                        jobs[i].LogWhen = "失败时记录";
                    }
                    //转化任务类型
                    if (jobs[i].JobType == "0")
                    {
                        jobs[i].JobType = "动态库";
                    }
                    else if (jobs[i].JobType == "1")
                    {
                        jobs[i].JobType = "单个cs文件";
                    }
                    else if (jobs[i].JobType == "2")
                    {
                        jobs[i].JobType = "exe程序";
                    }
                    //转化使用标志
                    if (jobs[i].Enable == "0")
                    {
                        jobs[i].Enable = "禁用";
                    }
                    else if (jobs[i].Enable == "1")
                    {
                        jobs[i].Enable = "可用";
                    }

                    //获取上一次和下一次执行时间以及任务状态
                    JobKey           key      = new JobKey(jobs[i].Name, jobs[i].Name);
                    IJobDetail       detail   = JobHelper.sche.GetJobDetail(key);
                    IList <ITrigger> triggers = JobHelper.sche.GetTriggersOfJob(key);
                    if (triggers.Count > 0)
                    {
                        ICronTrigger   croTri = triggers[0] as ICronTrigger;
                        DateTimeOffset?dtoff  = croTri.GetPreviousFireTimeUtc();
                        if (dtoff != null && dtoff.ToString() != "")
                        {
                            jobs[i].LastDate = TimeZone.CurrentTimeZone.ToLocalTime(Convert.ToDateTime(dtoff.ToString())).ToString("yyyy-MM-dd HH:mm:ss");
                        }
                        else
                        {
                            jobs[i].LastDate = "";
                        }
                        dtoff = croTri.GetNextFireTimeUtc();
                        if (dtoff != null && dtoff.ToString() != "")
                        {
                            jobs[i].NextDate = TimeZone.CurrentTimeZone.ToLocalTime(Convert.ToDateTime(dtoff.ToString())).ToString("yyyy-MM-dd HH:mm:ss");
                        }
                        else
                        {
                            jobs[i].NextDate = "";
                        }
                        TriggerState state = JobHelper.sche.GetTriggerState(croTri.Key);

                        if (state == TriggerState.Complete)
                        {
                            jobs[i].State = "完成";
                        }
                        else if (state == TriggerState.Error)
                        {
                            jobs[i].State = "出错";
                        }
                        else if (state == TriggerState.None)
                        {
                            jobs[i].State = "无";
                        }
                        else if (state == TriggerState.Normal)
                        {
                            jobs[i].State = "正常";
                        }
                        else if (state == TriggerState.Paused)
                        {
                            jobs[i].State = "暂停";
                        }
                        else if (state == TriggerState.Blocked)
                        {
                            jobs[i].State = "锁定";
                        }
                    }
                }
                return(jobs.ToList());
            }
            catch (Exception ex)
            {
                Time2RunSrv.WriteLog("管理端:【Manager】拉去任务失败:" + ex.ToString());
                return(null);
            }
        }
예제 #7
0
        public void ExeJob(JackJob job)
        {
            string Info    = "";
            string jobtype = "未知类型";

            switch (job.JobType)
            {
            case "0":
            {
                jobtype = "dll调用";
                break;
            }

            case "1":
            {
                jobtype = "编译CS文件";
                break;
            }

            case "2":
            {
                jobtype = "exe调用";
                break;
            }

            default:
            {
                break;
            }
            }
            Info += "[任务名:" + job.Name + "][任务类型:" + jobtype + "]";

            //处理调用时的参数,默认没有参数
            Type[]   types = new Type[] { };
            object[] paras = new object[] { };
            if (job.Paras.Count > 0)
            {
                //有参数情况,参数默认是一个字符串数组
                types    = new Type[] { typeof(string[]) };
                paras    = new object[1];
                paras[0] = new string[job.Paras.Count];
                for (int i = 0; i < job.Paras.Count; i++)
                {
                    (paras[0] as string[])[i] = job.Paras[i];
                }
            }

            if (job.JobType == "0")
            {
                Info += "[程序集搜索路径:" + job.SearchPath + "][执行类:" + job.TypeName + "][执行方法:" + job.Method + "]";
                try
                {
                    Hashtable ht = DynamicUtil.MainlUtil.InvokeDll(job.SearchPath, job.TypeName, job.Method, types, paras);
                    if (!(bool)ht["Success"])
                    {
                        Info += "[出错]\r\n" + (ht["Data"] ?? "").ToString();
                        if (job.LogWhen == "0" || job.LogWhen == "2")
                        {
                            //失败或一定记录时
                            WriteLog(Info);
                        }
                        return;
                    }
                    else
                    {
                        Info += "[成功]\r\n";
                        if (job.LogWhen == "1" || job.LogWhen == "0")
                        {
                            //成功时记录
                            WriteLog(Info);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (job.LogWhen == "0" || job.LogWhen == "2")
                    {
                        //失败或一定记录时
                        WriteLog(Info + "\r\n" + ex.ToString());
                    }
                    return;
                }
            }
            else if (job.JobType == "1")
            {
                Info += "[程序集搜索路径:" + job.SearchPath + "][编译文件:" + job.SrcCodeFilePath + "][执行类:" + job.TypeName + "][执行方法:" + job.Method + "]";
                try
                {
                    Hashtable ht = DynamicUtil.MainlUtil.InvokeSrc(job.SearchPath, job.SrcCodeFilePath, job.TypeName, job.Method, types, paras);
                    if (!(bool)ht["Success"])
                    {
                        Info += "[出错]\r\n" + (ht["Data"] ?? "").ToString();
                        if (job.LogWhen == "0" || job.LogWhen == "2")
                        {
                            //失败或一定记录时
                            WriteLog(Info);
                        }
                        return;
                    }
                    else
                    {
                        Info += "[成功]\r\n";
                        if (job.LogWhen == "1" || job.LogWhen == "0")
                        {
                            //成功时记录
                            WriteLog(Info);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (job.LogWhen == "0" || job.LogWhen == "2")
                    {
                        //失败或一定记录时
                        WriteLog(Info + "\r\n" + ex.ToString());
                    }
                    return;
                }
            }
            else if (job.JobType == "2")
            {
                Info += "[程序集搜索路径:" + job.SearchPath + "][加载exe:" + job.TypeName + "]";
                try
                {
                    Hashtable ht = DynamicUtil.MainlUtil.InvokeExe(job.SearchPath, job.TypeName, job.Paras.ToArray <string>());
                    if (!(bool)ht["Success"])
                    {
                        Info += "[出错]\r\n" + (ht["Data"] ?? "").ToString();
                        if (job.LogWhen == "0" || job.LogWhen == "2")
                        {
                            //失败或一定记录时
                            WriteLog(Info);
                        }
                        return;
                    }
                    else
                    {
                        Info += "[成功]\r\n";
                        if (job.LogWhen == "1" || job.LogWhen == "0")
                        {
                            //成功时记录
                            WriteLog(Info);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (job.LogWhen == "0" || job.LogWhen == "2")
                    {
                        //失败或一定记录时
                        WriteLog(Info + "\r\n" + ex.ToString());
                    }
                    return;
                }
            }
        }
예제 #8
0
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            List <JackJob> jobs = new List <JackJob>();
            XmlNodeList    list = section.SelectNodes("job");

            if (list != null)
            {
                foreach (XmlNode node in list)
                {
                    JackJob      job  = new JackJob();
                    XmlAttribute attr = node.Attributes["name"];
                    if (attr != null)
                    {
                        job.Name = attr.Value;
                    }
                    attr = node.Attributes["cron"];
                    if (attr != null)
                    {
                        job.Cron = attr.Value;
                    }
                    attr = node.Attributes["logwhen"];
                    if (attr != null)
                    {
                        job.LogWhen = attr.Value;
                    }
                    attr = node.Attributes["jobtype"];
                    if (attr != null)
                    {
                        job.JobType = attr.Value;
                    }
                    attr = node.Attributes["searchpath"];
                    if (attr != null)
                    {
                        job.SearchPath = attr.Value;
                    }
                    attr = node.Attributes["typename"];
                    if (attr != null)
                    {
                        job.TypeName = attr.Value;
                    }
                    attr = node.Attributes["srccodepath"];
                    if (attr != null)
                    {
                        job.SrcCodeFilePath = attr.Value;
                    }
                    attr = node.Attributes["method"];
                    if (attr != null)
                    {
                        job.Method = attr.Value;
                    }
                    attr = node.Attributes["enable"];
                    if (attr != null)
                    {
                        job.Enable = attr.Value;
                    }
                    if (node.HasChildNodes)
                    {
                        node.ChildNodes.OfType <XmlElement>().ToList <XmlElement>().ForEach(i =>
                        {
                            if (i.Name == "para")
                            {
                                XmlAttribute attr_tmp = i.Attributes["value"];
                                if (attr_tmp != null)
                                {
                                    job.Paras.Add(attr_tmp.Value);
                                }
                            }
                        });
                    }
                    jobs.Add(job);
                }
            }
            JobHelper.Jobs = jobs;
            return(null);
        }