-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to resilience policies in Polly v8 with a new example (#79)
- Loading branch information
1 parent
63fea89
commit 493f332
Showing
12 changed files
with
124 additions
and
12 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,58 @@ | ||
using Polly.Retry; | ||
using Polly; | ||
using Tingle.PeriodicTasks; | ||
|
||
var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureServices((context, services) => | ||
{ | ||
var environment = context.HostingEnvironment; | ||
var configuration = context.Configuration; | ||
// register IDistributedLockProvider | ||
var path = configuration.GetValue<string?>("DistributedLocking:FilePath") | ||
?? Path.Combine(environment.ContentRootPath, "distributed-locks"); | ||
services.AddSingleton<Medallion.Threading.IDistributedLockProvider>(provider => | ||
{ | ||
return new Medallion.Threading.FileSystem.FileDistributedSynchronizationProvider(Directory.CreateDirectory(path)); | ||
}); | ||
// register periodic tasks | ||
services.AddPeriodicTasks(builder => | ||
{ | ||
builder.AddTask<DatabaseCleanerTask>(o => | ||
{ | ||
o.Schedule = "*/1 * * * *"; | ||
o.ResiliencePipeline = new ResiliencePipelineBuilder() | ||
.AddRetry(new RetryStrategyOptions | ||
{ | ||
ShouldHandle = new PredicateBuilder().Handle<Exception>(), | ||
Delay = TimeSpan.FromSeconds(1), | ||
MaxRetryAttempts = 3, | ||
BackoffType = DelayBackoffType.Constant, | ||
OnRetry = args => | ||
{ | ||
Console.WriteLine($"Attempt {args.AttemptNumber} failed; retrying in {args.RetryDelay}"); | ||
return ValueTask.CompletedTask; | ||
}, | ||
}) | ||
.Build(); | ||
}); | ||
}); | ||
}) | ||
.Build(); | ||
|
||
await host.RunAsync(); | ||
|
||
class DatabaseCleanerTask(ILogger<DatabaseCleanerTask> logger) : IPeriodicTask | ||
{ | ||
public async Task ExecuteAsync(PeriodicTaskExecutionContext context, CancellationToken cancellationToken = default) | ||
{ | ||
if (Random.Shared.Next(1, 5) > 2) // 60% of the time | ||
{ | ||
throw new Exception("Failed to clean up old records from the database"); | ||
} | ||
|
||
logger.LogInformation("Cleaned up old records from the database"); | ||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"profiles": { | ||
"ResilienceSample": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DistributedLock.FileSystem" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Tingle.PeriodicTasks\Tingle.PeriodicTasks.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,15 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
}, | ||
"Console": { | ||
"FormatterName": "simple", | ||
"FormatterOptions": { | ||
"TimestampFormat": "[yyyy-MM-dd HH:mm:ss] ", | ||
"SingleLine": true | ||
} | ||
} | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
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
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