Skip to content

Commit

Permalink
Merge pull request #1 from MiloszKrajewski/alive-keeper-fixes
Browse files Browse the repository at this point in the history
Alive keeper fixes
  • Loading branch information
MiloszKrajewski authored Nov 2, 2023
2 parents a9f309d + 601a75c commit ffee1ca
Show file tree
Hide file tree
Showing 22 changed files with 616 additions and 95 deletions.
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## 0.0.12 (2023/05/18)
## 0.0.14 (2023/11/02)
* FIXED: multiple bugs in AliveKeeper

## 0.0.13 (2023/05/18)
* ADDED: maximum concurrency setting
* ADDED: concurrency policies for AliveKeeper
* FIXED: safe sync policy was deadlocking in some cases
* ADDED: ability for agents to end their work without being disposed
* ADDED: cancellation token can be passed to agents

## 0.0.8 (2023/05/06)
* CHANGED: build process
Expand Down
8 changes: 8 additions & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ storage none
framework auto-detect
source https://www.nuget.org/api/v2

nuget Microsoft.Extensions.Hosting
nuget Microsoft.Extensions.DependencyInjection
nuget Microsoft.Extensions.DependencyInjection.Abstractions
nuget Microsoft.Extensions.Logging
nuget Microsoft.Extensions.Logging.Abstractions

nuget System.Reactive
nuget System.Interactive

nuget Serilog
nuget Serilog.Sinks.Console
nuget Serilog.Sinks.File
nuget Serilog.Extensions.Hosting
nuget Serilog.Settings.Configuration
nuget Serilog.Formatting.Compact
136 changes: 136 additions & 0 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,156 @@ STORAGE: NONE
RESTRICTION: == net6.0
NUGET
remote: https://www.nuget.org/api/v2
Microsoft.Extensions.Configuration (7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.Configuration.Abstractions (7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.Configuration.Binder (7.0.4)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.CommandLine (7.0)
Microsoft.Extensions.Configuration (>= 7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.EnvironmentVariables (7.0)
Microsoft.Extensions.Configuration (>= 7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.FileExtensions (7.0)
Microsoft.Extensions.Configuration (>= 7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.FileProviders.Abstractions (>= 7.0)
Microsoft.Extensions.FileProviders.Physical (>= 7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.Configuration.Json (7.0)
Microsoft.Extensions.Configuration (>= 7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.FileExtensions (>= 7.0)
Microsoft.Extensions.FileProviders.Abstractions (>= 7.0)
System.Text.Json (>= 7.0)
Microsoft.Extensions.Configuration.UserSecrets (7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.Json (>= 7.0)
Microsoft.Extensions.FileProviders.Abstractions (>= 7.0)
Microsoft.Extensions.FileProviders.Physical (>= 7.0)
Microsoft.Extensions.DependencyInjection (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (7.0)
Microsoft.Extensions.DependencyModel (7.0)
System.Text.Encodings.Web (>= 7.0)
System.Text.Json (>= 7.0)
Microsoft.Extensions.FileProviders.Abstractions (7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.FileProviders.Physical (7.0)
Microsoft.Extensions.FileProviders.Abstractions (>= 7.0)
Microsoft.Extensions.FileSystemGlobbing (>= 7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.FileSystemGlobbing (7.0)
Microsoft.Extensions.Hosting (7.0.1)
Microsoft.Extensions.Configuration (>= 7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.Binder (>= 7.0.3)
Microsoft.Extensions.Configuration.CommandLine (>= 7.0)
Microsoft.Extensions.Configuration.EnvironmentVariables (>= 7.0)
Microsoft.Extensions.Configuration.FileExtensions (>= 7.0)
Microsoft.Extensions.Configuration.Json (>= 7.0)
Microsoft.Extensions.Configuration.UserSecrets (>= 7.0)
Microsoft.Extensions.DependencyInjection (>= 7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.FileProviders.Abstractions (>= 7.0)
Microsoft.Extensions.FileProviders.Physical (>= 7.0)
Microsoft.Extensions.Hosting.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Logging.Configuration (>= 7.0)
Microsoft.Extensions.Logging.Console (>= 7.0)
Microsoft.Extensions.Logging.Debug (>= 7.0)
Microsoft.Extensions.Logging.EventLog (>= 7.0)
Microsoft.Extensions.Logging.EventSource (>= 7.0)
Microsoft.Extensions.Options (>= 7.0.1)
System.Diagnostics.DiagnosticSource (>= 7.0.1)
Microsoft.Extensions.Hosting.Abstractions (7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.FileProviders.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (7.0)
Microsoft.Extensions.DependencyInjection (>= 7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Options (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (7.0)
Microsoft.Extensions.Logging.Configuration (7.0)
Microsoft.Extensions.Configuration (>= 7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.Binder (>= 7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Options (>= 7.0)
Microsoft.Extensions.Options.ConfigurationExtensions (>= 7.0)
Microsoft.Extensions.Logging.Console (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Logging.Configuration (>= 7.0)
Microsoft.Extensions.Options (>= 7.0)
System.Runtime.CompilerServices.Unsafe (>= 6.0)
System.Text.Json (>= 7.0)
Microsoft.Extensions.Logging.Debug (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Logging.EventLog (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Options (>= 7.0)
System.Diagnostics.EventLog (>= 7.0)
Microsoft.Extensions.Logging.EventSource (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Options (>= 7.0)
Microsoft.Extensions.Primitives (>= 7.0)
System.Runtime.CompilerServices.Unsafe (>= 6.0)
System.Text.Json (>= 7.0)
Microsoft.Extensions.Options (7.0.1)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.Options.ConfigurationExtensions (7.0)
Microsoft.Extensions.Configuration.Abstractions (>= 7.0)
Microsoft.Extensions.Configuration.Binder (>= 7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Options (>= 7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.Primitives (7.0)
System.Runtime.CompilerServices.Unsafe (>= 6.0)
Serilog (3.0.1)
Serilog.Extensions.Hosting (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Hosting.Abstractions (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Serilog (>= 2.12)
Serilog.Extensions.Logging (>= 7.0)
Serilog.Extensions.Logging (7.0)
Microsoft.Extensions.Logging (>= 7.0)
Serilog (>= 2.12)
Serilog.Formatting.Compact (1.1)
Serilog (>= 2.8)
Serilog.Settings.Configuration (7.0.1)
Microsoft.Extensions.Configuration.Binder (>= 7.0)
Microsoft.Extensions.DependencyModel (>= 7.0)
Serilog (>= 2.12)
Serilog.Sinks.Console (4.1)
Serilog (>= 2.10)
Serilog.Sinks.File (5.0)
Serilog (>= 2.10)
System.Diagnostics.DiagnosticSource (7.0.2)
System.Runtime.CompilerServices.Unsafe (>= 6.0)
System.Diagnostics.EventLog (7.0)
System.Interactive (6.0.1)
System.Reactive (5.0)
System.Runtime.CompilerServices.Unsafe (6.0)
System.Text.Encodings.Web (7.0)
System.Runtime.CompilerServices.Unsafe (>= 6.0)
System.Text.Json (7.0.3)
System.Runtime.CompilerServices.Unsafe (>= 6.0)
System.Text.Encodings.Web (>= 7.0)
2 changes: 1 addition & 1 deletion src/K4os.Async.Toys.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static async Task Execute(
switch (command)
{
case 's':
keeper.Upkeep(argument);
keeper.Register(argument);
break;
case 'd':
await keeper.Delete(argument);
Expand Down
19 changes: 19 additions & 0 deletions src/K4os.Async.Toys.Tests/TokenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Xunit;

namespace K4os.Async.Toys.Test;

public class TokenTests
{
[Fact]
public void LinkedTokenCancellationCascade()
{
var a = new CancellationTokenSource();
var b = new CancellationTokenSource();
var c = new CancellationTokenSource();
var cts = CancellationTokenSource.CreateLinkedTokenSource(a.Token, b.Token, c.Token).Token;

Assert.False(cts.IsCancellationRequested);
a.Cancel();
Assert.True(cts.IsCancellationRequested);
}
}
6 changes: 6 additions & 0 deletions src/K4os.Async.Toys.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "K4os.Async.Toys.App", "K4os
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Playground", "Playground\Playground.csproj", "{5A8468E0-AB21-4749-AD96-6179566BF31F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -59,6 +61,10 @@ Global
{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}.Release|Any CPU.Build.0 = Release|Any CPU
{5A8468E0-AB21-4749-AD96-6179566BF31F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A8468E0-AB21-4749-AD96-6179566BF31F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A8468E0-AB21-4749-AD96-6179566BF31F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A8468E0-AB21-4749-AD96-6179566BF31F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
23 changes: 17 additions & 6 deletions src/K4os.Async.Toys/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ public abstract class AbstractAgent: IAgent, IAgentContext

/// <summary>Creates new agent.</summary>
/// <param name="logger">Logger.</param>
protected AbstractAgent(ILogger? logger)
/// <param name="token">Cancellation token.</param>
protected AbstractAgent(ILogger? logger, CancellationToken token = default)
{
Log = logger ?? NullLogger.Instance;
_cancel = new CancellationTokenSource();
_done = Task.Run(Loop);
_cancel = CancellationTokenSource.CreateLinkedTokenSource(token);
_done = Task.Run(Loop, _cancel.Token);
}

/// <inheritdoc />
Expand Down Expand Up @@ -149,7 +150,12 @@ public partial class Agent: AbstractAgent
/// <summary>Creates new agent.</summary>
/// <param name="logger">Logger.</param>
/// <param name="action">Action to be executed in the loop.</param>
public Agent(Func<IAgentContext, Task<bool>> action, ILogger? logger = null): base(logger) =>
/// <param name="token">Cancellation token.</param>
public Agent(
Func<IAgentContext, Task<bool>> action,
ILogger? logger = null,
CancellationToken token = default):
base(logger, token) =>
_action = action.Required(nameof(action));

/// <inheritdoc />
Expand All @@ -166,12 +172,17 @@ public class Agent<T>: AbstractAgent, IAgent<T>, IAgentContext<T>
/// <summary>Creates new agent and starts it.</summary>
/// <param name="logger">Log.</param>
/// <param name="action">Agent's action.</param>
public Agent(Func<IAgentContext<T>, Task<bool>> action, ILogger? logger = null): base(logger) =>
/// <param name="token">Cancellation token.</param>
public Agent(
Func<IAgentContext<T>, Task<bool>> action,
ILogger? logger = null,
CancellationToken token = default):
base(logger, token) =>
_action = action.Required(nameof(action));

/// <inheritdoc />
protected override Task<bool> Execute() => _action(this);

/// <summary>Enqueues item to be processed by agent.</summary>
/// <param name="item">Item.</param>
/// <exception cref="InvalidOperationException">Thrown when queue is full.</exception>
Expand Down
Loading

0 comments on commit ffee1ca

Please sign in to comment.