public async Task <T> Query <T>(ISingleQueryHandler <T> handler, CancellationToken cancellation) { using var conn = _database.CreateConnection(); var command = handler.BuildCommand(); command.Connection = conn; await conn.OpenAsync(cancellation).ConfigureAwait(false); using var reader = await command.ExecuteReaderAsync(cancellation).ConfigureAwait(false); try { return(await handler.HandleAsync(reader, cancellation).ConfigureAwait(false)); } finally { #if NET6_0_OR_GREATER await reader.CloseAsync().ConfigureAwait(false); #else #pragma warning disable VSTHRD103 reader.Close(); #pragma warning restore VSTHRD103 #endif await conn.CloseAsync().ConfigureAwait(false); } }
public async Task <T> Query <T>(ISingleQueryHandler <T> handler, CancellationToken cancellation) { using var conn = _database.CreateConnection(); var command = handler.BuildCommand(); command.Connection = conn; await conn.OpenAsync(cancellation).ConfigureAwait(false); using var reader = await command.ExecuteReaderAsync(cancellation).ConfigureAwait(false); return(await handler.HandleAsync(reader, cancellation).ConfigureAwait(false)); }
public async Task SetFloor(long floor) { var numberOfPages = (long)Math.Ceiling((double)floor / MaxLo); var updateSql = $"update {_options.DatabaseSchemaName}.mt_hilo set hi_value = :floor where entity_name = :name"; // This guarantees that the hilo row exists await AdvanceToNextHi().ConfigureAwait(false); using var conn = _database.CreateConnection(); await conn.OpenAsync().ConfigureAwait(false); await conn.CreateCommand(updateSql) .With("floor", numberOfPages) .With("name", _entityName) .ExecuteNonQueryAsync().ConfigureAwait(false); // And again to get it where we need it to be await AdvanceToNextHi().ConfigureAwait(false); }
private async Task <bool> tryToAttainLockAndStartShards() { bool gotLock = false; NpgsqlConnection conn = null; try { conn = _database.CreateConnection(); await conn.OpenAsync(_cancellation.Token).ConfigureAwait(false); gotLock = (bool)await conn.CreateCommand("SELECT pg_try_advisory_lock(:id);") .With("id", _settings.DaemonLockId) .ExecuteScalarAsync(_cancellation.Token).ConfigureAwait(false); if (!gotLock) { await conn.CloseAsync().ConfigureAwait(false); } } catch (Exception e) { conn?.SafeDispose(); _logger.LogError(e, "Error trying to attain the async daemon lock for database {Database}", _database.Identifier); return(false); } if (gotLock) { _logger.LogInformation("Attained lock for the async daemon for database {Database}, attempting to start all shards", _database.Identifier); try { await startAllProjections(conn).ConfigureAwait(false); stopPollingForOwnership(); return(true); } catch (Exception ex) { _logger.LogError(ex, "Failure while trying to start all async projection shards for database {Database}", _database.Identifier); } } else { _logger.LogDebug("Attempted to attain lock for async projections, but could not take leadership for database {Database}.", _database.Identifier); } if (_timer == null || !_timer.Enabled) { startPollingForOwnership(); } else { _timer.Start(); } return(false); }