Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public Task<List<Endpoint>> GetEndpoints(CancellationToken cancellationToken)

class AuditCountApi_ReturningThreeAuditCounts : IAuditCountApi
{
public async Task<IList<AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken token)
public async Task<IList<AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken cancellationToken)
{
var auditCounts = new List<AuditCount>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

class FakeAuditCountApi : IAuditCountApi
{
public Task<IList<ServiceControl.Api.Contracts.AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken token) => throw new NotImplementedException();
public Task<IList<ServiceControl.Api.Contracts.AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken cancellationToken) => throw new NotImplementedException();
}
}
7 changes: 0 additions & 7 deletions src/ServiceControl.Api/.editorconfig

This file was deleted.

2 changes: 1 addition & 1 deletion src/ServiceControl.Api/IAuditCountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface IAuditCountApi
{
Task<IList<AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken token);
Task<IList<AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken cancellationToken = default);
}
}
6 changes: 3 additions & 3 deletions src/ServiceControl.Api/IConfigurationApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

public interface IConfigurationApi
{
Task<RootUrls> GetUrls(string baseUrl, CancellationToken cancellationToken);
Task<RootUrls> GetUrls(string baseUrl, CancellationToken cancellationToken = default);

Task<object> GetConfig(CancellationToken cancellationToken);
Task<object> GetConfig(CancellationToken cancellationToken = default);

Task<RemoteConfiguration[]> GetRemoteConfigs(CancellationToken cancellationToken);
Task<RemoteConfiguration[]> GetRemoteConfigs(CancellationToken cancellationToken = default);
}
}
2 changes: 1 addition & 1 deletion src/ServiceControl.Api/IEndpointsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface IEndpointsApi
{
Task<List<Endpoint>> GetEndpoints(CancellationToken cancellationToken);
Task<List<Endpoint>> GetEndpoints(CancellationToken cancellationToken = default);
}
}
7 changes: 0 additions & 7 deletions src/ServiceControl.DomainEvents/.editorconfig

This file was deleted.

10 changes: 9 additions & 1 deletion src/ServiceControl.DomainEvents/DomainEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class DomainEvents(IServiceProvider serviceProvider, ILogger<DomainEvents> logger) : IDomainEvents
{
public async Task Raise<T>(T domainEvent, CancellationToken cancellationToken) where T : IDomainEvent
public async Task Raise<T>(T domainEvent, CancellationToken cancellationToken = default) where T : IDomainEvent
{
var handlers = serviceProvider.GetServices<IDomainHandler<T>>();
foreach (var handler in handlers)
Expand All @@ -18,6 +18,10 @@ public async Task Raise<T>(T domainEvent, CancellationToken cancellationToken) w
await handler.Handle(domainEvent, cancellationToken)
.ConfigureAwait(false);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception e)
{
logger.LogError(e, "Unexpected error publishing domain event {EventType}", typeof(T));
Expand All @@ -33,6 +37,10 @@ await handler.Handle(domainEvent, cancellationToken)
await handler.Handle(domainEvent, cancellationToken)
.ConfigureAwait(false);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception e)
{
logger.LogError(e, "Unexpected error publishing domain event {EventType}", typeof(T));
Expand Down
6 changes: 0 additions & 6 deletions src/ServiceControl.Hosting/.editorconfig

This file was deleted.

2 changes: 2 additions & 0 deletions src/ServiceControl.Hosting/Auth/PermissionPolicyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static FrozenDictionary<string, AuthorizationPolicy> BuildPolicies(OpenIdConnect
: AllowAll,
StringComparer.Ordinal);

#pragma warning disable PS0018 // IAuthorizationPolicyProvider declares these without a CancellationToken
public Task<AuthorizationPolicy?> GetPolicyAsync(string policyName) =>
Task.FromResult(policies.GetValueOrDefault(policyName));

Expand All @@ -64,4 +65,5 @@ public Task<AuthorizationPolicy> GetDefaultPolicyAsync()

public Task<AuthorizationPolicy?> GetFallbackPolicyAsync()
=> Task.FromResult(authorizationOptions.Value.FallbackPolicy);
#pragma warning restore PS0018
}
2 changes: 2 additions & 0 deletions src/ServiceControl.Hosting/Auth/RolesClaimsTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed class RolesClaimsTransformation(OpenIdConnectSettings oidcSettings
// placeholder is required because a Claim value cannot be null.
const string SentinelClaimValue = "1";

#pragma warning disable PS0018 // IClaimsTransformation declares this without a CancellationToken
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
var isAuthenticated = principal.Identity?.IsAuthenticated == true;
Expand All @@ -49,6 +50,7 @@ public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
transformed.AddIdentity(new ClaimsIdentity(claims));
return Task.FromResult(transformed);
}
#pragma warning restore PS0018

// True once this transformation has stamped its sentinel claim, keeping TransformAsync
// idempotent across the repeated calls ASP.NET makes for the same principal.
Expand Down
7 changes: 0 additions & 7 deletions src/ServiceControl.Infrastructure.Metrics/.editorconfig

This file was deleted.

10 changes: 5 additions & 5 deletions src/ServiceControl.Infrastructure.Metrics/MetricsReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Start()
await Task.Delay(interval, tokenSource.Token).ConfigureAwait(false);
}
}
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
catch (OperationCanceledException) when (tokenSource.Token.IsCancellationRequested)
{
//no-op
}
Expand All @@ -54,14 +54,14 @@ void Print()
}
}

public Task Stop()
public async Task Stop(CancellationToken cancellationToken = default)
{
if (tokenSource == null)
{
return Task.CompletedTask;
return;
}
tokenSource.Cancel();
return task;
await tokenSource.CancelAsync().ConfigureAwait(false);
await task.WaitAsync(cancellationToken).ConfigureAwait(false);
}
}
}
12 changes: 4 additions & 8 deletions src/ServiceControl.Infrastructure/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
[*.cs]

# Cancellation analyzer debt. These fire because Particular.Analyzers is no longer pinned to 0.9.0.
# They are scheduled work, not accepted exceptions: remove a line once this project has no
# violations of that rule left, and never add a rule back to this list.
dotnet_diagnostic.PS0003.severity = none
# Cancellation analyzer debt, narrowed to the one file still owed. MessageActionAuditLogExtensions
# takes a Func<Task> callback that has to become Func<CancellationToken, Task>, which changes the
# lambda at a dozen controller call sites, so it moves with the controllers in Phase 2.
[Auth/MessageActionAuditLogExtensions.cs]
dotnet_diagnostic.PS0013.severity = none
dotnet_diagnostic.PS0018.severity = none
dotnet_diagnostic.PS0020.severity = none
dotnet_diagnostic.PS0021.severity = none
8 changes: 5 additions & 3 deletions src/ServiceControl.Infrastructure/AsyncTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback,
}, CancellationToken.None);
}

public async Task Stop(CancellationToken cancellationToken)
public async Task Stop(CancellationToken cancellationToken = default)
{
if (tokenSource == null)
{
Expand All @@ -87,9 +87,11 @@ public async Task Stop(CancellationToken cancellationToken)
{
await task.WaitAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
//NOOP
// The job's own cancellation is already swallowed inside the loop, so the only way out
// here is the caller giving up on waiting. tokenSource is disposed by this point, which
// is why the filter must not reach through it for a token.
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/ServiceControl.Infrastructure/IntegratedSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public static class IntegratedSetup
{
const string SetupAndRunCmd = "--setup-and-run";
const string SetupCmd = "--setup";

public static async Task<int> Run()
public static async Task<int> Run(CancellationToken cancellationToken = default)
{
// Using GetCommandLineArgs instead of the args passed into Main because GetCommandLineArgs provides the entry assembly path
var args = Environment.GetCommandLineArgs().ToList();
Expand Down Expand Up @@ -52,7 +53,7 @@ public static async Task<int> Run()
process.BeginOutputReadLine();
process.BeginErrorReadLine();

await process.WaitForExitAsync().ConfigureAwait(false);
await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);

return process.ExitCode;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ServiceControl.Infrastructure/ReadOnlyStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void CopyTo(Stream destination, int bufferSize)
destination.Write(source);
}

public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public override int Read(Span<byte> buffer)
return bytesToCopy;
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -150,13 +150,13 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken

public override void WriteByte(byte value) => throw new NotSupportedException();

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => throw new NotSupportedException();
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default) => throw new NotSupportedException();

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) => throw new NotSupportedException();

public override void Write(ReadOnlySpan<byte> buffer) => throw new NotSupportedException();

public override Task FlushAsync(CancellationToken cancellationToken) => throw new NotSupportedException();
public override Task FlushAsync(CancellationToken cancellationToken = default) => throw new NotSupportedException();

public override void Flush() => throw new NotSupportedException();

Expand Down
18 changes: 12 additions & 6 deletions src/ServiceControl.Infrastructure/Watchdog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ ILogger log
this.log = log;
}

public Task OnFailure(string failure)
public async Task OnFailure(string failure, CancellationToken cancellationToken = default)
{
reportFailure(failure);
return ensureStopped(shutdownTokenSource.Token);
using var stopTokenSource = CancellationTokenSource.CreateLinkedTokenSource(shutdownTokenSource.Token, cancellationToken);
await ensureStopped(stopTokenSource.Token).ConfigureAwait(false);
}

public Task Start(Action onFailedOnStartup, CancellationToken cancellationToken)
public Task Start(Action onFailedOnStartup, CancellationToken cancellationToken = default)
{
watchdog = Task.Run(async () =>
{
Expand All @@ -51,6 +52,10 @@ public Task Start(Action onFailedOnStartup, CancellationToken cancellationToken)

while (!shutdownTokenSource.IsCancellationRequested)
{
// Two tokens in play: the shutdown token, and a linked one that additionally trips after
// MaxStartDurationMs. Only shutdown means "stop watching"; the timeout is a startup failure
// and must fall through to the Exception handler so it is reported and retried.
#pragma warning disable PS0021
try
{
// Host builder start is launching the loop. The watch dog loop task runs in isolation
Expand All @@ -65,7 +70,7 @@ public Task Start(Action onFailedOnStartup, CancellationToken cancellationToken)
clearFailure();
startup = false;
}
catch (OperationCanceledException e) when (shutdownTokenSource.IsCancellationRequested)
catch (OperationCanceledException e) when (shutdownTokenSource.Token.IsCancellationRequested)
{
log.LogDebug(e, "Cancelled");
return;
Expand All @@ -83,11 +88,12 @@ public Task Start(Action onFailedOnStartup, CancellationToken cancellationToken)

log.LogError(e, "Error while trying to start {TaskName}. Starting will be retried in {TimeToWaitBetweenStartupAttempts}", taskName, timeToWaitBetweenStartupAttempts);
}
#pragma warning restore PS0021
try
{
await Task.Delay(timeToWaitBetweenStartupAttempts, shutdownTokenSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException) when (shutdownTokenSource.IsCancellationRequested)
catch (OperationCanceledException) when (shutdownTokenSource.Token.IsCancellationRequested)
{
//Ignore, no need to log cancellation of delay
}
Expand All @@ -97,7 +103,7 @@ public Task Start(Action onFailedOnStartup, CancellationToken cancellationToken)
return Task.CompletedTask;
}

public async Task Stop(CancellationToken cancellationToken)
public async Task Stop(CancellationToken cancellationToken = default)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceControl/Infrastructure/Api/AuditCountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class AuditCountApi(GetAuditCountsForEndpointApi auditCountsForEndpointApi) : IAuditCountApi
{
public async Task<IList<AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken token) =>
public async Task<IList<AuditCount>> GetEndpointAuditCounts(string endpoint, CancellationToken cancellationToken) =>
(await auditCountsForEndpointApi.Execute(new AuditCountsForEndpointContext(new PagingInfo(), endpoint),
$"/api/endpoints/{endpoint}/audit-count")).Results;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
{
try
{
await reporter.Stop();
await reporter.Stop(cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
Expand Down
Loading