dprxj1995 2017-02-09 23:16
浏览 15
已采纳

Go App Engine在没有上下文的init()中获取版本

Is there a way to to get my autoscaled application's VersionID in my init() function without a Context? The only available option seems to be appengine.VersionID(context.Context). Manually scaled instances have /_ah/start called when they start up (giving access to a Context), but there is nothing like this for autoscaled instances.

I am not caring about the generated ID that appengine.VersionID returns with it, just the app.yaml version.

EDIT: A bit of context: I am wanting to deploy versions in the form x-x-x-dev or x-x-x-live and have my database connection depend on the version suffix. This way, when I look in the GCP console, I can be certain which deployed modules/services are using which database. Of course, I setup my DB connection pool in the init(), which has no access to a Context.

  • 写回答

1条回答 默认 最新

  • dpkpaxhzffixp8426 2017-02-09 23:16
    关注

    I searched and searched with no answers online anywhere, so here it is.

    Simply parse the app.yaml file in your init() function. My example here uses a yaml parsing package, but it can be done more lightweight if you need.

    import "github.com/ghodss/yaml"
    
    type AppVersion struct {
        Version string `json:"version"`
    }
    
    func VersionID() (string, error) {
        dat, err := ioutil.ReadFile("app.yaml")
        if err != nil {
            return "", err
        }
        a := &AppVersion{}
        err = yaml.Unmarshal(dat, a)
        if err != nil {
            return "", err
        }
        return a.Version, nil
    }
    

    Note that this DOES NOT return the generated ID in the form X.Y that appengine.VersionID() does. Only the X part of the version.

    As an aside, in the appengine repo on Github, the actual call to appengine.VersionID requires a Context, but internally calls the internal package with nil. So they basically force you to call it with a Context, but it isn't actually used. It's incredibly infuriating.

    EDIT: It should be noted that the new Go SDK in gcloud no longer supports version in the app.yaml, as it is now a required parameter at deploy. However, the "legacy" SDK is still supported and maintained, which I am continuing to use as of today (12/24/2018).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器