-
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.
- Loading branch information
1 parent
71f786f
commit 96817de
Showing
13 changed files
with
236 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UserSecretsId>dotnet-ConfiguratorPlayground-66B43100-EA16-4F40-98D4-92F9B78A02B9</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\K4os.Xpovoc.Hosting\K4os.Xpovoc.Hosting.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,16 @@ | ||
using ConfiguratorPlayground; | ||
using K4os.Xpovoc.Hosting; | ||
|
||
var builder = Host.CreateDefaultBuilder(args); | ||
|
||
var host = builder | ||
.AddXpovoc(ConfigureXpovoc) | ||
.ConfigureServices(services => { services.AddHostedService<Worker>(); }) | ||
.Build(); | ||
|
||
await host.RunAsync(); | ||
|
||
static void ConfigureXpovoc(IXpovocBuilder xb) | ||
{ | ||
xb.UseDatabase(dbc => dbc.UseSqlite("Data Source=jobs.db")); | ||
} |
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": { | ||
"ConfiguratorPlayground": { | ||
"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,17 @@ | ||
namespace ConfiguratorPlayground; | ||
|
||
public class Worker: BackgroundService | ||
{ | ||
private readonly ILogger<Worker> _logger; | ||
|
||
public Worker(ILogger<Worker> logger) { _logger = logger; } | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); | ||
await Task.Delay(1000, stoppingToken); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.CodeDom; | ||
using System.Reactive.Concurrency; | ||
using System.Threading.Tasks; | ||
using K4os.Xpovoc.Abstractions; | ||
using K4os.Xpovoc.Core; | ||
using K4os.Xpovoc.Core.Rx; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace K4os.Xpovoc.Hosting; | ||
|
||
public static class BuilderTester | ||
{ | ||
public static void Main() | ||
{ | ||
var builder = (IHostBuilder)null!; | ||
|
||
builder.AddXpovoc(b => b.UseMemoryJobScheduler()); | ||
} | ||
} | ||
|
||
public interface IXpovocBuilder | ||
{ | ||
public IServiceCollection Services { get; } | ||
} | ||
|
||
public interface IXpovocHostBuilder: IXpovocBuilder | ||
{ | ||
} | ||
|
||
public class XpovocHostBuilder: IXpovocHostBuilder | ||
{ | ||
public IServiceCollection Services { get; } | ||
|
||
public XpovocHostBuilder(IServiceCollection services) => Services = services; | ||
|
||
public void Apply(Action<IXpovocHostBuilder> configure) | ||
{ | ||
Services.AddHostedService<HostedJobScheduler>(); | ||
configure(this); | ||
Services.TryAddSingleton<IJobScheduler, RxJobScheduler>(); | ||
Services.TryAddSingleton<IJobHandler, SimpleJobHandler>(); | ||
} | ||
} | ||
|
||
public static class XpovocHostBuilderExtensions | ||
{ | ||
public static IXpovocHostBuilder UseJobScheduler<T>( | ||
this IXpovocHostBuilder builder) | ||
where T: class, IJobScheduler | ||
{ | ||
builder.Services.AddSingleton<IJobScheduler, T>(); | ||
return builder; | ||
} | ||
|
||
public static IXpovocHostBuilder UseJobHandler<T>( | ||
this IXpovocHostBuilder builder) | ||
where T: class, IJobHandler | ||
{ | ||
builder.Services.AddSingleton<IJobHandler, T>(); | ||
return builder; | ||
} | ||
|
||
public static IXpovocHostBuilder UseMemoryJobScheduler( | ||
this IXpovocHostBuilder builder) => | ||
builder.UseJobScheduler<RxJobScheduler>(); | ||
|
||
public static IXpovocHostBuilder UseSimpleJobHandler( | ||
this IXpovocHostBuilder builder) => | ||
builder.UseJobHandler<SimpleJobHandler>(); | ||
|
||
public static IXpovocHostBuilder UseDirectJobHandler( | ||
this IXpovocHostBuilder builder, | ||
Func<DirectJobHandler.Context, Task> handler) | ||
{ | ||
builder.Services.AddSingleton<IJobHandler>(p => new DirectJobHandler(p, handler)); | ||
return builder; | ||
} | ||
} |
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,20 @@ | ||
using System; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace K4os.Xpovoc.Hosting; | ||
|
||
public static class XpovocConfiguratorExtensions | ||
{ | ||
public static IHostBuilder AddXpovoc( | ||
this IHostBuilder hostBuilder, | ||
Action<HostBuilderContext, IXpovocHostBuilder> configure) => | ||
hostBuilder.ConfigureServices( | ||
(ctx, svc) => { | ||
new XpovocHostBuilder(svc).Apply(b => configure(ctx, b)); | ||
}); | ||
|
||
public static IHostBuilder AddXpovoc( | ||
this IHostBuilder hostBuilder, | ||
Action<IXpovocHostBuilder> configure) => | ||
hostBuilder.AddXpovoc((_, b) => configure(b)); | ||
} |
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