Skip to content

Commit 3f1ef43

Browse files
committed
Restart extractor on config change
1 parent f156431 commit 3f1ef43

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

ExtractorUtils.Test/unit/Unstable/RuntimeTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ private ExtractorRuntimeBuilder<DummyConfig, DummyExtractor> CreateMockRuntimeBu
187187
public async Task TestRuntimeRestartNewConfig()
188188
{
189189
var builder = CreateMockRuntimeBuilder();
190+
// Restart policy won't be the default "Always" in customer envs, but we should
191+
// still restart on config change.
192+
builder.RestartPolicy = ExtractorRestartPolicy.OnError;
190193

191194
using var evt = new ManualResetEventSlim(false);
192195

@@ -199,6 +202,7 @@ public async Task TestRuntimeRestartNewConfig()
199202
extractor = ext;
200203
};
201204
};
205+
202206

203207
using var source = new CancellationTokenSource();
204208

ExtractorUtils/Unstable/Runtime/Runtime.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ enum ExtractorRunResult
3636
/// The extractor crashed.
3737
/// </summary>
3838
Error,
39+
/// <summary>
40+
/// The extractor was stopped with a clean shutdown.
41+
/// But we need to restart it (possibly due to a revision change).
42+
/// </summary>
43+
RestartRequired
3944
}
4045

4146
/// <summary>
@@ -138,6 +143,7 @@ public async Task Run()
138143
}
139144
else if (result == ExtractorRunResult.CleanShutdown)
140145
{
146+
_activeLogger.LogInformation("Extractor stopped cleanly with policy {Policy}", _params.RestartPolicy);
141147
// Shut down, if the extractor is configured to only restart on error.
142148
if (_params.RestartPolicy != ExtractorRestartPolicy.Always)
143149
{
@@ -147,6 +153,11 @@ public async Task Run()
147153
// Otherwise, immediately restart.
148154
backoff = 0;
149155
}
156+
else if (result == ExtractorRunResult.RestartRequired)
157+
{
158+
_activeLogger.LogInformation("Extractor stopped cleanly with restart required, restarting with backoff");
159+
backoff = 1;
160+
}
150161

151162
if (backoff == 0)
152163
{
@@ -328,6 +339,7 @@ private async Task<ExtractorRunResult> BuildAndRunExtractor(ServiceProvider prov
328339
{
329340
using var internalTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_source.Token);
330341
TExtractor extractor;
342+
var shouldRestart = false;
331343
try
332344
{
333345
if (_params.AddMetrics)
@@ -386,6 +398,7 @@ private async Task<ExtractorRunResult> BuildAndRunExtractor(ServiceProvider prov
386398
{
387399
_activeLogger.LogInformation("Revision changed, reloading config");
388400
internalTokenSource.Cancel();
401+
shouldRestart = true;
389402
await extractorTask.ConfigureAwait(false);
390403
}
391404

@@ -394,6 +407,7 @@ private async Task<ExtractorRunResult> BuildAndRunExtractor(ServiceProvider prov
394407
{
395408
ExceptionDispatchInfo.Capture(extractorTask.Exception).Throw();
396409
}
410+
397411
}
398412
catch (OperationCanceledException) when (internalTokenSource.IsCancellationRequested)
399413
{
@@ -415,7 +429,11 @@ private async Task<ExtractorRunResult> BuildAndRunExtractor(ServiceProvider prov
415429

416430
return ExtractorRunResult.Error;
417431
}
418-
432+
if (shouldRestart)
433+
{
434+
_activeLogger.LogInformation("Extractor stopped cleanly with policy {Policy}, restart is required", _params.RestartPolicy);
435+
return ExtractorRunResult.RestartRequired;
436+
}
419437
return ExtractorRunResult.CleanShutdown;
420438
}
421439

0 commit comments

Comments
 (0)