Ejemplo n.º 1
0
        public static void Entry <TStartup, TConfig>()
            where TStartup : class
            where TConfig : class, IDocumentsWebConfiguration, new()
        {
            ProcessEntry.Entry();

            var config = new TConfig();

            Configuration <TConfig> .Load(config.SectionName, instance : config);


            ThreadPool.SetMinThreads(50, 50);
            var host = new WebHostBuilder()
                       .UseKestrel(options => {
                options.Limits.MaxRequestBodySize               = 250 * 1024 * 1024;
                options.Limits.MinResponseDataRate              = null;
                options.Limits.MaxConcurrentConnections         = null;
                options.Limits.MaxConcurrentUpgradedConnections = null;
            })
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .ConfigureServices((services) =>
            {
                services.AddSingleton(config);
            })
                       .UseStartup <TStartup>()
                       .UseUrls(config.HostingURL)
                       .Build();

            host.Run();
        }
Ejemplo n.º 2
0
        public async static Task <int> StartAsync(params Task[] tasks)
        {
            ProcessEntry.Entry();
            await Task.WhenAny(tasks);

            return(0);
        }
Ejemplo n.º 3
0
        public static Task <int> StartAsync(Func <IEnumerable <LaunchProfile> > getLaunches)
        {
            ProcessEntry.Entry();
            ServiceRunner <MicroService> .Run(config =>
            {
                var name = Assembly.GetEntryAssembly().GetName().Name;
                config.SetName(name);
                config.SetDisplayName(name);
                config.SetDescription(name);
                var assemblyPath = Path.GetDirectoryName(
                    Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));

                Configuration <TaskConfiguration> .Load("Documents", basePath: assemblyPath);
                var logger = Logging.CreateLogger(typeof(MicroService));
                config.Service(serviceConfig =>
                {
                    serviceConfig.ServiceFactory((extraArguments, controller) =>
                    {
                        return(new MicroService(
                                   logger: logger,
                                   entry: async cancel =>
                        {
                            logger.LogInformation("Starting QueuedApplication StartAsync:Entry");
                            var tasks = new List <Task>();
                            var applications = new List <QueuedApplication>();

                            var launches = getLaunches();

                            while (!cancel.IsCancellationRequested)
                            {
                                if (applications.Any())
                                {
                                    foreach (var application in applications)
                                    {
                                        try
                                        {
                                            ((IDisposable)application).Dispose();
                                        }
                                        catch (Exception) { }
                                    }
                                    applications.Clear();
                                }

                                foreach (var launch in launches)
                                {
                                    if (launch.FixedInstanceCount == 0)
                                    {
                                        continue;
                                    }

                                    // here's the first one
                                    var app = Activator.CreateInstance(launch.ApplicationType) as QueuedApplication;
                                    applications.Add(app);
                                    var configuration = Configuration <TaskConfiguration> .Load(app.ConfigurationSectionName).Object;

                                    // loop starts at second instance
                                    for (int i = 1; i < (launch.FixedInstanceCount ?? configuration.ConcurrentInstances); i++)
                                    {
                                        applications.Add(Activator.CreateInstance(launch.ApplicationType) as QueuedApplication);
                                    }
                                }

                                logger.LogInformation("launches.count:" + launches.Count());
                                logger.LogInformation("applications.count:" + applications.Count());

                                await Task.WhenAny(
                                    applications.Select(a =>
                                {
                                    //Console.WriteLine($"Starting QueuedApplication {a.QueueName}");
                                    try
                                    {
                                        return a.RunAsync(cancel);
                                    }
                                    catch (Exception e)
                                    {
                                        logger.LogError(e, "Exception running application: " + e);
                                        throw;
                                    }
                                }).ToArray()
                                    );
                            }
                        }));
                    });

                    serviceConfig.OnStart((service, extraParams) =>
                    {
                        logger.LogInformation("Service {0} started", name);
                        ((IMicroService)service).Start();
                    });

                    serviceConfig.OnStop(service =>
                    {
                        logger.LogInformation("Service {0} stopped", name);
                        ((IMicroService)service).Stop();
                    });

                    serviceConfig.OnError(e =>
                    {
                        logger.LogError("Service {0} errored with exception : {1}\n{2}", name, e.Message, e.ToString());
                    });
                });
            });

            return(Task.FromResult(0));
        }