GAE Go“请求失败,因为实例无法成功启动”

时间:2019-10-20 04:06:23

标签: google-app-engine go google-cloud-platform runtime

我已经使用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

这些都不是指向我代码中任何特定行的地方,这些地方会出问题,也无法解释更有意义的内容...

我猜测错误是由于我在golang版本之间升级而引起的。我必须将app软件包更改为main,向应用程序添加main函数,将appengine软件包更新为较新的版本,更新gsuite应用程序,添加一个云编译小部件,将app.yaml脚本从“自动”更改为“等”。

总而言之,我迷路了。 A Similar SE question yielded no good answersSomeone else suggested app.yaml might be at fault, so here is mine:

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)
}

还有其他可能吗?

2 个答案:

答案 0 :(得分:1)

好的,在获得一些评论的帮助之后,以下是我必须修复的一些问题,以使我的代码可以正常工作:

  1. 我将自己的init()函数转换为main(),而没有添加监听请求的方法。该代码只是通过main()函数运行并退出而没有错误,因此出现了调试问题。
  2. appengine.NewContext(r)显然已被弃用,因此我不得不将这些语句切换为r.Context()。同时,Appengine Datastore仍在使用golang.org/x/net/context,而不仅仅是context,因此,如果您想使用RunInTransaction()之类的东西,请不要更新导入,context投放到golang.org/x/net/context很好
  3. 如果您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的错误。相反,您的代码需要看起来像下面的示例。
  4. 由于以前的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()显然将您连接到使用上下文进行数据存储/ memcache /任何操作所需的所有appengine功能。

谢谢那些帮助我度过了第一次难关的评论员!

答案 1 :(得分:0)

您的代码缺少启动侦听器的部分。将此添加到您的代码中:

$files = Get-ChildItem -File -Path C:\users\Lenovo\Desktop\Test
$dir = Get-ChildItem -Path C:\users\lenovo\Desktop\Test -Directory
foreach ($item in $files) {
    foreach ($folder in $dir)
    {
        if (($item.Name).Substring(0,3) -like $folder.Name)
        {
            Move-Item -Path $item.FullName -Destination $folder.FullName
        }
    }
}