public CrawlerStrategySimpleFactory(
     EazeCrawlerConfiguration configuration,
     Func <CrawlRequestType, HttpMessageHandler> handlerFactory,
     ILogger <ICrawlerStrategy> logger)
 {
     _configuration  = configuration;
     _handlerFactory = handlerFactory;
     _logger         = logger;
 }
示例#2
0
        private static IHost CreateHost()
        {
            var hostBuilder = new HostBuilder()
                              .UseContentRoot(Directory.GetCurrentDirectory())
                              .UseEnvironment(Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT") ?? "Development")
                              .ConfigureAppConfiguration((ctx, builder) => {
                builder
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();
            })
                              .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                              .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                                          .ReadFrom.Configuration(hostingContext.Configuration))
                              .UseOrleans <OrleansService>(ConfigureSilo)
                              .UseHangfire <HangfireService>(ConfigureHangfire)
                              .ConfigureContainer <ContainerBuilder>((ctx, builder) =>
            {
                // silo cancellation source / token
                // builder.RegisterInstance(new CancellationTokenSource())
                //     .ExternallyOwned();
                // builder.Register((c) => c.Resolve<CancellationTokenSource>().Token)
                //     .As<CancellationToken>()
                //     .ExternallyOwned();

                var config = new EazeCrawlerConfiguration();
                ctx.Configuration.BindJson("configuration", config);
                builder.RegisterInstance(config)
                .As <EazeCrawlerConfiguration>();

                builder
                .Register(c =>
                {
                    var hyperlinkConfig = c.Resolve <EazeCrawlerConfiguration>()
                                          .GetCrawlerSettings <HyperlinkCrawlerSettings>();
                    var handler = new HttpClientHandler
                    {
                        AllowAutoRedirect       = hyperlinkConfig.AllowAutoRedirect,
                        MaxConnectionsPerServer = hyperlinkConfig.MaxConnectionsPerServer
                    };

                    return(handler);
                })
                .Keyed <HttpMessageHandler>(CrawlRequestType.Hyperlink);

                builder
                .Register <Func <CrawlRequestType, HttpMessageHandler> >(
                    (c, p) =>
                {
                    var context = c.Resolve <IComponentContext>();
                    return(requestType =>
                           context.ResolveKeyed <HttpMessageHandler>(requestType));
                });

                // Simple factory for instantiating crawler strategies.
                builder.RegisterType <CrawlerStrategySimpleFactory>()
                .As <ICrawlerStrategyFactory>()
                .SingleInstance();

                // Orleans registrations
                builder.Register(c =>
                {
                    IPAddress ip;
                    try
                    {
                        var hostEntry = Dns.GetHostEntry("silo");
                        ip            = hostEntry.AddressList[0];
                    }
                    catch
                    {
                        ip = IPAddress.Loopback;
                    }

                    return(new ClientBuilder()
                           .UseStaticClustering(new IPEndPoint(ip, EndpointOptions.DEFAULT_GATEWAY_PORT))
                           .Configure <ClusterOptions>(o =>
                    {
                        o.ClusterId = "eaze";
                        o.ServiceId = "eaze";
                    })
                           .Configure <SerializationProviderOptions>(o =>
                    {
                        //o.SerializationProviders.Add(typeof(OrleansJsonSerializer).GetTypeInfo());
                    })
                           .ConfigureApplicationParts(parts =>
                    {
                        parts.AddApplicationPart(typeof(IManageCrawlRequests).Assembly);
                        parts.AddApplicationPart(typeof(IProcessCrawlRequests).Assembly);
                        parts.AddApplicationPart(typeof(CrawlRequestBase).Assembly);
                    })
                           .Build());
                })
                .As <IClusterClient>()
                .SingleInstance();

                builder.RegisterType <CrawlJobPerformer>()
                .InstancePerBackgroundJob();

                // Hangfire final configuration
                builder.RegisterBuildCallback(container =>
                {
                    GlobalConfiguration.Configuration.UseAutofacActivator(container);
                });
            });

            return(hostBuilder.UseConsoleLifetime().Build());
        }