首页 "The request failed because the instance could not start successfully"
GAE Go "The request failed because the instance could not start successfully"
我使用 Google App Engine Go 进行开发已经有一段时间了,但我已经一年没碰它了。我尝试将我的应用程序从 "go1" (1.9?) 更新到 "go111",但我目前遇到一些奇怪的错误,但没有任何关于发生了什么的解释。
错误包括:
The request failed because the instance could not start successfully
Container called exit(1).
500 Internal server error
- 等等
None 其中一些指向我代码中可能出错的任何特定行,也没有解释任何更有意义的内容...
我猜这个错误是由于我在 golang 版本之间升级造成的。我不得不将 app
包更改为 main
,向应用程序添加 main
函数,将 appengine 包更新到更新版本,更新 gsuite 应用程序,添加云编译小部件等等, 将 app.yaml 脚本从 go 更改为 auto 等
总而言之,我迷路了。 A Similar SE question yielded no good answers.
runtime: go111
handlers:
- url: /static
static_dir: static
- url: /res
static_dir: res
- url: /update
script: auto
secure: always
login: admin
- url: /.*
script: auto
secure: always
login: optional
调试控制台日志非常无用:
主文件看起来基本上是这样的:
package main
import (
"fmt"
"google.golang.org/appengine"
"html/template"
"log"
"net/http"
)
var MainTemplate *template.Template
func main() {
http.HandleFunc("/", hello)
var err error
MainTemplate, err = template.ParseFiles("html/main.html")
if err != nil {
log.Printf("ERROR! %v\n", err.Error())
panic(err)
}
log.Printf("Hello world!\n")
}
func hello(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
//................................//
MainTemplate.Execute(w, nil)
}
还有其他可能吗?
您的代码缺少启动侦听器的部分。将此添加到您的代码中:
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
好的,在一些评论的帮助下,为了让我的代码正常工作,我必须解决以下几个问题:
- 我将
init()
函数转换为 main()
,但没有添加侦听请求的方法。代码只是 运行 通过 main()
函数并且没有错误地退出,因此存在调试问题。
appengine.NewContext(r)
显然已被弃用,因此我不得不将这些语句切换为 r.Context()
。与此同时,Appengine Datastore
仍在使用 golang.org/x/net/context
而不仅仅是 context
,因此如果您想使用 RunInTransaction()
之类的东西,请不要更新您的导入,context
投射到 golang.org/x/net/context
就好了
- 如果您 follow the official examples 由 Google 提供,您很可能会 运行 陷入
textPayload: "2019/10/20 22:32:46 http: panic serving 127.0.0.1:16051: not an App Engine context
之类的错误。相反,您的代码需要类似于以下示例。
- 由于之前的
app
包现在是 main
包,请确保 app.yaml
(例如 favicon.ico
)中引用的任何文件都在相对于 main
包的正确位置(我不得不将我的移动到不同的文件夹以避免每个请求弹出错误...)。
package main
import (
"google.golang.org/appengine"
"html/template"
"net/http"
)
var MainTemplate *template.Template
func init() {
http.HandleFunc("/", hello)
MainTemplate, _= template.ParseFiles("html/main.html")
}
func main() {
appengine.Main()
}
func hello(w http.ResponseWriter, r *http.Request) {
c := r.Context()
//................................//
MainTemplate.Execute(w, nil)
}
这应该有效。 appengine.Main()
显然将您连接到使用数据存储/内存缓存/任何操作的上下文所需的所有 appengine
功能。
感谢帮助我度过第一个难关的评论者!
我使用 Google App Engine Go 进行开发已经有一段时间了,但我已经一年没碰它了。我尝试将我的应用程序从 "go1" (1.9?) 更新到 "go111",但我目前遇到一些奇怪的错误,但没有任何关于发生了什么的解释。
错误包括:
The request failed because the instance could not start successfully
Container called exit(1).
500 Internal server error
- 等等
None 其中一些指向我代码中可能出错的任何特定行,也没有解释任何更有意义的内容...
我猜这个错误是由于我在 golang 版本之间升级造成的。我不得不将 app
包更改为 main
,向应用程序添加 main
函数,将 appengine 包更新到更新版本,更新 gsuite 应用程序,添加云编译小部件等等, 将 app.yaml 脚本从 go 更改为 auto 等
总而言之,我迷路了。 A Similar SE question yielded no good answers.
runtime: go111
handlers:
- url: /static
static_dir: static
- url: /res
static_dir: res
- url: /update
script: auto
secure: always
login: admin
- url: /.*
script: auto
secure: always
login: optional
调试控制台日志非常无用:
主文件看起来基本上是这样的:
package main
import (
"fmt"
"google.golang.org/appengine"
"html/template"
"log"
"net/http"
)
var MainTemplate *template.Template
func main() {
http.HandleFunc("/", hello)
var err error
MainTemplate, err = template.ParseFiles("html/main.html")
if err != nil {
log.Printf("ERROR! %v\n", err.Error())
panic(err)
}
log.Printf("Hello world!\n")
}
func hello(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
//................................//
MainTemplate.Execute(w, nil)
}
还有其他可能吗?
您的代码缺少启动侦听器的部分。将此添加到您的代码中:
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
好的,在一些评论的帮助下,为了让我的代码正常工作,我必须解决以下几个问题:
- 我将
init()
函数转换为main()
,但没有添加侦听请求的方法。代码只是 运行 通过main()
函数并且没有错误地退出,因此存在调试问题。 appengine.NewContext(r)
显然已被弃用,因此我不得不将这些语句切换为r.Context()
。与此同时,Appengine Datastore
仍在使用golang.org/x/net/context
而不仅仅是context
,因此如果您想使用RunInTransaction()
之类的东西,请不要更新您的导入,context
投射到golang.org/x/net/context
就好了- 如果您 follow the official examples 由 Google 提供,您很可能会 运行 陷入
textPayload: "2019/10/20 22:32:46 http: panic serving 127.0.0.1:16051: not an App Engine context
之类的错误。相反,您的代码需要类似于以下示例。 - 由于之前的
app
包现在是main
包,请确保app.yaml
(例如favicon.ico
)中引用的任何文件都在相对于main
包的正确位置(我不得不将我的移动到不同的文件夹以避免每个请求弹出错误...)。
package main
import (
"google.golang.org/appengine"
"html/template"
"net/http"
)
var MainTemplate *template.Template
func init() {
http.HandleFunc("/", hello)
MainTemplate, _= template.ParseFiles("html/main.html")
}
func main() {
appengine.Main()
}
func hello(w http.ResponseWriter, r *http.Request) {
c := r.Context()
//................................//
MainTemplate.Execute(w, nil)
}
这应该有效。 appengine.Main()
显然将您连接到使用数据存储/内存缓存/任何操作的上下文所需的所有 appengine
功能。
感谢帮助我度过第一个难关的评论者!