Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloszKrajewski committed Jun 20, 2024
1 parent 71f786f commit 96817de
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .nuke/build/_build.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_ANONYMOUSMETHOD_ON_SINGLE_LINE/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=f9fce829_002De6f4_002D4cb2_002D80f1_002D5497c44f51df/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Expand All @@ -24,4 +26,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
17 changes: 17 additions & 0 deletions src/ConfiguratorPlayground/ConfiguratorPlayground.csproj
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>
16 changes: 16 additions & 0 deletions src/ConfiguratorPlayground/Program.cs
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"));
}
11 changes: 11 additions & 0 deletions src/ConfiguratorPlayground/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"ConfiguratorPlayground": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
17 changes: 17 additions & 0 deletions src/ConfiguratorPlayground/Worker.cs
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);
}
}
}
8 changes: 8 additions & 0 deletions src/ConfiguratorPlayground/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions src/ConfiguratorPlayground/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
21 changes: 21 additions & 0 deletions src/K4os.Xpovoc.Core/NewScopeJobHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ async Task IJobHandler.Handle(CancellationToken token, object payload)

protected abstract Task Handle(
CancellationToken token, IServiceProvider services, object payload);
}

public class DirectJobHandler: NewScopeJobHandler
{
public record Context(
CancellationToken Token,
IServiceProvider Services,
object Payload
);

private readonly Func<Context, Task> _handler;

public DirectJobHandler(
IServiceProvider provider,
Func<Context, Task> handler):
base(provider) =>
_handler = handler ?? throw new ArgumentNullException(nameof(handler));

protected override Task Handle(
CancellationToken token, IServiceProvider services, object payload) =>
_handler(new Context(token, services, payload));
}
41 changes: 20 additions & 21 deletions src/K4os.Xpovoc.Hosting/HostedJobScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace K4os.Xpovoc.Hosting
namespace K4os.Xpovoc.Hosting;

public class HostedJobScheduler: IHostedService
{
public class HostedJobScheduler: IHostedService
{
private readonly IServiceProvider _serviceProvider;
private IJobScheduler? _scheduler;
private readonly IServiceProvider _serviceProvider;
private IJobScheduler? _scheduler;

public HostedJobScheduler(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public HostedJobScheduler(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public Task StartAsync(CancellationToken cancellationToken)
{
_scheduler = _serviceProvider.GetRequiredService<IJobScheduler>();
return Task.CompletedTask;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_scheduler = _serviceProvider.GetRequiredService<IJobScheduler>();
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
_scheduler?.Dispose();
_scheduler = null;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_scheduler?.Dispose();
_scheduler = null;
return Task.CompletedTask;
}
}
}
3 changes: 3 additions & 0 deletions src/K4os.Xpovoc.Hosting/K4os.Xpovoc.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions"/>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions"/>
<PackageReference Include="PolySharp"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\K4os.Xpovoc.Abstractions\K4os.Xpovoc.Abstractions.csproj"/>
<ProjectReference Include="..\K4os.Xpovoc.Core\K4os.Xpovoc.Core.csproj"/>
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions src/K4os.Xpovoc.Hosting/SchedulerConfigurator.cs
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;
}
}
20 changes: 20 additions & 0 deletions src/K4os.Xpovoc.Hosting/XpovocConfiguratorExtensions.cs
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));
}
7 changes: 7 additions & 0 deletions src/K4os.Xpovoc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "..\.nuke\build\_b
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "K4os.Xpovoc.Sqs.Test", "K4os.Xpovoc.Sqs.Test\K4os.Xpovoc.Sqs.Test.csproj", "{26039CB2-C5F9-40E6-9F16-85A0CD6897EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfiguratorPlayground", "ConfiguratorPlayground\ConfiguratorPlayground.csproj", "{FEFF5B75-6C2A-4107-A1E7-A0B8E99245E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -148,6 +150,10 @@ Global
{26039CB2-C5F9-40E6-9F16-85A0CD6897EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26039CB2-C5F9-40E6-9F16-85A0CD6897EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26039CB2-C5F9-40E6-9F16-85A0CD6897EF}.Release|Any CPU.Build.0 = Release|Any CPU
{FEFF5B75-6C2A-4107-A1E7-A0B8E99245E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FEFF5B75-6C2A-4107-A1E7-A0B8E99245E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEFF5B75-6C2A-4107-A1E7-A0B8E99245E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEFF5B75-6C2A-4107-A1E7-A0B8E99245E2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -170,5 +176,6 @@ Global
{ED3837B6-3434-4213-B6D1-9DA0E7F9A258} = {4964B568-60A9-45DC-BFB5-F09772ECB635}
{9A66EC7B-41D6-4C72-94CE-D359164EE0AA} = {4964B568-60A9-45DC-BFB5-F09772ECB635}
{26039CB2-C5F9-40E6-9F16-85A0CD6897EF} = {05CC704E-DDB0-4335-82E4-460AD6E89D20}
{FEFF5B75-6C2A-4107-A1E7-A0B8E99245E2} = {05CC704E-DDB0-4335-82E4-460AD6E89D20}
EndGlobalSection
EndGlobal

0 comments on commit 96817de

Please sign in to comment.