Google App Engine Go Flexible EnvironmentからGo Standard Environment Go1.9(beta)へ移行
対応内容
- app.yaml変更
- アプリケーションディレクトリ構成変更
- contextの生成方法変更
- プリケーションロジックをStandard環境に変更
app.yamlをFlexibleからStandardに変更
変更前
app.yaml
#Flexible
runtime: go
env: flex
service: xxxx
automatic_scaling:
min_num_instances: 1
max_num_instances: 1
cool_down_period_sec: 180
cpu_utilization:
target_utilization: 0.6
resources:
cpu: 1
memory_gb: 0.6
disk_size_gb: 10
env_variables:
DEPLOYMENT_ENV: production
変更後
app.yaml
#Standard
application: xxxx
service: contents-api
runtime: go
api_version: go1.9
instance_class: F1
automatic_scaling:
min_idle_instances: 0
max_idle_instances: automatic # default value
min_pending_latency: 30ms # default value
max_pending_latency: automatic
max_concurrent_requests: 80
env_variables:
OAUTH2_CALLBACK: 'https://your-project-id.appspot.com/oauth2callback'
DEPLOYMENT_ENV: 'production'
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /(.*\.(gif|png|jpg))$
static_files: static/\1
upload: static/.*\.(gif|png|jpg)$
- url: /.*
script: _go_app
gcloud app deployをすると下記のエラーが発生します
ERROR: (gcloud.app.deploy) Error Response: [9] Deployment contains files that cannot be compiled: Compile failed:
2018/03/22 19:17:11 go-app-builder: Failed parsing input: package "github.com/xxxx/xxxx/vendor/go.opencensus.io/trace" cannot import internal package "go.opencensus.io/internal"
goapp deployの場合はvendorがアプリケーションのルートディレクトリにあれば問題なくdeployできるとの情報もあります。
gcloud app deployでdeployの対応方法としてアプリケーションのディレクトリ構成を変更
ディレクトリ構成変更前
$GOPATH/src/github.com/user/gae-app
|-- appengine/
| |-- app/
| | |-- lib/
| |-- lib.go
| | |-- app.yaml
| | |-- main.go
| | ...
|-- vendor/
...
ディレクトリ構成変更後
$GOPATH/src/github.com/user/gae-app
|-- appengine/
| |-- app/
| | |-- app.yaml
| | |-- main.go
| | ...
| `-- gopath/
| |-- src/github.com/user/gae-app/lib # lib への symlink|
| |-- src/github.com/labstack # vender/github/labstack への symlink
|-- lib/ # アプリケーションのコアロジック
|-- vendor/
...
deployするときはGOPATHを切り替える
$ export GOPATH/src/github.com/user/gae-app/appengine/gopath
$ cd GOPATH/src/github.com/user/gae-app/appengine/app
$ gcloud app deploy
ディレクトリ構成変更以外での対応
contextの変更
変更前
ctx := context.Background()
変更後
ctx := appengine.NewContext(r)
DefaultClientからurlfetchに変更
[ Get https://hogehoge.co.jp/: http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/ ]
INFO 2018-03-26 09:33:25,095 module.py:835] contents-crawler: "GET /?url=https%3A%2F%2Fhogehoge.co.jp%2F HTTP/1.1" 500 155
変更前
res, err = http.DefaultClient.Do(req)
変更後
client := urlfetch.Client(ctx)