【问题标题】:How to check whether a Node.js service is running - in my case Azurite emulator?如何检查 Node.js 服务是否正在运行 - 在我的情况下是 Azurite 模拟器?
【发布时间】:2021-04-16 19:53:06
【问题描述】:

我正在开发一个应该在 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 上。

有人知道吗?

【问题讨论】:

  • 你能检查一下1000010001端口是否被127.0.0.1使用了吗?

标签: node.js azure unit-testing azure-storage-emulator azurite


【解决方案1】:

受到 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, 0, 0, 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

【讨论】:

    猜你喜欢
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2017-04-18
    相关资源
    最近更新 更多