fix(workers): capture worker stderr/stdout, forward it to the gateway log, and attach the stderr tail to early-exit StartupFailed (WRK-002, #259)
ci / windows-x86 (push) Successful in 3m50s
ci / java (push) Failing after 10m14s
ci / portable (push) Failing after 19m55s
ci / windows-x86 (push) Successful in 3m50s
ci / java (push) Failing after 10m14s
ci / portable (push) Failing after 19m55s
The worker was started with CreateNoWindow and no redirection, so its log lines went to a hidden console of its own and were lost under both a console run and the NSSM service (verified on windev). The launcher now redirects both streams and WorkerOutputCapture drains them: each line is forwarded under the ZB.MOM.WW.MxGateway.Worker category at the worker's own level, the session nonce is scrubbed, and the last 50 lines / 4 KB of stderr are kept. The drain never waits on the logger (bounded drop-oldest queue) and ends at EOF when the worker exits. A worker that exits before connecting (session factory race) or that the probe finds already exited now fails StartupFailed with the stderr tail.
This commit is contained in:
@@ -63,6 +63,13 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
private readonly WorkerOptions _workerOptions;
|
||||
private readonly AlarmsOptions _alarmsOptions;
|
||||
private readonly ILogger<WorkerProcessLauncher> _logger;
|
||||
private readonly ILogger _workerOutputLogger;
|
||||
|
||||
/// <summary>
|
||||
/// How long a startup failure waits for the exited worker's stderr to reach end-of-stream, so
|
||||
/// its last lines make it into the failure message.
|
||||
/// </summary>
|
||||
internal static readonly TimeSpan StandardErrorDrainWait = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the worker process launcher with gateway options and dependencies.
|
||||
@@ -73,13 +80,19 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
/// <param name="metrics">Gateway metrics collector.</param>
|
||||
/// <param name="logger">Optional logger for diagnostic output.</param>
|
||||
/// <param name="timeProvider">Optional time provider for timestamps.</param>
|
||||
/// <param name="loggerFactory">
|
||||
/// Optional logger factory for the forwarded worker stderr/stdout lines (category
|
||||
/// <see cref="WorkerOutputCapture.LogCategory"/>). Without it the lines are still drained and
|
||||
/// kept for startup-failure messages, but not logged.
|
||||
/// </param>
|
||||
public WorkerProcessLauncher(
|
||||
IOptions<GatewayOptions> gatewayOptions,
|
||||
IWorkerProcessFactory processFactory,
|
||||
IWorkerStartupProbe startupProbe,
|
||||
GatewayMetrics metrics,
|
||||
ILogger<WorkerProcessLauncher>? logger = null,
|
||||
TimeProvider? timeProvider = null)
|
||||
TimeProvider? timeProvider = null,
|
||||
ILoggerFactory? loggerFactory = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(gatewayOptions);
|
||||
ArgumentNullException.ThrowIfNull(processFactory);
|
||||
@@ -93,6 +106,7 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
_metrics = metrics;
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_logger = logger ?? NullLogger<WorkerProcessLauncher>.Instance;
|
||||
_workerOutputLogger = loggerFactory?.CreateLogger(WorkerOutputCapture.LogCategory) ?? NullLogger.Instance;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -133,6 +147,14 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
exception);
|
||||
}
|
||||
|
||||
// Started before anything else so the worker can never block on a full stderr pipe. It
|
||||
// runs until the worker exits, whichever way this launch ends.
|
||||
WorkerOutputCapture? outputCapture = WorkerOutputCapture.StartIfRedirected(
|
||||
process,
|
||||
request.SessionId,
|
||||
request.Nonce,
|
||||
_workerOutputLogger);
|
||||
|
||||
try
|
||||
{
|
||||
using CancellationTokenSource startupTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
@@ -144,7 +166,7 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
.WaitUntilReadyAsync(process, request, startupTimeout.Token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return new WorkerProcessHandle(process, commandLine, startedAt);
|
||||
return new WorkerProcessHandle(process, commandLine, startedAt, outputCapture);
|
||||
}
|
||||
catch (OperationCanceledException exception) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
@@ -167,9 +189,23 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
"Worker process failed during startup.",
|
||||
exception);
|
||||
}
|
||||
catch (WorkerProcessLaunchException)
|
||||
catch (WorkerProcessLaunchException exception)
|
||||
{
|
||||
bool exited = HasExitedSafely(process);
|
||||
KillAndDispose(process, "StartupFailed");
|
||||
if (outputCapture is not null
|
||||
&& exited
|
||||
&& exception.ErrorCode == WorkerProcessLaunchErrorCode.StartupFailed)
|
||||
{
|
||||
// The probe saw the worker already gone: say what it wrote on the way out.
|
||||
await outputCapture.WaitForStandardErrorDrainedAsync(StandardErrorDrainWait).ConfigureAwait(false);
|
||||
string message = WorkerOutputCapture.AppendStandardErrorTail(outputCapture, exception.Message);
|
||||
if (!ReferenceEquals(message, exception.Message))
|
||||
{
|
||||
throw new WorkerProcessLaunchException(exception.ErrorCode, message, exception);
|
||||
}
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -197,6 +233,14 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
ErrorDialog = false,
|
||||
|
||||
// The worker logs to stderr. Without redirection CreateNoWindow gives it a hidden
|
||||
// console of its own and every line is lost; redirected, WorkerOutputCapture forwards
|
||||
// the lines to the gateway log and keeps a tail for startup failures (#259). Stdout is
|
||||
// redirected too, so a stray write cannot land in the gateway's own stdout (the NSSM
|
||||
// stdout log) now that the child's standard handles are set explicitly.
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
};
|
||||
|
||||
foreach (string argument in arguments)
|
||||
@@ -324,6 +368,18 @@ public sealed class WorkerProcessLauncher : IWorkerProcessLauncher
|
||||
}
|
||||
}
|
||||
|
||||
private static bool HasExitedSafely(IWorkerProcess process)
|
||||
{
|
||||
try
|
||||
{
|
||||
return process.HasExited;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static int? SafeProcessId(IWorkerProcess process)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user