Пример #1
0
 private void Stop()
 {
     timer.Elapsed -= StartTimer;
     timer.Stop();
     timer.Close();
     timer.Dispose();
     timer.EndInit();
     timer = null;
     if (downloadClient != null)
     {
         downloadClient.Dispose();
     }
     AllControlStatus(true);
     startBtn.Text = "Go!";
     ChangeStatus("停止");
 }
        /// <summary>
        /// Initializes the manager
        /// </summary>
        /// <returns>TRUE for success; FALSE otherwise</returns>
        public bool InitMgr()
        {
            // Get the manager settings
            try
            {
                m_MgrSettings = new clsMgrSettings();
            }
            catch
            {
                // Failures are logged by clsMgrSettings
                return false;
            }

            // Update the cached manager name
            m_MgrName = m_MgrSettings.GetParam("MgrName");

            // Set up the loggers
            var logFileName = m_MgrSettings.GetParam("logfilename");

            // LogLevel is 1 to 5: 1 for Fatal errors only, 4 for Fatal, Error, Warning, and Info, and 5 for everything including Debug messages
            var debugLevel = int.Parse(m_MgrSettings.GetParam("debuglevel"));
            clsLogTools.CreateFileLogger(logFileName, debugLevel);
            var logCnStr = m_MgrSettings.GetParam("connectionstring");

            clsLogTools.CreateDbLogger(logCnStr, "SpaceManager: " + m_MgrName);

            // Make initial log entry
            var msg = "=== Started Space Manager V" + System.Windows.Forms.Application.ProductVersion + " ===== ";
            clsLogTools.WriteLog(clsLogTools.LoggerTypes.LogFile, clsLogTools.LogLevels.INFO, msg);

            // Setup the message queue
            m_MsgQueueInitSuccess = false;
            m_MsgHandler = new clsMessageHandler();
            m_MsgHandler.BrokerUri = m_MsgHandler.BrokerUri = m_MgrSettings.GetParam("MessageQueueURI");
            m_MsgHandler.CommandQueueName = m_MgrSettings.GetParam("ControlQueueName");
            m_MsgHandler.BroadcastTopicName = m_MgrSettings.GetParam("BroadcastQueueTopic");
            m_MsgHandler.StatusTopicName = m_MgrSettings.GetParam("MessageQueueTopicMgrStatus");
            m_MsgHandler.MgrSettings = m_MgrSettings;

            // Initialize the message queue
            // Start this in a separate thread so that we can abort the initialization if necessary
            InitializeMessageQueue();

            if (m_MsgQueueInitSuccess)
            {
                //Connect message handler events
                m_MsgHandler.CommandReceived += OnCommandReceived;
                m_MsgHandler.BroadcastReceived += OnBroadcastReceived;
            }

            // Setup a file watcher for the config file
            var fInfo = new FileInfo(System.Windows.Forms.Application.ExecutablePath);
            m_FileWatcher = new FileSystemWatcher();
            m_FileWatcher.BeginInit();
            m_FileWatcher.Path = fInfo.DirectoryName;
            m_FileWatcher.IncludeSubdirectories = false;
            m_FileWatcher.Filter = m_MgrSettings.GetParam("configfilename");
            m_FileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
            m_FileWatcher.EndInit();
            m_FileWatcher.EnableRaisingEvents = true;

            // Subscribe to the file watcher Changed event
            m_FileWatcher.Changed += FileWatcherChanged;

            // Set up the tool for getting tasks
            m_Task = new clsSpaceMgrTask(m_MgrSettings);

            // Set up the status file class
            var statusFileNameLoc = Path.Combine(fInfo.DirectoryName, "Status.xml");
            m_StatusFile = new clsStatusFile(statusFileNameLoc)
            {
                //Note: Might want to put this back in someday
                //MonitorUpdateRequired += new StatusMonitorUpdateReceived(OnStatusMonitorUpdateReceived);
                LogToMsgQueue = bool.Parse(m_MgrSettings.GetParam("LogStatusToMessageQueue")),
                MgrName = m_MgrName,
                MgrStatus = EnumMgrStatus.Running
            };
            m_StatusFile.WriteStatusFile();

            // Set up the status reporting time
            m_StatusTimer = new System.Timers.Timer();
            m_StatusTimer.BeginInit();
            m_StatusTimer.Enabled = false;
            m_StatusTimer.Interval = 60000;	// 1 minute
            m_StatusTimer.EndInit();
            m_StatusTimer.Elapsed += m_StatusTimer_Elapsed;

            // Get the most recent job history
            var historyFile = Path.Combine(m_MgrSettings.GetParam("ApplicationPath"), "History.txt");
            if (File.Exists(historyFile))
            {
                try
                {
                    // Create an instance of StreamReader to read from a file.
                    // The using statement also closes the StreamReader.
                    using (var sr = new StreamReader(historyFile))
                    {
                        String line;
                        // Read and display lines from the file until the end of
                        // the file is reached.
                        while ((line = sr.ReadLine()) != null)
                        {
                            if (line.Contains("RecentJob: "))
                            {
                                var tmpStr = line.Replace("RecentJob: ", "");
                                m_StatusFile.MostRecentJobInfo = tmpStr;
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogError("Exception reading status history file", ex);
                }
            }

            // Set up the storage operations class
            m_StorageOps = new clsStorageOperations(m_MgrSettings);

            // Everything worked!
            return true;
        }