private async Task <int> Run(HostCommandContext context,
                                     string pipeName,
                                     long maxMemory,
                                     int affinityMask,
                                     int maxCpu)
        {
            var currentProcess = SDProcess.GetCurrentProcess();

            var logger = context.ServiceProvider.GetService <ILoggerFactory>().CreateLogger <HostProcessImpl>();

            logger.HostProcessStart(currentProcess.Id);

            string name = "pihost." + currentProcess.Id;

            var limits = new IsolationLimits(maxMemory, maxCpu, affinityMask);

            if (limits.IsAnyEnabled)
            {
                s_jobObject = new JobObject(name);
                s_jobObject.AddProcess(currentProcess);
                s_jobObject.Enable(limits, (l, v) => logger.ProcessLimitSet(l, v));
            }

            var host = new IpcServiceHostBuilder(context.ServiceProvider)
                       .AddNamedPipeEndpoint <IHostProcess>(name, pipeName, includeFailureDetailsInResponse: true)
                       .Build();

            logger.IpcServiceHostStarting(pipeName);
            context.SetStartComplete();
            await host.RunAsync(context.Token).ConfigureAwait(false);

            return(0);
        }
Exemple #2
0
        public static int Main(string[] args)
        {
            if (Console.IsOutputRedirected &&
                Console.IsErrorRedirected)
            {
                Console.OutputEncoding = Encoding.UTF8;
            }

            var application = new CommandLineApplication
            {
                Name               = "pihost",
                FullName           = "pihost",
                Description        = "ProcessIsolation generic hosting process",
                ShortVersionGetter = () => FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location).ProductVersion,
                LongVersionGetter  = () => FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location).ProductVersion
            };

            var serviceProvider = ConfigureServices();
            var context         = new HostCommandContext(serviceProvider, s_startComplete, s_tokenSource);

            new RootCommand().Configure(application, context);

            return(application.Execute(args));
        }