From f0d262b060864185bf4f97e7fb4ce5d809454111 Mon Sep 17 00:00:00 2001 From: MareMare Date: Mon, 12 Sep 2022 12:27:16 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89=E3=82=BF?= =?UTF-8?q?=E3=82=A4=E3=83=A0=E3=82=A2=E3=82=A6=E3=83=88=E7=A7=92=E3=82=92?= =?UTF-8?q?=E6=A7=8B=E6=88=90=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=8B?= =?UTF-8?q?=E3=82=89=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=97=E3=81=BE=E3=81=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ src/SqlDatabaseToolkit/SqlDatabaseOptions.cs | 9 ++++++++ src/SqlDatabaseToolkit/SqlDatabaseToolkit.cs | 23 +++++++++++++++----- src/appsettings.json | 1 + 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b56f9ef..8329ac8 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ SQL Server データベースの完全バックアップ、および新しい場 "ConnectionString": "Data Source=localhost;Integrated Security=True", "BackupDirectory": "C:\\DB\\BACKUP", "RestoreDirectory": "C:\\DB\\RESTORE", + "CommandTimeoutSeconds": 60, "Databases": [ { "Name": "DB1" }, { "Name": "DB2" }, @@ -52,6 +53,7 @@ SQL Server データベースの完全バックアップ、および新しい場 "ConnectionString": "Data Source=localhost;Integrated Security=True", "BackupDirectory": "C:\\DB\\BACKUP", "RestoreDirectory": "C:\\DB\\RESTORE", + "CommandTimeoutSeconds": 60, "Databases": [ { "Name": "DB1" }, { "Name": "DB2" }, @@ -76,6 +78,7 @@ SQL Server データベースの完全バックアップ、および新しい場 |ConnectionString|接続文字列|`Data Source=localhost;Integrated Security=True`|| |BackupDirectory|`*.bak` の格納先フォルダ|`C:\\DB\\BACKUP`|ローカルフォルダのみ| |RestoreDirectory|`*.bak` の復元先フォルダ|`C:\\DB\\RESTORE`|ローカルフォルダのみ、要アクセス権| +|CommandTimeoutSeconds|コマンドタイムアウト秒|60|既定値は60秒| |Databases|データベース設定の配列|| |Databases:Name|データベース名|`DB1`| diff --git a/src/SqlDatabaseToolkit/SqlDatabaseOptions.cs b/src/SqlDatabaseToolkit/SqlDatabaseOptions.cs index 5c1a080..8c3cb20 100644 --- a/src/SqlDatabaseToolkit/SqlDatabaseOptions.cs +++ b/src/SqlDatabaseToolkit/SqlDatabaseOptions.cs @@ -45,6 +45,15 @@ public class SqlDatabaseOptions /// public string RestoreDirectory { get; set; } = null!; + /// + /// コマンドタイムアウト秒を取得または設定します。 + /// + /// + /// 値を表す 型。 + /// コマンドタイムアウト秒。既定値は です。 + /// + public int CommandTimeoutSeconds { get; set; } = 60; + /// /// データベース設定のコレクションを取得または設定します。 /// diff --git a/src/SqlDatabaseToolkit/SqlDatabaseToolkit.cs b/src/SqlDatabaseToolkit/SqlDatabaseToolkit.cs index 6475055..fa69fef 100644 --- a/src/SqlDatabaseToolkit/SqlDatabaseToolkit.cs +++ b/src/SqlDatabaseToolkit/SqlDatabaseToolkit.cs @@ -106,6 +106,7 @@ private async Task BackupCoreAsync( .ToString(); var connection = new SqlConnection(this._connectionString); + var commandTimeoutSeconds = this._options.CommandTimeoutSeconds; try { this._logger?.LogDebug("完全バックアップを開始します。{Database} {Backup}", databaseName, backupFilePath); @@ -114,7 +115,7 @@ await connection.ExecuteAsync( sql, new { databaseName, description, backupFilePath }, commandType: CommandType.Text, - commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds) + commandTimeout: commandTimeoutSeconds) .ConfigureAwait(false); this._logger?.LogInformation("完全バックアップが完了しました。{Database} {Backup}", databaseName, backupFilePath); } @@ -151,13 +152,21 @@ private async Task RestoreCoreAsync( backupFilePath, restoreDirectoryPath); + var commandTimeoutSeconds = this._options.CommandTimeoutSeconds; var pairs = await GetFilePairsAsync( this._connectionString, backupFilePath, restoreDirectoryPath, + commandTimeoutSeconds, cancellationToken) .ConfigureAwait(false); - await RestoreCurrentlyAsync(this._connectionString, databaseName, backupFilePath, pairs, cancellationToken) + await RestoreCurrentlyAsync( + this._connectionString, + databaseName, + backupFilePath, + pairs, + commandTimeoutSeconds, + cancellationToken) .ConfigureAwait(false); this._logger?.LogInformation( @@ -184,6 +193,7 @@ static string ResolveNewPhysicalPath(string originalFilePath, string moveToDirec string connectionString, string backupFilePath, string restoreDirectoryPath, + int commandTimeoutSeconds, CancellationToken cancellationToken = default) { var connection = new SqlConnection(connectionString); @@ -198,7 +208,7 @@ static string ResolveNewPhysicalPath(string originalFilePath, string moveToDirec sql, new { backupFilePath }, commandType: CommandType.Text, - commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds) + commandTimeout: commandTimeoutSeconds) .ConfigureAwait(false); var results = records @@ -224,6 +234,7 @@ static async Task RestoreCurrentlyAsync( string databaseName, string backupFilePath, IEnumerable<(string LogicalName, string PhysicalName, string MoveToFilePath)> filePairs, + int commandTimeoutSeconds, CancellationToken cancellationToken = default) { var builder = new StringBuilder() @@ -245,18 +256,18 @@ static async Task RestoreCurrentlyAsync( await connection.ExecuteAsync( $"ALTER DATABASE [{databaseName}] SET OFFLINE WITH ROLLBACK IMMEDIATE", commandType: CommandType.Text, - commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds) + commandTimeout: commandTimeoutSeconds) .ConfigureAwait(false); await connection.ExecuteAsync( sql, new { databaseName, backupFilePath }, commandType: CommandType.Text, - commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds) + commandTimeout: commandTimeoutSeconds) .ConfigureAwait(false); await connection.ExecuteAsync( $"ALTER DATABASE [{databaseName}] SET ONLINE", commandType: CommandType.Text, - commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds) + commandTimeout: commandTimeoutSeconds) .ConfigureAwait(false); } finally diff --git a/src/appsettings.json b/src/appsettings.json index cbc3aed..301c28e 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -3,6 +3,7 @@ "ConnectionString": "Data Source=localhost;Integrated Security=True", "BackupDirectory": "C:\\DB\\BACKUP", "RestoreDirectory": "C:\\DB\\RESTORE", + "CommandTimeoutSeconds": 30, "Databases": [ { "Name": "DB1" }, { "Name": "DB2" },