-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Data; | ||
using System.Threading; | ||
using Shuttle.Core.Contract; | ||
|
||
namespace Shuttle.Core.Data | ||
{ | ||
public static class DatabaseContextFactoryExtensions | ||
{ | ||
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, | ||
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15) | ||
{ | ||
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory)); | ||
|
||
return IsAvailable(() => | ||
{ | ||
using (databaseContextFactory.Create()) | ||
{ | ||
} | ||
}, cancellationToken, retries, secondsBetweenRetries); | ||
} | ||
|
||
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string name, | ||
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15) | ||
{ | ||
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory)); | ||
|
||
return IsAvailable(() => | ||
{ | ||
using (databaseContextFactory.Create(name)) | ||
{ | ||
} | ||
}, cancellationToken, retries, secondsBetweenRetries); | ||
} | ||
|
||
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string providerName, IDbConnection dbConnection, | ||
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15) | ||
{ | ||
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory)); | ||
|
||
return IsAvailable(() => | ||
{ | ||
using (databaseContextFactory.Create(providerName, dbConnection)) | ||
{ | ||
} | ||
}, cancellationToken, retries, secondsBetweenRetries); | ||
} | ||
|
||
public static bool IsAvailable(this IDatabaseContextFactory databaseContextFactory, string providerName, string connectionString, | ||
CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15) | ||
{ | ||
Guard.AgainstNull(databaseContextFactory, nameof(databaseContextFactory)); | ||
|
||
return IsAvailable(() => | ||
{ | ||
using (databaseContextFactory.Create(providerName, connectionString)) | ||
{ | ||
} | ||
}, cancellationToken, retries, secondsBetweenRetries); | ||
} | ||
|
||
private static bool IsAvailable(Action action, CancellationToken cancellationToken, int retries = 4, int secondsBetweenRetries = 15) | ||
{ | ||
var attempt = 0; | ||
|
||
do | ||
{ | ||
try | ||
{ | ||
action.Invoke(); | ||
|
||
break; | ||
} | ||
catch | ||
{ | ||
attempt++; | ||
|
||
if (attempt < retries) | ||
{ | ||
var wait = DateTime.Now.AddSeconds(secondsBetweenRetries); | ||
|
||
while (!cancellationToken.IsCancellationRequested && DateTime.Now < wait) | ||
{ | ||
Thread.Sleep(250); | ||
} | ||
} | ||
} | ||
} while (attempt < retries); | ||
|
||
return attempt <= retries; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters