private void CtrlCHandler(object sender, EventArgs e)
        {
            _term.WriteLine("Exiting...");
            if (_inConfigStage)
            {
                HostContext.Dispose();
                Environment.Exit(Constants.Agent.ReturnCode.TerminatedError);
            }
            else
            {
                ConsoleCancelEventArgs cancelEvent = e as ConsoleCancelEventArgs;
                if (cancelEvent != null && HostContext.GetService <IConfigurationStore>().IsServiceConfigured())
                {
                    ShutdownReason reason;
                    if (cancelEvent.SpecialKey == ConsoleSpecialKey.ControlBreak)
                    {
                        Trace.Info("Received Ctrl-Break signal from agent service host, this indicate the operating system is shutting down.");
                        reason = ShutdownReason.OperatingSystemShutdown;
                    }
                    else
                    {
                        Trace.Info("Received Ctrl-C signal, stop agent.listener and agent.worker.");
                        reason = ShutdownReason.UserCancelled;
                    }

                    HostContext.ShutdownAgent(reason);
                }
                else
                {
                    HostContext.ShutdownAgent(ShutdownReason.UserCancelled);
                }
            }
        }
Exemple #2
0
        public static async Task <int> MainAsync(
            string[] args)
        {
            //ITerminal registers a CTRL-C handler, which keeps the Agent.Worker process running
            //and lets the Agent.Listener handle gracefully the exit.
            using (var hc = new HostContext("Worker"))
                using (var term = hc.GetService <ITerminal>())
                {
                    Tracing trace = hc.GetTrace(nameof(Program));
                    try
                    {
                        trace.Info($"Version: {Constants.Agent.Version}");
                        trace.Info($"Commit: {BuildConstants.Source.CommitHash}");
                        trace.Info($"Culture: {CultureInfo.CurrentCulture.Name}");
                        trace.Info($"UI Culture: {CultureInfo.CurrentUICulture.Name}");

                        // Validate args.
                        ArgUtil.NotNull(args, nameof(args));
                        ArgUtil.Equal(3, args.Length, nameof(args.Length));
                        ArgUtil.NotNullOrEmpty(args[0], $"{nameof(args)}[0]");
                        ArgUtil.Equal("spawnclient", args[0].ToLowerInvariant(), $"{nameof(args)}[0]");
                        ArgUtil.NotNullOrEmpty(args[1], $"{nameof(args)}[1]");
                        ArgUtil.NotNullOrEmpty(args[2], $"{nameof(args)}[2]");
                        var worker = hc.GetService <IWorker>();

                        // Run the worker.
                        return(await worker.RunAsync(
                                   pipeIn : args[1],
                                   pipeOut : args[2]));
                    }
                    catch (Exception ex)
                    {
                        // Populate any exception that cause worker failure back to agent.
                        Console.WriteLine(ex.ToString());
                        try
                        {
                            trace.Error(ex);
                        }
                        catch (Exception e)
                        {
                            // make sure we don't crash the app on trace error.
                            // since IOException will throw when we run out of disk space.
                            Console.WriteLine(e.ToString());
                        }
                    }
                    finally
                    {
                        hc.Dispose();
                    }

                    return(1);
                }
        }
Exemple #3
0
 private void CtrlCHandler(object sender, EventArgs e)
 {
     _term.WriteLine("Exiting...");
     if (_inConfigStage)
     {
         HostContext.Dispose();
         Environment.Exit(Constants.Agent.ReturnCode.TerminatedError);
     }
     else
     {
         TokenSource.Cancel();
     }
 }
Exemple #4
0
 private void Quit()
 {
     _term.WriteLine("Exiting...");
     if (_inConfigStage)
     {
         HostContext.Dispose();
         Environment.Exit(1);
     }
     else
     {
         TokenSource.Cancel();
     }
 }
Exemple #5
0
 public void Dispose()
 {
     if (!disposed)
     {
         disposed = true;
         cancellationTokenSource.Cancel();
         cancellationTokenSource.Dispose();
         listenerSocket.Dispose();
         hostContext.Dispose();
         foreach (var session in sessions.ToArray())
         {
             session.Dispose();
         }
     }
 }
Exemple #6
0
        public static async Task <int> MainAsync(
            string[] args)
        {
            //ITerminal registers a CTRL-C handler, which keeps the Agent.Worker process running
            //and lets the Agent.Listener handle gracefully the exit.
            using (var hc = new HostContext("Worker"))
                using (var term = hc.GetService <ITerminal>())
                {
                    Tracing trace = hc.GetTrace(nameof(Program));
                    try
                    {
                        trace.Info($"Version: {Constants.Agent.Version}");
                        trace.Info($"Commit: {BuildConstants.Source.CommitHash}");

                        // Validate args.
                        ArgUtil.NotNull(args, nameof(args));
                        ArgUtil.Equal(3, args.Length, nameof(args.Length));
                        ArgUtil.NotNullOrEmpty(args[0], $"{nameof(args)}[0]");
                        ArgUtil.Equal("spawnclient", args[0].ToLowerInvariant(), $"{nameof(args)}[0]");
                        ArgUtil.NotNullOrEmpty(args[1], $"{nameof(args)}[1]");
                        ArgUtil.NotNullOrEmpty(args[2], $"{nameof(args)}[2]");
                        var worker = hc.GetService <IWorker>();

                        // Run the worker.
                        return(await worker.RunAsync(
                                   pipeIn : args[1],
                                   pipeOut : args[2]));
                    }
                    catch (Exception ex)
                    {
                        trace.Error(ex);
                    }
                    finally
                    {
                        hc.Dispose();
                    }

                    return(1);
                }
        }
 public void Teardown()
 {
     _hc?.Dispose();
     _tokenSource?.Dispose();
 }
Exemple #8
0
 private void Teardown()
 {
     _hc?.Dispose();
     _tokenSource?.Dispose();
 }