在使用supervisord运行Golang程序时出现了无法识别的GOPATH错误。

huangapple go评论95阅读模式
英文:

Unrecognised GOPATH when running Golang program using supervisord

问题

我有一个使用Golang编写的实现Web服务器的程序。它预期会持续运行,并在出现任何意外故障或崩溃时重新启动自身。为此,我正在尝试将其配置为UNIX进程,使用supervisord。然而,我面临的问题是,代码中包含的外部Go库无法被supervisord识别,因为它无法识别GOPATH。这导致出现以下错误:

web_server.go:11:2: cannot find package "github.com/gorilla/mux" in any of:
	/usr/lib/go/src/github.com/gorilla/mux (from $GOROOT)
    ($GOPATH not set)

当使用supervisord运行Web服务器时。我的Web服务器的supervisord配置如下:

[program:web_server]
command=go run web_server.go
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=5
stderr_logfile=/home/ubuntu/err_logs/web_server.err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10

有什么解决方法吗?

英文:

I have a Golang program implementing a web server. It is expected to be running continuously, and incase of any unexpected failure or crash, to restart itself. For that, I am trying to configure it as a UNIX-process using supervisord. However, the issue I am facing is that the external go libraries included in the code aren't getting recognised as supervisord is unable to recognise the GOPATH. This is leading to errors such as:

web_server.go:11:2: cannot find package "github.com/gorilla/mux" in any of:
	/usr/lib/go/src/github.com/gorilla/mux (from $GOROOT)
    ($GOPATH not set)

when running the web server using supervisord. The supervisord configuration for my web server is:

[program:web_server]
command=go run web_server.go
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=5
stderr_logfile=/home/ubuntu/err_logs/web_server.err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10

What is the workaround for this?

答案1

得分: 4

supervisord的一个重要属性是,子进程将继承启动supervisord程序的shell的环境。supervisord本身还会在子进程的环境中设置一些环境变量,包括SUPERVISOR_ENABLED(表示该进程受supervisor控制的标志)、SUPERVISOR_PROCESS_NAME(指定该进程的配置文件中的进程名称)和SUPERVISOR_GROUP_NAME(指定子进程的配置文件中的进程组名称)。

这些环境变量可以在[supervisord]部分的environment配置选项中进行覆盖(适用于所有子进程),或者在每个[program:x]部分的环境配置选项中进行覆盖(仅适用于[program:x]部分指定的子进程)。

因此,在environment变量中添加GOPATH解决了该问题。

[program:web_server]
command=go run web_server.go
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=5
stderr_logfile=/home/ubuntu/err_logs/web_server.err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
environment=GOPATH="/home/ubuntu"

参考链接:这里

英文:

One important property of supervisord as stated [here][1], that I had been missing out is:

> Subprocesses will inherit the environment of the shell used to start
> the supervisord program. Several environment variables will be set by
> supervisord itself in the child’s environment also, including
> SUPERVISOR_ENABLED (a flag indicating the process is under supervisor
> control), SUPERVISOR_PROCESS_NAME (the config-file-specified process
> name for this process) and SUPERVISOR_GROUP_NAME (the
> config-file-specified process group name for the child process).
>
> These environment variables may be overridden within the [supervisord]
> section config option named environment (applies to all subprocesses)
> or within the per- [program:x] section environment config option
> (applies only to the subprocess specified within the [program:x]
> section).

Therefore, adding the GOPATH in the environment variable solved the issue.

[program:web_server]
command=go run web_server.go
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=5
stderr_logfile=/home/ubuntu/err_logs/web_server.err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
environment=GOPATH="/home/ubuntu"

[1]: http://supervisord.org/subprocess.html#subprocess-environment "here"

huangapple
  • 本文由 发表于 2015年12月29日 15:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/34506251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定