如何检查 Node.js 服务是否正在运行 - 在我的情况下是 Azurite 模拟器?

我正在开发一个应该在 Azure 中运行的 C# 应用程序。我想使用 Azurite emulator 在本地测试它。我想要实现的是:让我的测试检测 Azurite 是否正在运行,如果它没有运行,则通过一条很好的错误消息快速中止。

显然 Azurite 在 Node.js 上运行。

使用旧的 microsoft Azure 存储模拟器,我可以这样检查:

public static class AzureStorageEmulatorDetector
{
    public static bool IsRunning()
    {
        const string exePath = @"C:\Program Files (x86)\microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe";
        if (!File.Exists(exePath))
            return false;
        var processStartInfo = new ProcessStartInfo {FileName = exePath,Arguments = "status",RedirectStandardOutput = true};
        var process = new Process {StartInfo = processStartInfo};
        process.Start();
        process.WaitForExit();
        var processOutput = process.StandardOutput.ReadToEnd();
        return processOutput.Contains("IsRunning: True");
    }
}

我想用 Azurite 完成类似的事情。

我已经像这样安装了 Azurite:

npm install -g azurite

我是这样运行的:

azurite --silent --location C:\temp\Azurite --debug c:\temp\Azurite\debug.log

我注意到 Azurite 命令行应用程序没有告诉我它是否已经在运行的参数。当我从控制台启动 Azurite 时,我在任务资源管理器中没有看到任何名为“azurite”的进程或服务。所以我不知道我应该检查什么进程。

编辑:显然 Azurite 在 Node.js 上运行。确实有一个名为 node.exe 的进程在运行,但这不是充分条件。我可以查询正在运行的 Node.js 实例并让它告诉我它在做什么吗?

我使用的是 Windows。

有人知道吗?

wakin22098 回答:如何检查 Node.js 服务是否正在运行 - 在我的情况下是 Azurite 模拟器?

受到 Ivan Yang 和 this answer 评论的启发,我做到了:

private static bool IsAzuriteRunning()
{
    // If Azurite is running,it will run on localhost and listen on port 10000 and/or 10001.
    IPAddress expectedIp = new(new byte[] {127,1});
    var expectedPorts = new[] {10000,10001};

    var activeTcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();

    var relevantListeners = activeTcpListeners.Where(t =>
            expectedPorts.Contains(t.Port) &&
            t.Address.Equals(expectedIp))
        .ToList();

    return relevantListeners.Any();
}

编辑:或者,在他们的 GitHub 上查看此线程以了解其他可能性:https://github.com/Azure/Azurite/issues/734

本文链接:https://www.f2er.com/703070.html

大家都在问