namespace ZB.MOM.WW.MxGateway.Server.Workers;

public sealed class WorkerProcessStartedProbe : IWorkerStartupProbe
{
    /// <summary>Verifies that the worker process has started and has not exited.</summary>
    /// <param name="process">Worker process to verify.</param>
    /// <param name="request">Process launch request.</param>
    /// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
    /// <returns>Completed task if process is running.</returns>
    public Task WaitUntilReadyAsync(
        IWorkerProcess process,
        WorkerProcessLaunchRequest request,
        CancellationToken cancellationToken)
    {
        if (process.HasExited)
        {
            throw new WorkerProcessLaunchException(
                WorkerProcessLaunchErrorCode.StartupFailed,
                $"Worker process exited before startup completed with exit code {process.ExitCode}.");
        }

        return Task.CompletedTask;
    }
}
