Ejemplo n.º 1
0
 protected void Application_Start(object sender, EventArgs e)
 {
     //服务启动的时候启动定时任务管理器
     try
     {
         //Process[] arr = Process.GetProcessesByName("Component.TimerTask.TaskManager");
         //if (arr.Length == 0)
         //{
         //    TaskManagerProcess = Process.Start(HttpContext.Current.Server.MapPath("Bin") + "\\Component.TimerTask.TaskManager.exe");
         //}
         IBLLService ts = new TaskService();
         if (!ts.IsTaskManagerAlive())
         {
             ts.StartTaskManager();
         }
     }
     catch(Exception ex)
     {
         IBLLService bll = BLLFactory.GetBLL();
         LogEntity log = new LogEntity();
         log.LogContent = ex.Message;
         //log.LogDate = DateTime.Now;
         log.LogType = LogType.TaskManagerStartError;
         log.TaskID = -1;
         bll.WriteLog(log);
     }
 }
Ejemplo n.º 2
0
 public static void ReserMappingLogEntity(LogEntity paraLog, ref LogDataSet.PL_TimerTask_LogRow paraLogRow)
 {
     paraLogRow.LogDate = paraLog.LogDate;
     paraLogRow.LogType = paraLog.LogType.ToString();
     paraLogRow.TaskID = paraLog.TaskID;
     paraLogRow.TaskName = paraLog.TaskName;
     paraLogRow.LogContent = paraLog.LogContent;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 循环监听函数
        /// </summary>
        private void ThreadFuncRecieve(object state)
        {
            while (true)
            {
                try
                {
                    byte[] recieveByte = new byte[1024];
                    Socket recieveSocket = _Socket.Accept();
                    //关键地方,Recieve方法会阻塞线程
                    recieveSocket.Receive(recieveByte);
                    SocketHelper.Send(recieveSocket, SocketHelper.HANDSHAKE);
                    string recieveContent = Encoding.Default.GetString(recieveByte);
                    SocketHelper.CloseSocket(recieveSocket);

                    //解析取到的消息
                    List<TaskEntity> addedList;
                    List<Int64> deledList;
                    List<TaskEntity> updateList;
                    List<Int64> stopedList;
                    List<Int64> runList;
                    List<RunTaskType> runTypeList;
                    MessageParser.ParseMessage(recieveContent, out addedList, out deledList, out updateList, out runList,out runTypeList, out stopedList);

                    #region 先保存入库
                    //----入库在发消息前已经保存,这里不再保存
                    //foreach (TaskEntity entity in addedList)
                    //{
                    //    _IBLLLogic.AddTask2DB(entity);
                    //}
                    //foreach (TaskEntity entity in updateList)
                    //{
                    //    _IBLLLogic.UpdateTask2DB(entity);
                    //}
                    //foreach (Int64 entitu in deledList)
                    //{
                    //    _IBLLLogic.DeleteTask2DB(entitu);
                    //}

                    #endregion

                    //用于检查从Socket接收过来的实体是否在本地有配置
                    List<string> regestApps = _IBLLLogic.GetRegestedApp();
                    //开始更新引擎中的任务列表
                    foreach (TaskEntity entity in addedList)
                    {
                        if (regestApps.Contains(entity.RegestesAppName))
                        {
                            _Engine.AddWorkingTask(entity);
                        }
                        else
                        {
                            string s = "传入的计划尚未在本地配置:" + "\t" + entity.ToString();
                            Console.WriteLine(s);
                            _IBLLLogic.WriteLog(entity.ID, entity.Name, s, LogType.SocketRecieveTaskNoExist);
                        }
                    }
                    foreach (TaskEntity entity in updateList)
                    {
                        if (regestApps.Contains(entity.RegestesAppName))
                        {
                            _Engine.ModifyTask(entity);
                        }
                        else
                        {
                            string s = "传入的计划尚未在本地配置:" + "\t" + entity.ToString();
                            Console.WriteLine(s);
                            _IBLLLogic.WriteLog(entity.ID, entity.Name, s, LogType.SocketRecieveTaskNoExist);
                        }
                    }
                    foreach (Int64 entitt in deledList)
                    {
                        _Engine.DelTask(entitt);
                    }
                    for (int i = 0; i < runList.Count; i++)
                    {
                        _Engine.ManualRunTask(runList[i], runTypeList[i]);
                    }
                    foreach (Int64 entity in stopedList)
                    {
                        _Engine.StopRuningTask(entity);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
                    LogEntity log = new LogEntity();
                    log.LogContent = ex.Message;
                    log.LogType = Component.TimerTask.Model.Enums.LogType.SocketServerRecievveError;
                    _IBLLLogic.WriteLog(log);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 开始工作
        /// </summary>
        /// <param name="paraRunType">被调度方式</param>
        public override void DoWork(RunTaskType paraRunType)
        {
            try
            {
                Console.WriteLine("**************************  Work Start {0} {1}", _WrkTask.Task.TaskEntity.Name, DateTime.Now);

                #region 开始工作
                string destFile = Utility.AssemblyHelper.GetAssemblyPath() + _WrkTask.Task.TaskAssembly.AppFile;    //最好用Path.Combine
                if (File.Exists(destFile))
                {
                    FileInfo fi = new FileInfo(destFile);
                    object obj;

                    #region 反射加载
                    try
                    {
                        Assembly abl;
                        List<Assembly> assmlys = new List<Assembly>();
                        assmlys.AddRange(AppDomain.CurrentDomain.GetAssemblies());
                        abl = assmlys.Find(delegate(Assembly aa) { return aa.Location.Equals(fi.FullName); });
                        assmlys = null;
                        if (abl == null)
                        {
                            abl = System.Reflection.Assembly.LoadFrom(fi.FullName);
                        }

                        obj = abl.CreateInstance(_WrkTask.Task.TaskAssembly.ProtocolNameSpace + "." + _WrkTask.Task.TaskAssembly.ProtocolClass);
                    }
                    catch (System.Reflection.TargetInvocationException ex)   //捕获反射错误的异常
                    {
                        string s = "无法加载目标对象,请检查与目标对象相关联的引用是否存在。" + ex.Message;
                        Console.WriteLine("执行任务发生异常:{0}", s);
                        _BLL.WriteLog(_WrkTask.Task.TaskEntity.ID, _WrkTask.Task.TaskEntity.Name, s, LogType.ReflectError);
                        return;
                    }
                    #endregion

                    #region 转换为ITask接口
                    if (obj is ITask)
                    {
                        _WorkInterface = (ITask)obj;
                    }
                    else
                    {
                        string s = "无法加载目标对象,目标对象未继承ITask接口。";
                        Console.WriteLine("执行任务发生异常:{0}", s);
                        _BLL.WriteLog(_WrkTask.Task.TaskEntity.ID, _WrkTask.Task.TaskEntity.Name, s, LogType.TypeConvertITaskError);
                        base.DoWork(paraRunType);
                        return;
                    }
                    #endregion

                    _WorkInterface.OnFuncComplete = new WaitCallback(Process_Exited);
                    _WorkInterface.ExtraParaStr = _WrkTask.Task.TaskEntity.ExtraParaStr;
                    Thread thWork = new Thread(new ThreadStart(_WorkInterface.RunTask));
                    _Thread4Work = thWork;
                    _Thread4Work.IsBackground = true;
                    _Thread4Work.Start();

                    #region 监控超时
                    if (_WrkTask.Task.TaskEntity.RunTimeOutSecs > 0)
                    {
                        //ParameterizedThreadStart threadStart = new ParameterizedThreadStart(WorkMonitor);
                        //Thread th = new Thread(threadStart);
                        //th.IsBackground = true;
                        //th.Start(thWork);
                        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadFuncWorkMonitor), thWork);
                        //th.Start(_WorkInterface);
                    }
                    #endregion
                }
                else
                {
                    string s = string.Format("目标位置不存在文件,无法执行该任务({0})", destFile);
                    Console.WriteLine(s);
                    _BLL.WriteLog(_WrkTask.Task.TaskEntity.ID, _WrkTask.Task.TaskEntity.Name, s, LogType.TaskConfigAssemblyFileNotFind);
                    //return;
                }
                #endregion

                base.DoWork(paraRunType);
            }
            catch (Exception ex)
            {
                LogEntity log = new LogEntity();
                log.LogContent = ex.Message;
                log.LogType = LogType.EnforceKillWorkError;
                log.TaskID = _WrkTask.Task.TaskEntity.ID;
                log.TaskName = _WrkTask.Task.TaskEntity.Name;
                _BLL.WriteLog(log);
                Console.WriteLine("执行任务发生异常:{0}", ex.Message);
            }
        }
Ejemplo n.º 5
0
        public override void ManualStopWork()
        {
            try
            {
                if (_WorkInterface != null)
                {
                    _WorkInterface.StopRuning();
                }

                if (_Thread4Work != null && _Thread4Work.ThreadState != System.Threading.ThreadState.Aborted)
                {
                    try
                    {
                        _Thread4Work.Abort();
                    }
                    catch { }
                }
                //base.ManualStopWork();

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                LogEntity log = new LogEntity();
                log.LogContent = ex.Message;
                log.LogType = LogType.EnforceKillWorkError;
                log.TaskID = _WrkTask.Task.TaskEntity.ID;
                log.TaskName = _WrkTask.Task.TaskEntity.Name;
                _BLL.WriteLog(log);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 写日志
 /// </summary>
 /// <param name="paraLogEntity"></param>
 public void WriteLog(LogEntity paraLogEntity)
 {
     _BLL.WriteLog(paraLogEntity);
 }
Ejemplo n.º 7
0
 public bool WriteLog(LogEntity paraLog)
 {
     return _Log.WriteLog(paraLog);
 }
Ejemplo n.º 8
0
 public void WriteLog(long paraTaskid, string paraTaskName, string paraContent, Component.TimerTask.Model.Enums.LogType paraLogType)
 {
     LogEntity log = new LogEntity();
     log.LogContent = paraContent;
     log.LogType = paraLogType;
     log.TaskID = paraTaskid;
     log.TaskName = paraTaskName;
     this.WriteLog(log);
 }
Ejemplo n.º 9
0
        public override void ManualStopWork()
        {
            try
            {
                if (_Process != null && !_Process.HasExited)
                {
                    _Process.Kill();
                }
                base.ManualStopWork();

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                LogEntity log = new LogEntity();
                log.LogContent = ex.Message;
                log.LogType = LogType.EnforceKillWorkError;
                log.TaskID = _WrkTask.Task.TaskEntity.ID;
                log.TaskName = _WrkTask.Task.TaskEntity.Name;
                _BLL.WriteLog(log);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 开始工作
        /// </summary>
        /// <param name="paraRunType">被调度方式</param>
        public override void DoWork(RunTaskType paraRunType)
        {
            try
            {
                Console.WriteLine("**************************  Work Start {0} {1}", _WrkTask.Task.TaskEntity.Name, DateTime.Now);

                base.DoWork(paraRunType);

                #region 开始工作
                string destFile = Utility.AssemblyHelper.GetAssemblyPath() + _WrkTask.Task.TaskAssembly.AppFile;
                if (File.Exists(destFile))
                {
                    FileInfo fi = new FileInfo(destFile);

                    try
                    {
                        _Process = Process.Start(fi.FullName, _WrkTask.Task.TaskEntity.ExtraParaStr);
                    }
                    catch   //捕获执行异常问题
                    {
                        string s = "无法执行目标对象,执行目标对象出现异常:" + fi.FullName;
                        Console.WriteLine("执行任务发生异常:{0}", s);
                        _BLL.WriteLog(_WrkTask.Task.TaskEntity.ID, _WrkTask.Task.TaskEntity.Name, s, LogType.RunExeFileError);
                        return;
                    }
                    _Process.EnableRaisingEvents = true;
                    _Process.Exited += new EventHandler(_Process_Exited);
                    #region 监控超时
                    if (_WrkTask.Task.TaskEntity.RunTimeOutSecs > 0)
                    {
                        //ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ThreadWorkMonitor);
                        //Thread th = new Thread(threadStart);
                        //th.IsBackground = true;
                        //th.Start(_Process);
                        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWorkMonitor), _Process);
                    }
                    #endregion
                }
                else
                {
                    string s = string.Format("目标位置不存在文件,无法执行该任务({0})", destFile); ;
                    Console.WriteLine(s);
                    _BLL.WriteLog(_WrkTask.Task.TaskEntity.ID, _WrkTask.Task.TaskEntity.Name, s, LogType.TaskConfigAssemblyFileNotFind);
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogEntity log = new LogEntity();
                log.LogContent = ex.Message;
                log.LogType = LogType.EnforceKillWorkError;
                log.TaskID = _WrkTask.Task.TaskEntity.ID;
                log.TaskName = _WrkTask.Task.TaskEntity.Name;
                _BLL.WriteLog(log);
                Console.WriteLine("执行任务发生异常:{0}", ex.Message);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 给服务器发送消息
        /// </summary>
        /// <param name="paraContent"></param>
        private void SendXMLSocket2Server(string paraContent)
        {
            Socket socket = null; ;
            try
            {
                IPEndPoint ip = SocketHelper.GetIpEndPoint();
                ip.Port = Convert.ToInt32(_DataAccess.ReadConfigValue("LISTENING_ADDRESS"));
                socket = SocketHelper.GetSocket(ip);

                string handshake = SocketHelper.SendMessageGetStr(socket, paraContent);
                if (handshake == SocketHelper.HANDSHAKE)
                {
                    return;
                }
                else
                {
                    throw new Exception("Socket没有收到握手信息");
                }
            }
            catch (Exception ex)
            {
                LogEntity log = new LogEntity();
                log.LogContent = ex.Message;
                log.LogType = LogType.SocketClientSendError;
                log.TaskID = -1;
                WriteLog(log);
                throw new Exception("计划数据通过Socket同步到定时任务管理器失败", ex);
            }
            finally
            {
                if (socket != null)
                    SocketHelper.CloseSocket(socket);
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// 写日志
 /// </summary>
 /// <param name="paraLogEntity"></param>
 public void WriteLog(LogEntity paraLogEntity)
 {
     try
     {
         _DataAccess.WriteLog(paraLogEntity);
     }
     catch
     {
         Console.WriteLine("WriteLog Error, Please Call Administrator To Check");
     }
 }
Ejemplo n.º 13
0
        /// <summary>
        /// 启动任务管理器进程
        /// </summary>
        /// <returns></returns>
        public bool StartTaskManager()
        {
            Process[] arr = Process.GetProcessesByName(StaticConfig.STR_ENGINE_PROCESS_NAME);
            if (arr.Length > 0)
            {
                LogEntity log = new LogEntity();
                log.LogContent = "已经有进程启动,无法再次启动进程。";
                log.LogType = LogType.TaskManagerStartError;
                log.TaskID = -1;
                WriteLog(log);
                return false;
            }

            string path = AssemblyHelper.GetAssemblyPath() + StaticConfig.STR_ENGINE_PROCESS_NAME + ".exe";
            if (File.Exists(path))
            {
                Process.Start(path);
                return true;
            }
            else
            {
                LogEntity log = new LogEntity();
                log.LogContent = "当前目录下不存在定时任务管理器程序(Component.TimerTask.TaskManager.exe)";
                log.LogType = LogType.TaskManagerStartError;
                log.TaskID = -1;
                WriteLog(log);
                return false;
            }
        }
Ejemplo n.º 14
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (false == _Bll.IsTaskManagerAlive())
                {//没有启动
                    this.lbl_State.Text = ProcessState.没有启动.ToString();
                    bool b = _Bll.StartTaskManager();
                    if (false == b)
                    {
                        this.tssl_Info.Text = "当前目录下不存在任务管理器进程,请检查程序。";
                    }
                    else
                    {
                        this.tssl_Info.Text = "成功启动任务管理";
                    }
                }
                else
                {//已经启动
                    this.lbl_State.Text = ProcessState.已经启动.ToString();

                    //监视是否需要营救引擎
                    if (BLLFactory.GetBLLEngineRes().IsNotRecievedLongTime(StaticConfig.TimerTaskEngineIdelSec * 10))
                    {
                        ProcessHelper.KillProcess(StaticConfig.STR_ENGINE_PROCESS_NAME);

                        LogEntity log = new LogEntity();
                        log.LogContent = "Engine Heart Time Out,Killed";
                        log.LogType = LogType.EngineRescue;
                        log.TaskID = -1;
                        BLLFactory.GetBLL().WriteLog(log);
                    }
                }
                InitTaskList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 15
0
 public bool WriteLog(LogEntity paraLog)
 {
     string sql = "INSERT INTO " + _DataSet.PL_TimerTask_Log.TableName + " (" +
         _DataSet.PL_TimerTask_Log.TaskIDColumn.ColumnName + "," +
         _DataSet.PL_TimerTask_Log.TaskNameColumn.ColumnName + "," +
         _DataSet.PL_TimerTask_Log.LogTypeColumn.ColumnName + "," +
         _DataSet.PL_TimerTask_Log.LogContentColumn.ColumnName + "," +
         _DataSet.PL_TimerTask_Log.LogDateColumn.ColumnName + "" +
         ") VALUES(" +
         "@" + _DataSet.PL_TimerTask_Log.TaskIDColumn.ColumnName + "," +
         "@" + _DataSet.PL_TimerTask_Log.TaskNameColumn.ColumnName + "," +
         "@" + _DataSet.PL_TimerTask_Log.LogTypeColumn.ColumnName + "," +
         "@" + _DataSet.PL_TimerTask_Log.LogContentColumn.ColumnName + "," +
         "@" + _DataSet.PL_TimerTask_Log.LogDateColumn.ColumnName + "" +
         ")";
     object[] paraList = new object[5];
     paraList[0] = paraLog.TaskID;
     paraList[1] = paraLog.TaskName;
     paraList[2] = paraLog.LogType.ToString();
     paraList[3] = paraLog.LogContent;
     paraList[4] = paraLog.LogDate;
     int i = DBUtility.SQLiteHelper.ExecuteNonQuery(DBStructureInfo.ConnectionString_log, sql, paraList);
     return i > 0 ? true : false;
 }