Esempio n. 1
0
        /// <summary>
        /// Prepare database connection for future communications with server.
        /// </summary>
        private async Task <IDbConnection> PrepareConnectionAsync(IDbConnection dbConnection)
        {
            if (!dbConnection.State.HasFlag(ConnectionState.Open))
            {
                if (dbConnection is DbConnection awaitableConnection)
                {
                    await awaitableConnection.OpenAsync();
                }
                else
                {
                    dbConnection.Open();
                }
            }

            // Acquire lock to prevent multiple threads from parallel scripts execution.
            using var _ = await Lock.AcquireAsync(lockTimeout : TimeSpan.FromMinutes(1));

            if (!await DatabaseStructureIsPrepared(dbConnection))
            {
                await sqlScriptsExecutor.ExecuteScriptsAsync(dbConnection);
                await FillParameterDictionaryTable(dbConnection);
            }

            return(dbConnection);
        }
Esempio n. 2
0
        exception is SocketException;        // Network related

        // Gets the next cache file and moves it to "processing"
        private async Task <string?> TryPrepareNextCacheFileAsync(
            CancellationToken cancellationToken = default)
        {
            using var lockClaim = await _cacheDirectoryLock.AcquireAsync(cancellationToken).ConfigureAwait(false);

            var filePath = GetCacheFilePaths().FirstOrDefault();

            if (string.IsNullOrWhiteSpace(filePath))
            {
                _options.DiagnosticLogger?.LogDebug("No cached file to process.");
                return(null);
            }

            var targetFilePath = Path.Combine(_processingDirectoryPath, Path.GetFileName(filePath));

            Directory.CreateDirectory(_processingDirectoryPath);
            File.Move(filePath, targetFilePath);

            return(targetFilePath);
        }