Example #1
0
        protected override IManagedAppBase CreateAndStartServerInstance()
        {
            var currentDomain = AppDomain.CurrentDomain;

            m_Locker = new ProcessLocker(AppWorkingDir, "instance.lock");

            var process = m_Locker.GetLockedProcess();

            if (process == null)
            {
                try
                {
                    process = Process.Start(m_StartInfo);
                }
                catch (Exception e)
                {
                    OnExceptionThrown(e);
                    return null;
                }

                m_Locker.SaveLock(process);
            }

            m_WorkingProcess = process;

            process.EnableRaisingEvents = true;
            process.ErrorDataReceived += new DataReceivedEventHandler(WorkingProcess_ErrorDataReceived);
            process.BeginErrorReadLine();

            process.Exited += new EventHandler(WorkingProcess_Exited);

            m_PerformanceCounter = new ProcessPerformanceCounter(process, PerformanceCounterInfo.GetDefaultPerformanceCounterDefinitions());

            m_Status.StartedTime = DateTime.Now;
            m_Status[StatusInfoKeys.IsRunning] = true;

            return new NullManagedApp();
        }
Example #2
0
 void WorkingProcess_Exited(object sender, EventArgs e)
 {
     m_Locker.CleanLock();
     m_PerformanceCounter = null;
     OnStopped();
 }
Example #3
0
        protected override IManagedAppBase CreateAndStartServerInstance()
        {
            var currentDomain = AppDomain.CurrentDomain;

            m_Locker = new ProcessLocker(AppWorkingDir, "instance.lock");

            var process = m_Locker.GetLockedProcess();

            if (process == null)
            {
                var args = string.Join(" ", (new string[] { Name }).Select(a => "\"" + a + "\"").ToArray());

                ProcessStartInfo startInfo;

                if (!NRack.Base.NRackEnv.IsMono)
                {
                    startInfo = new ProcessStartInfo(ProcessAppConst.WorkerAssemblyName, args);
                }
                else
                {
                    startInfo = new ProcessStartInfo((Path.DirectorySeparatorChar == '\\' ? "mono.exe" : "mono"), "--runtime=v" + System.Environment.Version.ToString(2) + " \"" + ProcessAppConst.WorkerAssemblyName + "\" " + args);
                }

                startInfo.CreateNoWindow = true;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                startInfo.WorkingDirectory = currentDomain.BaseDirectory;
                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                startInfo.RedirectStandardInput = true;

                try
                {
                    m_WorkingProcess = Process.Start(startInfo);
                }
                catch (Exception e)
                {
                    OnExceptionThrown(e);
                    return null;
                }

                m_WorkingProcess.EnableRaisingEvents = true;
                m_WorkingProcess.ErrorDataReceived += new DataReceivedEventHandler(WorkingProcess_ErrorDataReceived);
                m_WorkingProcess.OutputDataReceived += new DataReceivedEventHandler(WorkingProcess_OutputDataReceived);
                m_WorkingProcess.BeginErrorReadLine();
                m_WorkingProcess.BeginOutputReadLine();
            }
            else
            {
                m_WorkingProcess = process;
                m_WorkingProcess.EnableRaisingEvents = true;
            }

            var portName = string.Format(ProcessAppConst.PortNameTemplate, Name, m_WorkingProcess.Id);
            m_ServerTag = portName;

            var remoteUri = string.Format(ProcessAppConst.WorkerUri, portName, ProcessAppConst.WorkerRemoteName);

            IRemoteManagedApp appServer = null;

            if (process == null)
            {
                var startTimeOut = 0;

                int.TryParse(Config.Options.GetValue("startTimeOut", "0"), out startTimeOut);

                if (startTimeOut <= 0)
                {
                    startTimeOut = 10;
                }

                if (!m_ProcessWorkEvent.WaitOne(1000 * startTimeOut))
                {
                    ShutdownProcess();
                    OnExceptionThrown(new Exception("The remote work item was timeout to setup!"));
                    return null;
                }

                if (!"Ok".Equals(m_ProcessWorkStatus, StringComparison.OrdinalIgnoreCase))
                {
                    OnExceptionThrown(new Exception("The worker process didn't start successfully!"));
                    return null;
                }

                appServer = GetRemoteServer(remoteUri);

                if (appServer == null)
                    return null;

                var bootstrapIpcPort = AppDomain.CurrentDomain.GetData("BootstrapIpcPort") as string;

                if (string.IsNullOrEmpty(bootstrapIpcPort))
                    throw new Exception("The bootstrap's remoting service has not been started.");

                var ret = false;
                Exception exc = null;

                try
                {
                    //Setup and then start the remote server instance
                    ret = appServer.Setup(GetMetadata().AppType, "ipc://" + bootstrapIpcPort + "/Bootstrap.rem", currentDomain.BaseDirectory, Config, StartupConfigFile);
                }
                catch (Exception e)
                {
                    exc = e;
                }

                if (!ret)
                {
                    ShutdownProcess();
                    OnExceptionThrown(new Exception("The remote work item failed to setup!", exc));
                    return null;
                }

                try
                {
                    ret = appServer.Start();
                }
                catch (Exception e)
                {
                    ret = false;
                    exc = e;
                }

                if (!ret)
                {
                    ShutdownProcess();
                    OnExceptionThrown(new Exception("The remote work item failed to start!", exc));
                    return null;
                }

                m_Locker.SaveLock(m_WorkingProcess);
            }
            else
            {
                appServer = GetRemoteServer(remoteUri);

                if (appServer == null)
                    return null;
            }

            m_WorkingProcess.Exited += new EventHandler(WorkingProcess_Exited);

            m_PerformanceCounter = new ProcessPerformanceCounter(m_WorkingProcess, PerformanceCounterInfo.GetDefaultPerformanceCounterDefinitions());

            return appServer;
        }