コード例 #1
0
 public Postgres92Processor(Postgres92Generator generator,
                            ILogger <PostgresProcessor> logger,
                            ProcessorOptions options,
                            PostgresOptions pgOptions)
     : base(generator, logger, options, pgOptions)
 {
 }
コード例 #2
0
 public PostgresQuoter(
     IOptions <QuoterOptions> quoterOptions,
     PostgresOptions options)
     : base(quoterOptions)
 {
     Options = options ?? new PostgresOptions();
 }
コード例 #3
0
        public async Task <NpgsqlConnectionFactory> CreateDatabaseAsync(
            CancellationToken cancellationToken)
        {
            // Create test database.
            var databaseSuffix = Guid.NewGuid();
            var databaseName   = $"sequence_{databaseSuffix:N}";

            string connectionString;

            using (var connection = new NpgsqlConnection(_connectionString))
            {
                await connection.OpenAsync(cancellationToken);

                await connection.ExecuteAsync($"CREATE DATABASE {databaseName};");

                connection.ChangeDatabase(databaseName);
                connectionString = connection.ConnectionString;
            }

            // Apply migrations.
            var postgresOptions = new PostgresOptions {
                ConnectionString = connectionString
            };
            var options    = Options.Create(postgresOptions);
            var logger     = NullLogger <PostgresMigrations> .Instance;
            var migrations = new PostgresMigrations(options, logger);
            await migrations.UpgradeDatabaseAsync(cancellationToken);

            return(new NpgsqlConnectionFactory(options));
        }
        public PostgresCheckpointManagerTests()
        {
            var fixture = new Fixture();

            _options = fixture.Build <PostgresOptions>()
                       .Without(o => o.Host)
                       .Without(o => o.Port)
                       .Without(o => o.Database)
                       .Without(o => o.Username)
                       .Without(o => o.Password)
                       .Without(o => o.Pooling)
                       .Without(o => o.Schema)
                       .Do(o =>
            {
                o.Host     = Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "localhost";
                o.Port     = Convert.ToInt32(Environment.GetEnvironmentVariable("POSTGRES_PORT") ?? "5432");
                o.Database = Environment.GetEnvironmentVariable("POSTGRES_DATABASE") ?? "postgres";
                o.Username = Environment.GetEnvironmentVariable("POSTGRES_USERNAME") ?? "postgres";
                o.Password = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") ?? "postgres";
                o.Pooling  = Convert.ToBoolean(Environment.GetEnvironmentVariable("POSTGRES_POOLING") ?? "False");
                o.Schema   = Environment.GetEnvironmentVariable("POSTGRES_SCHEMA") ?? "public";
            })
                       .Create();

            _connection = new PostgresConnection(_options);

            _sut = new PostgresCheckpointManager(_options, _connection);
        }
コード例 #5
0
 private static PostgresOptions CreatePostgresOptions(IPostgresOptions options)
 {
     return(new PostgresOptions((key, value) =>
     {
         var envFlag = Environment.GetEnvironmentVariable(key.ToUpperInvariant());
         return !string.IsNullOrEmpty(envFlag) ? envFlag : PostgresOptions.GetFromIOptions(options, key, value);
     }));
 }
コード例 #6
0
        public DatabaseChanges(PostgresOptions postgresOptions, string tableName)
        {
            tcs = new CancellationTokenSource();

            task = Task.Factory.StartNew(async unusedStateObject =>
            {
                NpgsqlConnection connection = null;

                while (tcs != null && !tcs.Token.IsCancellationRequested)
                {
                    var connectionString = postgresOptions.ConnectionString;
                    try
                    {
                        connection = new NpgsqlConnection(connectionString);
                        connection.Open();

                        Log.Information("Listening to {TableName}", tableName);

                        connection.Notification += (sender, args) =>
                        {
                            var changeNotification = JsonConvert.DeserializeObject <TType>(args.AdditionalInformation);
                            changes = changes.Add(changeNotification);

                            Metrics.Inc(Metrics.TotalChangesReceived);
                        };

                        // Receive notifications from the database when rows change.
                        using (var cmd = new NpgsqlCommand($"LISTEN {tableName}", connection))
                        {
                            cmd.ExecuteNonQuery();
                        }

                        while (connection.State == ConnectionState.Open)
                        {
                            await connection.WaitAsync(tcs.Token);
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        // Don't log, avoid adding confusion to logs on a graceful shutdown.
                        break;
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, "LISTEN {TableName}", tableName);
                    }
                    finally
                    {
                        connection?.Dispose();
                    }

                    await Task.Delay(TimeSpan.FromSeconds(5), tcs.Token);
                }

                Log.Information("Stopped listening to {TableName}", tableName);
            }, TaskCreationOptions.LongRunning);
        }
コード例 #7
0
 public TournamentSubscriber(
     PostgresOptions postgresOptions,
     ITeamQueryHandler teamQueryHandler)
     : base(
         new StreamConfig(Constants.InMemoryStream, Constants.TournamentNamespace))
 {
     _projectionManager = new ProjectionManager <TournamentProjection>("read", "tournament_projection", postgresOptions);
     _teamQueryHandler  = teamQueryHandler;
 }
コード例 #8
0
 public StringBuilderService(PostgresTypeMapper typeMapper, IOptions <MssqlConnectOptions> msConnectOptions,
                             IOptions <PostgresOptions> pgOptions, PgDbService pgDbService, IOptions <TableOptions> tableOptions)
 {
     _typeMapper       = typeMapper;
     _msConnectOptions = msConnectOptions.Value;
     _pgOptions        = pgOptions.Value;
     _pgDbService      = pgDbService;
     _tableOptions     = tableOptions.Value;
 }
コード例 #9
0
 public TeamSubscriber(
     PostgresOptions postgresOptions,
     ITournamentQueryHandler tournamentQueryHandler)
     : base(
         new StreamConfig(Constants.InMemoryStream, Constants.TeamNamespace))
 {
     _tournamentQueryHandler = tournamentQueryHandler;
     _projectionManager      = new ProjectionManager <TeamProjection>("read", "team_projection", postgresOptions);
 }
コード例 #10
0
        public PostgresCheckpointManager(PostgresOptions options, IPostgresConnection connection)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            _scripts = new Scripts.Scripts(_options.Schema);

            _createConnection = connection.Build;

            CreateSchemaIfNotExists();
        }
コード例 #11
0
 public Postgres92Processor(
     [NotNull] PostgresDbFactory factory,
     [NotNull] Postgres92Generator generator,
     [NotNull] ILogger <PostgresProcessor> logger,
     [NotNull] IOptionsSnapshot <ProcessorOptions> options,
     [NotNull] IConnectionStringAccessor connectionStringAccessor,
     [NotNull] PostgresOptions pgOptions)
     : base(factory, generator, logger, options, connectionStringAccessor, pgOptions)
 {
 }
コード例 #12
0
    public ProjectionManager(
        string schemaName,
        string tableName,
        PostgresOptions postgresOptions)
    {
        _postgresOptions = postgresOptions;

        _getQuery      = $"SELECT payload FROM {schemaName}.{tableName} WHERE id = @id";
        _getAllQuery   = $"SELECT payload FROM {schemaName}.{tableName}";
        _updateCommand = $"INSERT INTO {schemaName}.{tableName} (id, payload) VALUES (@id, @payload::jsonb) ON CONFLICT(id) DO UPDATE SET payload = excluded.payload;";
    }
コード例 #13
0
 /// <summary>
 /// Add common Postgres services.
 /// </summary>
 /// <param name="builder">The builder to add the Postgres-specific services to</param>
 /// <returns>The migration runner builder</returns>
 private static IMigrationRunnerBuilder AddCommonPostgresServices(this IMigrationRunnerBuilder builder)
 {
     builder.Services
     .AddScoped(
         sp =>
     {
         var processorOptions = sp.GetRequiredService <IOptionsSnapshot <ProcessorOptions> >();
         return(PostgresOptions.ParseProviderSwitches(processorOptions.Value.ProviderSwitches));
     })
     .AddScoped <PostgresDbFactory>()
     .AddScoped <PostgresQuoter>();
     return(builder);
 }
        public static IServiceCollection AddPostgresCheckpointManagement(this IServiceCollection services,
                                                                         Action <PostgresOptions>?options = null)
        {
            var postgresOptions = new PostgresOptions();

            options?.Invoke(postgresOptions);

            // ReSharper disable once RedundantAssignment
            services.AddPostgresData(options)
            .TryAddSingleton <ICheckpointManager>(sp =>
                                                  new PostgresCheckpointManager(postgresOptions, sp.GetRequiredService <IPostgresConnection>()));

            return(services);
        }
コード例 #15
0
        private static PostgresOptions.GetStringDelegate GetPostgresFlags(IOptions options, WorkerConnection connection)
        {
            return((key, value) =>
            {
                if (options.PostgresFromWorkerFlags)
                {
                    var flagValue = connection.GetWorkerFlag(key);

                    if (!string.IsNullOrEmpty(flagValue))
                    {
                        return flagValue;
                    }
                }

                var envFlag = Environment.GetEnvironmentVariable(key.ToUpperInvariant());
                return !string.IsNullOrEmpty(envFlag) ? envFlag : PostgresOptions.GetFromIOptions(options, key, value);
            });
        }
コード例 #16
0
        public ExtendedPostgresProcessor(IDbMigrationConfig dbMigrationConfig,
                                         PostgresQuoter quoter,
                                         PostgresDbFactory factory,
                                         PostgresGenerator generator,
                                         ILogger <ExtendedPostgresProcessor> logger,
                                         IOptionsSnapshot <ProcessorOptions> options,
                                         IOptionsSnapshot <RunnerOptions> runnerOptions,
                                         IConnectionStringAccessor connectionStringAccessor,
                                         IExtendedMigrationGenerator <ExtendedPostgresGenerator> extendedGenerator,
                                         PostgresOptions pgOptions)
            : base(factory, generator, logger, options, connectionStringAccessor, pgOptions)
        {
            ExtendedGeneratorField = extendedGenerator;
            DbMigrationConfig      = dbMigrationConfig;
            RunnerOptions          = runnerOptions;

            ConnectionStringFunc = () => connectionStringAccessor.ConnectionString;

            if (dbMigrationConfig.ProcessorId == ProcessorIds.PostgresProcessorId && !NoConnection)
            {
                var stopWatch = new StopWatch();
                PostgresDatabaseCreator.CreateDatabase(
                    dbMigrationConfig,
                    () =>
                {
                    Logger.LogSay($"Creating Postgres database '{dbMigrationConfig.DatabaseName.ToLower()}'...");
                    stopWatch.Start();
                },
                    sql =>
                {
                    stopWatch.Stop();
                    Logger.LogSql(sql);
                    Logger.LogSay($"Created Postgres database '{dbMigrationConfig.DatabaseName.ToLower()}'...");
                    Logger.LogElapsedTime(stopWatch.ElapsedTime());
                });
            }

            this.SetPrivateFieldValue <PostgresProcessor>("_quoter", quoter);
            Initialize();
        }
コード例 #17
0
 public TournamentQueryHandler(PostgresOptions postgresOptions)
 {
     _projectionManager = new ProjectionManager <TournamentProjection>("read", "tournament_projection", postgresOptions);
 }
コード例 #18
0
 public PostgresQuoter(PostgresOptions options)
 {
     Options = options ?? new PostgresOptions();
 }
コード例 #19
0
        private static async Task RunAsync(IOptions options)
        {
            if (string.IsNullOrEmpty(options.LogFileName))
            {
                options.LogFileName = Path.Combine(Environment.CurrentDirectory, options.WorkerName + ".log");
            }

            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .MinimumLevel.Debug()
                         .WriteTo.Console()
                         .WriteTo.File(options.LogFileName)
                         .CreateLogger();

            Log.Debug($"Opened logfile {options.LogFileName}");

            var connectionParameters = new ConnectionParameters
            {
                EnableProtocolLoggingAtStartup = true,
                ProtocolLogging = new ProtocolLoggingParameters
                {
                    LogPrefix = Path.ChangeExtension(options.LogFileName, string.Empty) + "-protocol"
                },
                WorkerType             = WorkerType,
                DefaultComponentVtable = new ComponentVtable()
            };


            using (var connection = await WorkerConnection.ConnectAsync(options, connectionParameters))
            {
                var postgresOptions             = new PostgresOptions(GetPostgresFlags(options, connection));
                DatabaseSyncLogic databaseLogic = null;

                var tableName = connection.GetWorkerFlag("postgres_tablename") ?? "postgres";

                var databaseService = Task.Run(async() =>
                {
                    using (var response = await connection.SendEntityQueryRequest(new EntityQuery {
                        Constraint = new ComponentConstraint(DatabaseSyncService.ComponentId), ResultType = new SnapshotResultType()
                    }))
                    {
                        if (response.ResultCount == 0)
                        {
                            throw new ServiceNotFoundException(nameof(DatabaseSyncService));
                        }

                        databaseLogic = new DatabaseSyncLogic(postgresOptions, tableName, connection, response.Results.First().Key, DatabaseSyncService.CreateFromSnapshot(response.Results.First().Value));
                        connection.StartSendingMetrics(databaseLogic.UpdateMetrics);
                    }
                });

                using (var databaseChanges = new DatabaseChanges <DatabaseSyncItem.DatabaseChangeNotification>(postgresOptions, tableName))
                {
                    foreach (var opList in connection.GetOpLists(TimeSpan.FromMilliseconds(16)))
                    {
                        var changes = databaseChanges.GetChanges();

                        if (!changes.IsEmpty)
                        {
                            databaseLogic?.ProcessDatabaseSyncChanges(changes);
                        }

                        ProcessOpList(opList);

                        if (options.PostgresFromWorkerFlags)
                        {
                            postgresOptions.ProcessOpList(opList);
                        }

                        connection.ProcessOpList(opList);
                        databaseLogic?.ProcessOpList(opList);

                        // Propagate exceptions.
                        databaseService.Wait(TimeSpan.FromTicks(1));
                    }
                }
            }

            Log.Information("Disconnected from SpatialOS");
        }
コード例 #20
0
ファイル: UserRepository.cs プロジェクト: limin9988/Entropy
 public UserRepository(PostgresOptions options)
 {
     _options = options;
 }
コード例 #21
0
 public ServiceDbContext(DbContextOptions <ServiceDbContext> options,
                         IOptions <PostgresOptions> settings)
     : base(options)
 {
     _settings = settings.Value;
 }
コード例 #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CustomPostgresQuoter"/> class.
 /// </summary>
 /// <param name="options">The Postgres specific options.</param>
 public CustomPostgresQuoter(PostgresOptions options)
     : base(options)
 {
 }
コード例 #23
0
 public PgDbService(IOptions <PostgresOptions> postgresOptions)
 {
     _postgresOptions = postgresOptions.Value;
 }
コード例 #24
0
 public OverriddenPostgresQouter(PostgresOptions options) : base(options)
 {
 }
コード例 #25
0
 public BookingMonthRepository(PostgresOptions options)
 {
     _options = options;
 }
コード例 #26
0
ファイル: PostgresDb.cs プロジェクト: webtrad3r/AliseeksApi
 public PostgresDb(IOptions <PostgresOptions> config, IRavenClient raven)
 {
     this.config = config.Value;
     this.raven  = raven;
 }
コード例 #27
0
 public TeamQueryHandler(PostgresOptions postgresOptions)
 {
     _projectionManager = new ProjectionManager <TeamProjection>("read", "team_projection", postgresOptions);
 }
コード例 #28
0
 public PostgresQuoter(PostgresOptions options)
     : this(new OptionsWrapper <QuoterOptions>(new QuoterOptions()), options)
 {
 }