示例#1
0
        /// <summary>
        /// Constructor
        /// </summary>
        public clsStatusFile(string statusFilePath, clsMessageHandler msgHandler)
        {
            FileNamePath  = statusFilePath;
            TaskStartTime = DateTime.UtcNow;

            mMsgHandler = msgHandler;

            ClearCachedInfo();
        }
        /// <summary>
        /// Initializes the manager
        /// </summary>
        /// <returns>TRUE for success, FALSE for failure</returns>
        public bool InitMgr()
        {
            // Define the default logging info
            // This will get updated below
            LogTools.CreateFileLogger(DEFAULT_BASE_LOGFILE_NAME, BaseLogger.LogLevels.DEBUG);

            // Create a database logger connected to the Manager Control DB
            // Once the initial parameters have been successfully read,
            // we remove this logger than make a new one using the connection string read from the Manager Control DB
            var defaultDmsConnectionString = Properties.Settings.Default.MgrCnfgDbConnectStr;

            var hostName        = System.Net.Dns.GetHostName();
            var applicationName = "PkgFolderCreateManager_" + hostName;
            var defaultDbLoggerConnectionString = DbToolsFactory.AddApplicationNameToConnectionString(defaultDmsConnectionString, applicationName);

            LogTools.CreateDbLogger(defaultDbLoggerConnectionString, "FolderCreate: " + hostName);

            // Get the manager settings
            try
            {
                var localSettings = GetLocalManagerSettings();

                mMgrSettings = new MgrSettingsDB {
                    TraceMode = false
                };
                RegisterEvents(mMgrSettings);
                mMgrSettings.CriticalErrorEvent += ErrorEventHandler;

                var success = mMgrSettings.LoadSettings(localSettings, true);
                if (!success)
                {
                    if (string.Equals(mMgrSettings.ErrMsg, MgrSettings.DEACTIVATED_LOCALLY))
                    {
                        throw new ApplicationException(MgrSettings.DEACTIVATED_LOCALLY);
                    }

                    throw new ApplicationException("Unable to initialize manager settings class: " + mMgrSettings.ErrMsg);
                }
            }
            catch
            {
                // Failures are logged by clsMgrSettings to local emergency log file
                return(false);
            }

            // Setup the loggers
            var logFileNameBase = mMgrSettings.GetParam("LogFilename", "FolderCreate");

            BaseLogger.LogLevels logLevel;
            if (int.TryParse(mMgrSettings.GetParam("DebugLevel"), out var debugLevel))
            {
                logLevel = (BaseLogger.LogLevels)debugLevel;
            }
            else
            {
                logLevel = BaseLogger.LogLevels.INFO;
            }

            LogTools.CreateFileLogger(logFileNameBase, logLevel);

            // Typically:
            // Data Source=gigasax;Initial Catalog=DMS_Pipeline;Integrated Security=SSPI;
            var logCnStr   = mMgrSettings.GetParam("ConnectionString");
            var moduleName = mMgrSettings.GetParam("ModuleName");

            var dbLoggerConnectionString = DbToolsFactory.AddApplicationNameToConnectionString(logCnStr, mMgrSettings.ManagerName);

            LogTools.CreateDbLogger(dbLoggerConnectionString, moduleName);

            LogTools.MessageLogged += MessageLoggedHandler;

            // Make the initial log entry
            var appVersion = Assembly.GetExecutingAssembly().GetName().Version;
            var msg        = "=== Started Package Folder Creation Manager V" + appVersion + " === ";

            LogTools.LogMessage(msg);

            // Setup the message queue
            mMsgHandler = new clsMessageHandler
            {
                BrokerUri          = mMgrSettings.GetParam("MessageQueueURI"),
                CommandQueueName   = mMgrSettings.GetParam("ControlQueueName"),
                BroadcastTopicName = mMgrSettings.GetParam("BroadcastQueueTopic"),
                StatusTopicName    = mMgrSettings.GetParam("MessageQueueTopicMgrStatus"),
                MgrSettings        = mMgrSettings
            };

            if (!mMsgHandler.Init())
            {
                // Most error messages provided by .Init method, but debug message is here for program tracking
                LogDebug("Message handler init error");
                return(false);
            }

            LogDebug("Message handler initialized");

            // Connect message handler events
            mMsgHandler.CommandReceived   += OnMsgHandler_CommandReceived;
            mMsgHandler.BroadcastReceived += OnMsgHandler_BroadcastReceived;

            // Setup the status file class
            var appPath = PRISM.FileProcessor.ProcessFilesOrDirectoriesBase.GetAppPath();
            var fInfo   = new FileInfo(appPath);

            string statusFileNameLoc;

            if (fInfo.DirectoryName == null)
            {
                statusFileNameLoc = "Status.xml";
            }
            else
            {
                statusFileNameLoc = Path.Combine(fInfo.DirectoryName, "Status.xml");
            }

            mStatusFile = new clsStatusFile(statusFileNameLoc, mMsgHandler)
            {
                LogToMsgQueue = mMgrSettings.GetParam("LogStatusToMessageQueue", false),
                MgrName       = mMgrSettings.ManagerName
            };

            RegisterEvents(mStatusFile);

            mStatusFile.InitStatusFromFile();

            SetStartupStatus();
            mStatusFile.WriteStatusFile();

            LogDebug("Status file init complete");

            // Register the listeners for the message handler
            mMsgHandler.RegisterListeners();

            // Everything worked
            return(true);
        }