Ejemplo n.º 1
0
        private void FilesChanged(List <string> files)
        {
            if (files.Contains(m_TaskConfigMgr.ConfigFilePath, StringComparer.InvariantCultureIgnoreCase))
            {
                Restart(m_TaskConfigMgr.GetSetting());
                return;
            }

            foreach (IExecuteTask executor in m_TaskExecutorList)
            {
                if (executor is AppDomainTaskExecutor)
                {
                    try
                    {
                        AppDomainTaskExecutor ext = (AppDomainTaskExecutor)executor;
                        foreach (string filePath in ext.WatchFileList)
                        {
                            if (WildcardContains(files, filePath))
                            {
                                OnEachTaskRestarting(this, new TaskEventArgs(FolderPath, executor.TaskID));
                                try
                                {
                                    ext.Restart();
                                }
                                catch (Exception ex)
                                {
                                    OnUnhandledException(this, new ExceptionEventArgs(ex, FolderPath, executor.TaskID, "发现硬盘相关文件变化,重启服务时出现异常!"));
                                }
                                OnEachTaskRestarted(this, new TaskEventArgs(FolderPath, executor.TaskID));
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        OnUnhandledException(ex, executor.TaskID, "发现硬盘相关文件变化,执行Task”" + executor.TaskID + "“的Restart方法出错");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void InitFromTasks(List <TaskInfo> infoList)
        {
            m_TaskInfoList     = infoList;
            m_TaskExecutorList = new List <IExecuteTask>(infoList.Count);
            bool needAllFileWatch = false;

            foreach (TaskInfo info in infoList)
            {
                IExecuteTask tmp;
                if (info.IsExe)
                {
                    tmp = new ExeTaskExecutor(FolderPath, info);
                }
                else if (info.IsSp)
                {
                    tmp = new SpTaskExecutor(FolderPath, info);
                }
                else if (info.WatchFileList == null || info.WatchFileList.RelativePath == null || info.WatchFileList.RelativePath.Count <= 0)
                {
                    tmp = new TaskExecutor(FolderPath, info);
                }
                else
                {
                    tmp = new AppDomainTaskExecutor(FolderPath, info);
                    needAllFileWatch = true;
                }
                tmp.UnhandledException += (s, e) => { OnUnhandledException(s, e); };
                tmp.ExecutedNotify     += (s, e) => { OnExecutedNotify(s, e); };
                m_TaskExecutorList.Add(tmp);
            }
            if (needAllFileWatch)
            {
                m_FileWatcher = new FileWatcher(FolderPath, FilesChanged);
            }
            else
            {
                m_FileWatcher = new FileWatcher(FolderPath, Path.GetFileName(m_TaskConfigMgr.ConfigFilePath), FilesChanged);
            }
        }
Ejemplo n.º 3
0
        internal static void DomainCallBack()
        {
            TaskInfo t          = (TaskInfo)AppDomain.CurrentDomain.GetData("TaskInfo");
            string   folderPath = AppDomain.CurrentDomain.GetData("DllFolderPath").ToString();

            AppDomain.CurrentDomain.SetData("APPBASE", folderPath);
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", Path.Combine(folderPath, "app.config"));
            AppDomainTaskExecutor handler = (AppDomainTaskExecutor)AppDomain.CurrentDomain.GetData("ExceptionHandler");

            AppDomain.CurrentDomain.UnhandledException += (s, e) =>
            {
                if (e.ExceptionObject is System.Exception)
                {
                    Exception ex1  = (Exception)e.ExceptionObject;
                    string    desc = "AppDomain的UnhandledException事件被触发";
                    Logger.Error(new ExceptionEventArgs(ex1.ToString(), folderPath, t.ID, desc));
                    try
                    {
                        handler.OnUnhandledException(ex1.ToString(), desc);
                    }
                    catch (Exception ex2)
                    {
                        Logger.Error(folderPath, t.ID, ex2, "执行AppDomain.CurrentDomain.UnhandledException的OnUnhandledException时出错");
                    }
                }
            };
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                string path = args.Name.Split(',')[0].Trim() + ".dll";
                path = Path.Combine(AppDomain.CurrentDomain.GetData("DllFolderPath").ToString(), path);
                if (File.Exists(path))
                {
                    try
                    {
                        return(Assembly.LoadFrom(path));
                    }
                    catch (Exception ex)
                    {
                        var de = new ExceptionEventArgs(ex, folderPath, t.ID, "Failed to load assembly \"" + path + "\" in AppDomain.CurrentDomain.AssemblyResolve.");
                        Logger.Error(de);
                        try
                        {
                            handler.OnUnhandledException(de);
                        }
                        catch (Exception ex1)
                        {
                            Logger.Error(folderPath, t.ID, ex1, "执行AppDomain.CurrentDomain.AssemblyResolve的OnUnhandledException时出错");
                        }
                    }
                }
                return(null);
            };
            TaskExecutor executor = new TaskExecutor(folderPath, t);

            executor.UnhandledException += (s, e) =>
            {
                handler.OnUnhandledException(e);
            };
            executor.ExecutedNotify += (s, e) =>
            {
                handler.OnExecutedNotify(e);
            };
            AppDomain.CurrentDomain.SetData("Executor", executor);
        }