Skip to content

Commit 91c84f5

Browse files
net0wellclaude
andcommitted
fix(worker): limite de processos imposto pelo worker e desligamento duplo seguro (spec 006)
- O StepWorker limita os passos simultâneos com um semáforo de MaxConcurrency: após a recuperação da conexão AMQP, o canal novo recebe entregas enquanto as antigas ainda executam (AC-8). - Os CancellationTokenSource de parada não são mais descartados: um segundo StopAsync do host não quebra o primeiro, que ainda aguarda os passos em andamento. - Testes de cron com relógio simulado alinhado ao segundo 30 (um avanço = uma ocorrência). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 67a4856 commit 91c84f5

4 files changed

Lines changed: 43 additions & 24 deletions

File tree

‎src/PythonScriptRunner.RabbitMQ/Consumers.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
namespace PythonScriptRunner.RabbitMQ;
99

1010
/// <summary>Runs one <see cref="QueueConsumer"/> for the lifetime of the host.</summary>
11-
internal abstract class ConsumerService(PsrBroker broker, ILogger logger) : IHostedService, IDisposable
11+
internal abstract class ConsumerService(PsrBroker broker, ILogger logger) : IHostedService
1212
{
13+
// Never disposed: a stop can still be running when the host disposes its services (stop called twice); a plain
14+
// CancellationTokenSource holds no unmanaged resources.
1315
private readonly CancellationTokenSource _stopping = new();
1416
private QueueConsumer? _consumer;
1517
private Task? _running;
@@ -50,8 +52,6 @@ public async Task StopAsync(CancellationToken cancellationToken)
5052
await consumer.DisposeAsync().ConfigureAwait(false);
5153
}
5254

53-
public void Dispose() => _stopping.Dispose();
54-
5555
protected abstract QueueConsumerSettings Settings();
5656

5757
protected abstract Task<DeliveryOutcome> HandleAsync(PsrDelivery delivery, CancellationToken cancellationToken);

‎src/PythonScriptRunner.Worker/StepWorker.cs‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ internal sealed class StepWorker(
2323
WorkerOptions options,
2424
WorkerMetrics metrics,
2525
TimeProvider time,
26-
ILogger<StepWorker> logger) : IHostedService, IDisposable
26+
ILogger<StepWorker> logger) : IHostedService
2727
{
28+
// Never disposed: a stop can still be running when the host disposes its services (stop called twice); a plain
29+
// CancellationTokenSource holds no unmanaged resources.
2830
private readonly CancellationTokenSource _stopping = new();
31+
32+
// The prefetch limits deliveries per channel; after a connection recovery the new channel receives more while the old
33+
// deliveries still run, so the limit of processes is enforced here too (AC-8).
34+
private readonly SemaphoreSlim _slots = new(options.MaxConcurrency, options.MaxConcurrency);
2935
private QueueConsumer? _consumer;
3036
private Task? _running;
3137

@@ -69,8 +75,6 @@ public async Task StopAsync(CancellationToken cancellationToken)
6975
await consumer.DisposeAsync().ConfigureAwait(false);
7076
}
7177

72-
public void Dispose() => _stopping.Dispose();
73-
7478
/// <param name="delivery">The message.</param>
7579
/// <param name="abandon">Cancelled when the shutdown grace period is over.</param>
7680
internal async Task<DeliveryOutcome> HandleAsync(PsrDelivery delivery, CancellationToken abandon)
@@ -96,25 +100,33 @@ internal async Task<DeliveryOutcome> HandleAsync(PsrDelivery delivery, Cancellat
96100
}
97101

98102
using var activity = StartActivity(delivery, command);
99-
var claimed = await RetryAsync("claim step", () => lifecycle.ClaimStepAsync(command, WorkerId, options.LeaseDuration, abandon), abandon)
100-
.ConfigureAwait(false);
101-
if (claimed is null)
102-
{
103-
// Duplicate, stale (already executed or recovered elsewhere) or cancelled: nothing to do.
104-
return DeliveryOutcome.Ack;
105-
}
106-
107-
RecordQueueWait(delivery);
108-
activity?.SetTag("psr.step.key", claimed.StepKey);
109-
activity?.SetTag("psr.stage.key", claimed.Stage.Key);
110-
metrics.StepStarted();
103+
await _slots.WaitAsync(abandon).ConfigureAwait(false);
111104
try
112105
{
113-
return await ExecuteAsync(claimed, abandon).ConfigureAwait(false);
106+
var claimed = await RetryAsync("claim step", () => lifecycle.ClaimStepAsync(command, WorkerId, options.LeaseDuration, abandon), abandon)
107+
.ConfigureAwait(false);
108+
if (claimed is null)
109+
{
110+
// Duplicate, stale (already executed or recovered elsewhere) or cancelled: nothing to do.
111+
return DeliveryOutcome.Ack;
112+
}
113+
114+
RecordQueueWait(delivery);
115+
activity?.SetTag("psr.step.key", claimed.StepKey);
116+
activity?.SetTag("psr.stage.key", claimed.Stage.Key);
117+
metrics.StepStarted();
118+
try
119+
{
120+
return await ExecuteAsync(claimed, abandon).ConfigureAwait(false);
121+
}
122+
finally
123+
{
124+
metrics.StepEnded();
125+
}
114126
}
115127
finally
116128
{
117-
metrics.StepEnded();
129+
_slots.Release();
118130
}
119131
}
120132

‎tests/PythonScriptRunner.Worker.Tests/SchedulerTests.cs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public sealed class SchedulerTests(PostgresFixture postgres, BrokerFixture broke
1111
[Fact]
1212
public async Task Spec006_AC10_TwoSchedulers_FireEachOccurrenceOnce()
1313
{
14-
var time = new FakeTimeProvider(DateTimeOffset.UtcNow);
14+
var time = new FakeTimeProvider(HalfPastTheMinute());
1515
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python, time);
1616
var first = await env.StartHostAsync(s => s.Scheduler = true);
1717
await env.StartHostAsync(s => s.Scheduler = true);
@@ -56,7 +56,7 @@ public async Task Spec006_AC11_MissedOccurrences_BecomeOneRecoveryRun()
5656
[Fact]
5757
public async Task Spec006_AC12_LatestVersion_FollowsPublicationsAndPinnedVersionDoesNot()
5858
{
59-
var time = new FakeTimeProvider(DateTimeOffset.UtcNow);
59+
var time = new FakeTimeProvider(HalfPastTheMinute());
6060
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python, time);
6161
var host = await env.StartHostAsync(s => s.Scheduler = true);
6262
var latest = await CreateEchoScheduleAsync(host, "*/1 * * * *");
@@ -145,7 +145,7 @@ await Should.ThrowAsync<InputValidationException>(() =>
145145
[Fact]
146146
public async Task NewRequiredInput_WarnsOnPublicationAndRejectsTheFiringButKeepsTheSchedule()
147147
{
148-
var time = new FakeTimeProvider(DateTimeOffset.UtcNow);
148+
var time = new FakeTimeProvider(HalfPastTheMinute());
149149
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python, time);
150150
var host = await env.StartHostAsync(s => s.Scheduler = true);
151151
var schedule = await CreateEchoScheduleAsync(host, "*/1 * * * *");
@@ -167,6 +167,13 @@ public async Task NewRequiredInput_WarnsOnPublicationAndRejectsTheFiringButKeeps
167167
(await CountRunsAsync(env, schedule)).ShouldBe(0);
168168
}
169169

170+
/// <summary>Now, moved to second 30 of its minute: each advance of one minute then crosses exactly one occurrence of "*/1".</summary>
171+
private static DateTimeOffset HalfPastTheMinute()
172+
{
173+
var now = DateTimeOffset.UtcNow;
174+
return now.AddTicks(-(now.Ticks % TimeSpan.TicksPerMinute)).AddSeconds(30);
175+
}
176+
170177
private static ScheduleData EchoSchedule(string cron) => new() { Name = "eco", Cron = cron, Inputs = new JsonObject { ["texto"] = "agendado" } };
171178

172179
private static async Task PublishEchoPipelineAsync(PsrHost host)

‎tests/PythonScriptRunner.Worker.Tests/StepExecutionTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public async Task Spec006_AC08_MaxConcurrency_LimitsTheProcessesOfAWorker()
161161
}
162162

163163
var peak = attempts.Max(a => attempts.Count(other => other.StartedAt < a.FinishedAt && a.StartedAt < other.FinishedAt));
164-
peak.ShouldBe(2);
164+
peak.ShouldBe(2, string.Join(" | ", attempts.Select(a => $"{a.StartedAt:HH:mm:ss.ffffff}-{a.FinishedAt:HH:mm:ss.ffffff} {a.WorkerId}")));
165165
}
166166

167167
[Fact]

0 commit comments

Comments
 (0)