Ejemplo n.º 1
0
        protected override void OnStart(string[] args)
        {
            RunningLoop = Task.Run(
                () =>
            {
                try
                {
                    bool stopping;
                    WriteInfo("Starting VSTS Agent Service");
                    TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5);

                    lock (ServiceLock)
                    {
                        stopping = Stopping;
                    }

                    while (!stopping)
                    {
                        WriteInfo("Starting VSTS Agent listener");
                        lock (ServiceLock)
                        {
                            AgentListener = CreateAgentListener();
                            AgentListener.OutputDataReceived += AgentListener_OutputDataReceived;
                            AgentListener.ErrorDataReceived  += AgentListener_ErrorDataReceived;
                            AgentListener.Start();
                            AgentListener.BeginOutputReadLine();
                            AgentListener.BeginErrorReadLine();
                        }

                        AgentListener.WaitForExit();
                        int exitCode = AgentListener.ExitCode;

                        // exit code 0 and 1 need stop service
                        // exit code 2 and 3 need restart agent
                        switch (exitCode)
                        {
                        case 0:
                            Stopping = true;
                            WriteInfo(Resource.AgentExitWithoutError);
                            break;

                        case 1:
                            Stopping = true;
                            WriteInfo(Resource.AgentExitWithTerminatedError);
                            break;

                        case 2:
                            WriteInfo(Resource.AgentExitWithError);
                            break;

                        case 3:
                            WriteInfo(Resource.AgentUpdateInProcess);
                            var updateResult = HandleAgentUpdate();
                            if (updateResult == AgentUpdateResult.Succeed)
                            {
                                WriteInfo(Resource.AgentUpdateSucceed);
                            }
                            else if (updateResult == AgentUpdateResult.Failed)
                            {
                                WriteInfo(Resource.AgentUpdateFailed);
                                Stopping = true;
                            }
                            else if (updateResult == AgentUpdateResult.SucceedNeedRestart)
                            {
                                WriteInfo(Resource.AgentUpdateRestartNeeded);
                                _restart = true;
                                ExitCode = int.MaxValue;
                                Stop();
                            }
                            break;

                        default:
                            WriteInfo(Resource.AgentExitWithUndefinedReturnCode);
                            break;
                        }

                        if (Stopping)
                        {
                            ExitCode = exitCode;
                            Stop();
                        }
                        else
                        {
                            // wait for few seconds before restarting the process
                            Thread.Sleep(timeBetweenRetries);
                        }

                        lock (ServiceLock)
                        {
                            AgentListener.OutputDataReceived -= AgentListener_OutputDataReceived;
                            AgentListener.ErrorDataReceived  -= AgentListener_ErrorDataReceived;
                            AgentListener.Dispose();
                            AgentListener = null;
                            stopping      = Stopping;
                        }
                    }
                }
                catch (Exception exception)
                {
                    WriteException(exception);
                    ExitCode = 99;
                    Stop();
                }
            });
        }
Ejemplo n.º 2
0
        protected override void OnStart(string[] args)
        {
            RunningLoop = Task.Run(
                () =>
            {
                try
                {
                    bool stopping;
                    WriteInfo("Starting VSTS Agent Service");
                    TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5);

                    lock (ServiceLock)
                    {
                        stopping = Stopping;
                    }

                    while (!stopping)
                    {
                        WriteInfo("Starting VSTS Agent listener");
                        lock (ServiceLock)
                        {
                            AgentListener = CreateAgentListener();
                            AgentListener.Start();
                        }

                        AgentListener.WaitForExit();
                        int exitCode = AgentListener.ExitCode;

                        // Handle error code 1?
                        // If agent fails (because its not configured) also returns error code 1, in such case we dont want to run the service
                        // Killing a process also returns error code 1, but we want to restart the process here.
                        // TODO: change the error code for run method if agent is not configured?

                        if (exitCode == 2)
                        {
                            // Agent wants to stop the service as well
                            Stopping = true;
                            WriteInfo(Resource.ServiceRequestedToStop);
                            ExitCode = exitCode;
                            Stop();
                        }
                        else
                        {
                            // wait for few seconds before restarting the process
                            Thread.Sleep(timeBetweenRetries);
                        }
                    }

                    lock (ServiceLock)
                    {
                        AgentListener.Dispose();
                        AgentListener = null;
                    }
                }
                catch (Exception exception)
                {
                    WriteException(exception);
                    ExitCode = 1;
                    Stop();
                }
            });
        }