带有 goroutines 的 Golang 守护进程不会停止执行

Golang daemon with goroutines won't stop executing

我创建了一个旨在并行使用队列的守护进程。为了测试它是否继续在后台执行,我实现了一个函数,该函数每 10 秒创建一个文件,直到它到达 X,其中 X 是我为队列配置的最大进程数。队列的参数在 config.yaml 文件中定义。

现在的问题是,即使我停止并删除守护程序,程序似乎仍在 运行 并创建文件...我已经尝试再次构建和 运行 程序,退出它,结束进程,删除文件,但似乎没有任何效果,文件不断在程序目录中创建。

您可以查看程序代码here, and the config file here。 你知道我该如何解决这个问题吗? 提前致谢!

问这样的问题时,您应该考虑将 MCVE 放在一起。

这样一来,由于问题的规模要小得多,您可能会自己解决问题。
如果没有,至少这里的人会更容易帮助你。

此代码在处理 len(queues) 次之前永远不会退出。它不是并发代码 - 全部在主体中 - 并且没有信号告诉代码停止。问题在这里:

case "run":
    // Installing the service
    installed, err := service.Install()
    logError(err, installed)
    // Starting the service
    started, err := service.Start()
    logError(err, started)
    if err == nil {
        // Creating a goroutine and executing the queue's processes in parallel
        for i := 0; i < len(queues); i++ {
            go startProcessing(queues[i])
            time.Sleep(time.Second) // Waiting for other functions to execute
        }
        select {} // To prevent the goroutine from exiting the main func
    }
    fmt.Println(started)

正如所见,select{} 行将永远存在,而 运行 永远存在! :) 最好将此 case 子句移动到他们自己的 goroutine 中,并在那里有一个退出信号,如下所示:

select {
    case <-quit:
        return
}

尽管这不是在 Go 应用程序中处理 start/stop 的最干净的方式;它只是显示问题。