Ejemplo n.º 1
0
 public ThrotleSettings(Pandora pandora)
 {
     Server = pandora.Get("pushnot_rabbitmq_server");
     Port = pandora.Get<int>("pushnot_rabbitmq_port");
     Username = pandora.Get("pushnot_rabbitmq_username");
     Password = pandora.Get("pushnot_rabbitmq_password");
     VirtualHost = pandora.Get("pushnot_rabbitmq_virtual_host");
     PushNotificationsBatchSize = pandora.Get<int>("pushnot_batch_size");
     PushNotificationsSendoutDelay = pandora.Get<int>("pushnot_sendout_delay");
     PushNotificationsMaxCPUUtilization = pandora.Get<int>("pushnot_max_cpu_utilization");
     EndpointNameConvention = new ThrottledBrokerEndpointNameConvention(typeof(APNSNotificationMessage), typeof(GCMNotificationMessage));
     PipelineNameConvention = new ThrottledBrokerPipelineNameConvention();
 }
        public void Initialize(HttpConfiguration configuration, Pandora pandora)
        {
            var container = new Container();
            container.RegisterSingleton<Pandora>(() => pandora);
            Func<IPipelineTransport> transport = () => container.Resolve<IPipelineTransport>();
            Func<ISerializer> serializer = () => container.Resolve<ISerializer>();
            container.RegisterSingleton<IPublisher<ICommand>>(() => new PipelinePublisher<ICommand>(transport(), serializer()));

            var serviceLocator = new ServiceLocator(container);
            ControllerFactory = new ServiceLocatorFactory(serviceLocator);

            var cfg = new CronusSettings(container)
                  .UseCluster(cluster =>
                       cluster.UseAggregateRootAtomicAction(atomic =>
                       {
                           if (pandora.Get<bool>("enable_redis_atomic_action"))
                               atomic.UseRedis(redis => redis.SetLockEndPoints(pandora.Get("redis_endpoints").ParseIPEndPoints(";")));
                           else
                               atomic.WithInMemory();
                       }))
                  .UseContractsFromAssemblies(new[] { Assembly.GetAssembly(typeof(PushNotificationWasSent)) })
                  .UseRabbitMqTransport(x =>
                  {
                      x.Server = pandora.Get("rabbitmq_server");
                      x.Username = pandora.Get("rabbitmq_username");
                      x.Password = pandora.Get("rabbitmq_password");
                      x.VirtualHost = pandora.Get("rabbitmq_virtualhost");
                  });

            (cfg as ICronusSettings).Build();
        }
Ejemplo n.º 3
0
        public static void Start()
        {
            try
            {
                log.Info("Starting Cronus Push Notifications");

                var appContext = new ApplicationContext("PushNotifications");
                var cfgRepo = new ConsulForPandora(new Uri("http://consul.local.com:8500"));
                var pandora = new Pandora(appContext, cfgRepo);

                container = new Container();

                new CronusSettings(container)
                     .UseCluster(cluster =>
                        cluster.UseAggregateRootAtomicAction(atomic =>
                        {
                            if (pandora.Get<bool>("enable_redis_atomic_action"))
                                atomic.UseRedis(redis => redis.SetLockEndPoints(pandora.Get("redis_endpoints").ParseIPEndPoints(";")));
                            else
                                atomic.WithInMemory();
                        }))
                     .UseAppServices(pandora)
                     .UseProjections(pandora)
                     .UsePorts(pandora)
                     .Build();

                host = container.Resolve<CronusHost>();
                host.Start();
                log.Info("STARTED Cronus Push Notifications");
            }
            catch (Exception ex)
            {
                log.ErrorException("Unable to boot PushNotifications.WS", ex);
                throw;
            }
        }
Ejemplo n.º 4
0
        public static void UseHttpWebApi(this IAppBuilder app, Pandora pandora)
        {
            try
            {
                log.Info("Starting Cronus Push Notifications Api");

                JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

                var apiConfig = new HttpConfiguration();
                apiConfig.MapHttpAttributeRoutes();
                GlobalConfigureApi(apiConfig);

                var configurations = typeof(Cronus).Assembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IApiConfiguration)))
                     .Select(x => Activator.CreateInstance(x) as IApiConfiguration).ToList();
                foreach (var item in configurations)
                {
                    try
                    {
                        item.Initialize(apiConfig, pandora);
                    }
                    catch (Exception ex)
                    {
                        log.FatalException($"Failed to initialize:{item.GetType().Name}", ex);
                        throw;
                    }
                }

                apiConfig.Services.Replace(typeof(IAssembliesResolver), new ControllerAssembliesResolver(configurations.Select(x => x.Assembly).ToList()));
                apiConfig.Services.Replace(typeof(IHttpControllerActivator), new CompositeLocatorFactory(configurations.ToList()));

                app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
                {
                    Authority = pandora.Get("idsrv_authority")
                });

                app.UseWebApi(apiConfig);

                log.Info("Started Cronus Push Notifications Api");
            }
            catch (Exception ex)
            {
                log.ErrorException("Unable to boot PushNotifications.Api", ex);
                throw;
            }
        }
Ejemplo n.º 5
0
        private static ICronusSettings UseProjections(this ICronusSettings cronusSettings, Pandora pandora)
        {
            var projectionFactory = new ServiceLocator(cronusSettings.Container);

            var projectionSession = SessionFactory.Create(pandora.Get("pushnot_conn_str_projections"));
            projectionSession.InitializeProjectionDatabase(Assembly.GetAssembly(typeof(APNSSubscriptionsProjection)));
            var persister = new CassandraPersister(projectionSession);
            Func<ISerializer> serializer = () => container.Resolve<ISerializer>();
            Func<Repository> repo = () => new Repository(persister, obj => serializer().SerializeToBytes(obj), data => serializer().DeserializeFromBytes(data));
            container.RegisterTransient<IProjectionRepository>(() => new CassandraProjectionRepository(new ProjectionBuilder(), repo));
            container.RegisterTransient<IRepository>(() => new Repository(
                new CassandraPersister(container.Resolve<Cassandra.ISession>()),
                container.Resolve<ISerializer>().SerializeToBytes,
                container.Resolve<ISerializer>().DeserializeFromBytes));

            cronusSettings.UseProjectionConsumer(consumable => consumable
                .WithDefaultPublishers()
                .UseRabbitMqTransport(x =>
                {
                    x.Server = pandora.Get("rabbitmq_server");
                    x.Port = pandora.Get<int>("rabbitmq_port");
                    x.Username = pandora.Get("rabbitmq_username");
                    x.Password = pandora.Get("rabbitmq_password");
                    x.VirtualHost = pandora.Get("rabbitmq_virtualhost");
                })
                .UseProjections(h => h.RegisterHandlersInAssembly(new[] { Assembly.GetAssembly(typeof(APNSSubscriptionsProjection)) }, projectionFactory.Resolve)));

            return cronusSettings;
        }
Ejemplo n.º 6
0
        private static ICronusSettings UsePorts(this ICronusSettings cronusSettings, Pandora pandora)
        {
            var portFactory = new ServiceLocator(cronusSettings.Container);

            var broker = ConfigurePushBroker(pandora);
            var throttleSettings = new ThrotleSettings(pandora);
            Func<ISerializer> serializer = () => container.Resolve<ISerializer>();
            var throttler = new ThrottledBrokerAdapter(new ThrottledBroker(cronusSettings.Container, broker, throttleSettings));
            container.RegisterSingleton<IPushBroker>(() => throttler);

            cronusSettings.UsePortConsumer(consumable => consumable
                .WithDefaultPublishers()
                .UseRabbitMqTransport(x =>
                {
                    x.Server = pandora.Get("rabbitmq_server");
                    x.Port = pandora.Get<int>("rabbitmq_port");
                    x.Username = pandora.Get("rabbitmq_username");
                    x.Password = pandora.Get("rabbitmq_password");
                    x.VirtualHost = pandora.Get("rabbitmq_virtualhost");
                })
                .UsePorts(handler => handler.RegisterHandlersInAssembly(new[] { Assembly.GetAssembly(typeof(APNSPort)) }, portFactory.Resolve)));

            return cronusSettings;
        }
Ejemplo n.º 7
0
        private static ICronusSettings UseAppServices(this ICronusSettings cronusSettings, Pandora pandora)
        {
            var appServiceFactory = new ServiceLocator(cronusSettings.Container);

            cronusSettings.UseContractsFromAssemblies(new[] {
                    Assembly.GetAssembly(typeof(PushNotificationWasSent)),
                    Assembly.GetAssembly(typeof(APNSSubscriptionsProjection)),
                    Assembly.GetAssembly(typeof(APNSNotificationMessage))
                })
            .UseCommandConsumer(consumer => consumer
                .UseRabbitMqTransport(x =>
                {
                    x.Server = pandora.Get("rabbitmq_server");
                    x.Username = pandora.Get("rabbitmq_username");
                    x.Password = pandora.Get("rabbitmq_password");
                    x.VirtualHost = pandora.Get("rabbitmq_virtualhost");
                })
                .WithDefaultPublishers()
                .UseCassandraEventStore(eventStore => eventStore
                    .SetConnectionString(pandora.Get("pushnot_conn_str_es"))
                    .SetAggregateStatesAssembly(typeof(PushNotificationState))
                    .WithNewStorageIfNotExists())
                .UseApplicationServices(cmdHandler => cmdHandler.RegisterHandlersInAssembly(new[] { typeof(PushNotificationAppService).Assembly }, appServiceFactory.Resolve)));

            return cronusSettings;
        }
Ejemplo n.º 8
0
        private static PushBroker ConfigurePushBroker(Pandora pandora)
        {
            var broker = new PushBroker();
            broker.OnNotificationSent += PushNotificationLogger.NotificationSent;
            broker.OnChannelException += PushNotificationLogger.ChannelException;
            broker.OnServiceException += PushNotificationLogger.ServiceException;
            broker.OnNotificationFailed += PushNotificationLogger.NotificationFailed;
            broker.OnDeviceSubscriptionExpired += PushNotificationLogger.DeviceSubscriptionExpired;
            broker.OnDeviceSubscriptionChanged += PushNotificationLogger.DeviceSubscriptionChanged;
            broker.OnChannelCreated += PushNotificationLogger.ChannelCreated;
            broker.OnChannelDestroyed += PushNotificationLogger.ChannelDestroyed;

            var iosCert = pandora.Get("pushnot_ios_cert");
            var iosCertPass = pandora.Get("pushnot_ios_cert_pass");

            var androidToken = pandora.Get("pushnot_android_token");

            var parseAppId = pandora.Get("pushnot_parse_app_id");
            var parseRestApiKey = pandora.Get("pushnot_parse_rest_api_key");

            string iosCertPath = Environment.ExpandEnvironmentVariables(iosCert);

            var appleCert = File.ReadAllBytes(iosCertPath);

            bool iSprod = Boolean.Parse(pandora.Get("pushnot_ios_production"));
            broker.RegisterAppleService(new ApplePushChannelSettings(iSprod, appleCert, iosCertPass, disableCertificateCheck: true));
            broker.RegisterGcmService(new GcmPushChannelSettings(androidToken));

            return broker;
        }