-
-
Notifications
You must be signed in to change notification settings - Fork 250
Adding Foundatio builders for configuring in IServiceCollection. Various other improvements. #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24a6cf1
Adding Foundatio builders for configuring in IServiceCollection. Vari…
ejsmith 564fb57
Couple tweaks
ejsmith b124970
Change how we deal with primitive values in STJ serializer
ejsmith b4b2ae9
Don't log redis
ejsmith b5548e8
Builder updates
ejsmith 92e2594
Missing namespace
ejsmith b333317
Fix benchmark
ejsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,26 +1,42 @@ | ||
| using System.Threading; | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Foundatio.Jobs; | ||
| using Foundatio.Resilience; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Foundatio.HostingSample; | ||
|
|
||
| [Job(Description = "Sample 1 job", Interval = "5s", IterationLimit = 5)] | ||
| public class Sample1Job : IJob | ||
| { | ||
| private readonly IResiliencePolicy _policy; | ||
| private readonly ILogger _logger; | ||
| private int _iterationCount = 0; | ||
|
|
||
| public Sample1Job(ILoggerFactory loggerFactory) | ||
| public Sample1Job(IResiliencePolicyProvider provider, ILoggerFactory loggerFactory) | ||
| { | ||
| // get policy for Sample1Job and if not found, try to get policy for IJob, then fallback to default policy | ||
| _policy = provider.GetPolicy<Sample1Job, IJob>(); | ||
| _logger = loggerFactory.CreateLogger<Sample1Job>(); | ||
| } | ||
|
|
||
| public Task<JobResult> RunAsync(CancellationToken cancellationToken = default) | ||
| public async Task<JobResult> RunAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| Interlocked.Increment(ref _iterationCount); | ||
| _logger.LogTrace("Sample1Job Run #{IterationCount} Thread={ManagedThreadId}", _iterationCount, Thread.CurrentThread.ManagedThreadId); | ||
| return await _policy.ExecuteAsync(async () => | ||
| { | ||
| int count = Interlocked.Increment(ref _iterationCount); | ||
| _logger.LogTrace("Sample1Job Run #{IterationCount} Thread={ManagedThreadId}", _iterationCount, Thread.CurrentThread.ManagedThreadId); | ||
|
|
||
| return Task.FromResult(JobResult.Success); | ||
| if (count < 3) | ||
| { | ||
| _logger.LogInformation("Sample1Job Run #{IterationCount} Thread={ManagedThreadId} - Simulating failure", _iterationCount, Thread.CurrentThread.ManagedThreadId); | ||
| throw new InvalidOperationException("Simulated failure"); | ||
| } | ||
|
|
||
| await Task.Delay(5000, cancellationToken); | ||
|
|
||
| return JobResult.Success; | ||
| }, cancellationToken: cancellationToken); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Foundatio.Extensions; | ||
|
|
||
| public static class ServicesExtensions | ||
| { | ||
| /// <summary> | ||
| /// Replaces an existing service descriptor with a new singleton service of the specified type. | ||
| /// </summary> | ||
| /// <param name="services"></param> | ||
| /// <typeparam name="TService"></typeparam> | ||
| /// <typeparam name="TImplementation"></typeparam> | ||
| /// <returns></returns> | ||
| public static IServiceCollection ReplaceSingleton<TService, TImplementation>(this IServiceCollection services) | ||
| where TService : class | ||
| where TImplementation : class, TService | ||
| { | ||
| // Remove the existing service descriptor | ||
| var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(TService)); | ||
| if (descriptor != null) | ||
| { | ||
| services.Remove(descriptor); | ||
| } | ||
|
|
||
| // Add the new singleton service | ||
| services.AddSingleton<TService, TImplementation>(); | ||
|
|
||
| return services; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Replaces an existing service descriptor with a new singleton service of the specified type. | ||
| /// </summary> | ||
| /// <param name="services"></param> | ||
| /// <param name="implementationFactory"></param> | ||
| /// <typeparam name="TService"></typeparam> | ||
| /// <returns></returns> | ||
| public static IServiceCollection ReplaceSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory) | ||
| where TService : class | ||
| { | ||
| // Remove the existing service descriptor | ||
| var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(TService)); | ||
| if (descriptor != null) | ||
| { | ||
| services.Remove(descriptor); | ||
| } | ||
|
|
||
| // Add the new singleton service | ||
| services.AddSingleton(implementationFactory); | ||
|
|
||
| return services; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.