Ejemplo n.º 1
0
        public void Execute(IJobExecutionContext context)
        {
            var scriptId = Convert.ToInt32(context.JobDetail.Key.Name.Replace("ScriptJob_", ""));
            var taskEnt  = FunSqlToClass.ClassSingle <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                "where RUN_STATE='运行' AND SCRIPT_ID = " + scriptId,
                ConfigurationManager.AppSettings["dbPrefix"]
                );

            //如果有任务没有结束,则不添加任务
            if (taskEnt != null && taskEnt.ID != 0)
            {
                if (taskEnt.START_TIME != null && taskEnt.START_TIME.Value.AddHours(2) < DateTime.Now)
                {
                    Dictionary <string, object> dicStart = new Dictionary <string, object>();
                    dicStart.Add("RUN_STATE", "失败");
                    dicStart.Add("DISABLE_REASON", "运行超时");
                    FunSqlToClass.UpData <SCRIPT_TASK>(
                        ConfigurationManager.AppSettings["dbType"],
                        ConfigurationManager.AppSettings["dbConnSt"],
                        dicStart,
                        string.Format("where ID={0} ", scriptId),
                        ConfigurationManager.AppSettings["dbPrefix"]);
                }
                else
                {
                    return;
                }
            }


            var scriptEnt = FunSqlToClass.ClassSingle <SCRIPT>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                "where ID = " + scriptId,
                ConfigurationManager.AppSettings["dbPrefix"]
                );


            ScriptExt ext = new ScriptExt();

            if (scriptEnt.IS_GROUP == (short)1)//表示是任务组
            {
                IList <ProInterface.Models.SCRIPT_GROUP_LIST> reList = new List <ProInterface.Models.SCRIPT_GROUP_LIST>();
                reList = FunSqlToClass.SqlToList <ProInterface.Models.SCRIPT_GROUP_LIST>(string.Format("SELECT * FROM YL_SCRIPT_GROUP_LIST WHERE GROUP_ID={0} ORDER BY ORDER_INDEX", scriptId), ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"]);
                if (reList.Count() > 0)
                {
                    long taskId = 0;
                    ext.AddScriptTask(reList[0].SCRIPT_ID, scriptId, ref taskId);
                    logger.InfoFormat("执行脚本 添加脚本【{0}】任务ID【{1}】", scriptId, taskId);
                }
            }
            else//普通脚本
            {
                long taskId = 0;
                ext.AddScriptTask(scriptId, null, ref taskId);
                logger.InfoFormat("执行脚本 添加脚本【{0}】任务ID【{1}】", scriptId, taskId);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="csharpCode"></param>
        /// <param name="scriptTaskID"></param>
        /// <param name="defaultDb"></param>
        /// <returns>程序集的实例</returns>
        public bool ScriptTaskStart(ref ErrorInfo error, int scriptTaskID)
        {
            var scriptEnt = FunSqlToClass.ClassSingle <SCRIPT_TASK>(
                ConfigurationManager.AppSettings["dbType"],
                ConfigurationManager.AppSettings["dbConnSt"],
                "where ID = " + scriptTaskID,
                ConfigurationManager.AppSettings["dbPrefix"]
                );

            if (scriptEnt == null)
            {
                error.IsError = true;
                error.Message = "脚本不存在";
                return(false);
            }

            string pathInterface = AppDomain.CurrentDomain.BaseDirectory + "ProInterface.dll";
            string pathServer    = AppDomain.CurrentDomain.BaseDirectory + "ProServer.dll";

            #region 判断类库是否存在
            if (!System.IO.File.Exists(pathInterface))
            {
                pathInterface = AppDomain.CurrentDomain.BaseDirectory + "Bin\\ProInterface.dll";
            }
            if (!System.IO.File.Exists(pathServer))
            {
                pathServer = AppDomain.CurrentDomain.BaseDirectory + "Bin\\ProServer.dll";
            }

            if (!System.IO.File.Exists(pathInterface))
            {
                error.Message = string.Format("类库【{0}】不存在", pathInterface);
                return(false);
            }
            if (!System.IO.File.Exists(pathServer))
            {
                error.Message = string.Format("类库【{0}】不存在", pathInterface);
                return(false);
            }
            #endregion

            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
            ICodeCompiler      objICodeCompiler      = objCSharpCodePrivoder.CreateCompiler();
            CompilerParameters objCompilerParameters = new CompilerParameters();
            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            objCompilerParameters.ReferencedAssemblies.Add(pathInterface);
            objCompilerParameters.ReferencedAssemblies.Add(pathServer);
            objCompilerParameters.GenerateExecutable = false;
            objCompilerParameters.GenerateInMemory   = true;

            string allCode = "";
            try
            {
                allCode = GenerateCode(scriptEnt.BODY_TEXT, AnalysisRunDate(scriptEnt.RUN_DATA));
            }
            catch
            {
                ErrorScriptTask(error.Message, Convert.ToInt32(scriptEnt.ID));
                return(false);
            }
            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, allCode);
            if (cr.Errors.HasErrors)
            {
                StringBuilder sb = new StringBuilder("编译错误:");
                foreach (CompilerError err in cr.Errors)
                {
                    sb.AppendLine(string.Format("行{0}列{1}:{2} <br />", err.Line - 12, err.Column, err.ErrorText));
                }
                error.IsError = true;
                error.Message = sb.ToString();

                ErrorScriptTask(error.Message, Convert.ToInt32(scriptEnt.ID));

                return(false);
            }
            else
            {
                // 通过反射,调用HelloWorld的实例
                objAssembly = cr.CompiledAssembly;
                object objScripRun = objAssembly.CreateInstance("ProServer.ScripRun");

                //创建任务
                var bodyText = objScripRun.GetType().GetMethod("ScriptTaskRun").Invoke(objScripRun, new object[] { scriptEnt.ID });
                var setnowdb = objScripRun.GetType().GetMethod("setnowdb").Invoke(objScripRun, new object[] { scriptEnt.SERVICE_FLAG });
                //运行脚本
                var run      = objScripRun.GetType().GetMethod("Run").Invoke(objScripRun, null);
                var errorMsg = objScripRun.GetType().GetMethod("GetError").Invoke(objScripRun, null);

                IList <ProInterface.Models.SCRIPT_GROUP_LIST> reList = FunSqlToClass.SqlToList <ProInterface.Models.SCRIPT_GROUP_LIST>(
                    string.Format("SELECT * FROM YL_SCRIPT_GROUP_LIST WHERE GROUP_ID={0} ORDER BY ORDER_INDEX", (scriptEnt.GROUP_ID == null) ? 0 : scriptEnt.GROUP_ID.Value),
                    ConfigurationManager.AppSettings["dbType"],
                    ConfigurationManager.AppSettings["dbConnSt"]);
                if (reList.Count() > 0 && (bool)run && string.IsNullOrEmpty(errorMsg.ToString()))
                {
                    var nowPlace = reList.SingleOrDefault(x => x.SCRIPT_ID == scriptEnt.SCRIPT_ID);
                    var last     = reList.Where(x => x.ORDER_INDEX > nowPlace.ORDER_INDEX).ToList();
                    if (last.Count() > 0)
                    {
                        long      taskId = 0;
                        ScriptExt ext    = new ScriptExt();
                        ext.AddScriptTask(last[0].SCRIPT_ID, last[0].GROUP_ID, ref taskId);
                    }
                }
                return(string.IsNullOrEmpty(errorMsg.ToString()));
            }
        }