private void HandleAppServerConfigurationChange(ProcessControlConfiguration config)
        {
            Console.WriteLine("ChangeEvent: ----------------- {0}", DateTime.Now.ToString("HH:mm:ss.ffffff"));

            foreach (var val in config.ProcessInstances)
            {
                Console.WriteLine("{0}, {1}", val.Name, val.WorkingDirectory);
            }

            //this.stopEvent.Set();
        }
        public static void SetupAppProcesses(string serverConfigFile)
        {
            if (string.IsNullOrWhiteSpace(serverConfigFile))
                throw new ArgumentNullException(serverConfigFile);

            serverConfigFilePath = serverConfigFile;

            // Load Controller Configuration 
            currentAppServerConfiguration = ProcessConfigurationManager.GetConfiguration(serverConfigFile);
            if (currentAppServerConfiguration == null)
                throw new ConfigurationErrorsException(serverConfigFile);

            var watch = ProcessConfigurationManager.EnableRaisingFileWatcherEvent;
            var monitor = currentAppServerConfiguration.MonitorProcess();
            var interval = currentAppServerConfiguration.MonitorTimeInterval();

            SetUpController(watch, monitor, interval);

            appAppProcesses = SetupAppProcesses(currentAppServerConfiguration);
        }
        private static ConcurrentDictionary<string, AppProcess> SetupAppProcesses(ProcessControlConfiguration serverConfig)
        {
            var appServers = new ConcurrentDictionary<string, AppProcess>();

            foreach (var val in serverConfig.ProcessInstances)
            {
                AppProcess module = SetupAppProcess(val);
                if (module != null)
                {
                    string guid = module.ProcessGuid;

                    appServers.TryAdd(guid, module);
                }
            }

            logger.DebugFormat("Setup [{0}] worker processes.", appServers.Count());

            return appServers;
        }
        private static void ManageAppServers(ProcessControlConfiguration updatedConfig)
        {
            foreach (var val in updatedConfig.ProcessInstances)
            {
                var module = appAppProcesses.GetAppProcessByInstanceID(val.InstanceID);
                if (module == null)   // New ProcessInstance
                {
                    logger.DebugFormat("Found the mew worker process setting with the InstanceID - {0} and Command - {1}",
                        val.InstanceID, val.Command);

                    if (! string.Equals(val.Command.ToUpper(), CommandMessage.STOP))
                    {
                        var newmodule = SetupAppProcess(val);
                        string guid = newmodule.ProcessGuid;
                        appAppProcesses.TryAdd(guid, newmodule);

                        logger.DebugFormat("Starting the new worker process with the InstanceID - {0}", val.InstanceID);

                        StartAppProcess(guid);
                    }
                }
                else // Existing ProcessInstance
                {
                    string guid = module.ProcessGuid;

                    if (module.IsRunning)
                    {
                        if (string.Equals(val.Command.ToUpper(), CommandMessage.STOP))
                        {
                            logger.DebugFormat("Stopping the existing worker process with the InstanceID - {0}", val.InstanceID);

                            StopAppProcess(guid);
                        }

                        if (string.Equals(val.Command.ToUpper(), CommandMessage.RECONFIGURE))
                        {
                            logger.DebugFormat("Reconfiguring the existing worker process with the InstanceID - {0}", val.InstanceID);

                            ReconfigureAppProcess(guid);
                        }
                    }
                    else
                    {
                        if (string.Equals(val.Command.ToUpper(), CommandMessage.START))
                        {
                            logger.DebugFormat("Starting the existing worker process with the InstanceID - {0}", val.InstanceID);

                            StartAppProcess(guid);
                        }
                    }
                }
            }

            List<string> removeList = new List<string>();
            foreach (var val in appAppProcesses.Values)
            {
                var setting = updatedConfig.ProcessInstances.Get(val.InstanceID);

                if (setting == null)
                    removeList.Add(val.ProcessGuid);
            }

            foreach(var val in removeList)
            {
                StopAppProcess(val);

                logger.DebugFormat("Removed the worker process not in the updated AppServerConfiguration. ");
            }
        }
        private static void HandleAppServerConfigurationUpdate(ProcessControlConfiguration updatedConfig)
        {
            lock (eventLocker)
            {
                logger.DebugFormat("Received the updated ProcessControlConfiguration from the file change event. {0}", updatedConfig.SerializeToString());

                if ((monitorTimerTask != null) && (monitoring))
                {
                    monitorTimerTask.StopTimer();
                }

                var watch = ProcessConfigurationManager.EnableRaisingFileWatcherEvent;
                var monitor = updatedConfig.MonitorProcess();
                var interval = updatedConfig.MonitorTimeInterval();

                SetUpController(watch, monitor, interval);

                ManageAppServers(updatedConfig);

                if ((monitorTimerTask != null) && (monitoring))
                {
                    monitorTimerTask.StartTimer(interval);
                }
            }
        }