2016-06-14 88 views
1

我已经创建了具有消费队列并行目标的一个守护进程。为了测试它是否在后台继续执行,我实现了一个函数,该函数每隔10秒创建一个文件,直到达到X,其中X是我为队列配置的最大进程数。队列的参数在config.yaml文件中定义。Golang守护进程够程不会停止执行

现在的问题是,即使我停止并删除后台程序,似乎该程序保持运行和创建文件......我再次尝试构建和运行该程序,退出它,结束进程,删除文件,但似乎没有任何工作,文件不断创建在程序目录中。

您可以检查程序代码here和配置文件here。 你有什么想法我可以如何解决这个问题? 在此先感谢!

回答

1

此代码将永远不会退出,直到它为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{}行会坐在那里跑,直到永远! :)最好是有移动这种情况下的条款,到他们自己够程,并有一个退出信号是这样的:

select { 
    case <-quit: 
     return 
} 

虽然这不是处理开始在Go应用程序/停止最彻底的方法;它只是表明了问题。

0

当问这样你应该考虑组建一个MCVE问题。

这样,因为这个问题是尺寸小了很多,你可以找出你自己的问题。
如果没有,至少这里的人会更容易帮助你。