dprxj1995 2017-02-09 15: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 15: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 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 GPTs营销指令提示词和创建方案
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)