質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Google App Engine

Google App Engineは、Googleの管理するデータセンター上でウェブアプリケーションの開発が可能なクラウドコンピュータ技術です。Java、Python、Go用にSDKが用意されています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1967閲覧

GAE, python3, google cloud datastore のチュートリアルコードのサービスの動作について

big_naminami

総合スコア15

Google App Engine

Google App Engineは、Googleの管理するデータセンター上でウェブアプリケーションの開発が可能なクラウドコンピュータ技術です。Java、Python、Go用にSDKが用意されています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/05/01 18:43

#前提
↓のチュートリアルをこなしていました。GAE、python3, cloud datastore を使っている公式チュートリアルです。
https://cloud.google.com/appengine/docs/standard/python3/building-app/storing-and-retrieving-data

#解決したい疑問
ローカルで動かしたら正常に動作するにもかかわらず、GAEにデプロイ後のサービスが正常に動作せず困っております。

チュートリアルの内容を簡単に言うと
・サイトにきたユーザーのアクセス時間を記録し、cloud datastoreに貯めていく(ユーザー識別しない状態)
・貯めたdatastoreから、最新のアクセス時間10個を画面のhtmlに埋め込み、表示させる

です。

ローカルで動かしたとき(python3 main.py)は予想した通りに正常に動きました。

しかし、GAEにデプロイしたとき、予想した通りに動きませんでした。
デプロイはエラー無く出来たのですが、GAEのappにアクセスしてもcloud datastore にアクセス時間を保存できていません。
(GCPのコンソール→cloud datastoreのエンティティから確認済み)
そのため、ページの表示も更新されない状況となっています。
なぜ、cloud datastore がデプロイ後だと、保存できないのでしょうか。

何か情報お持ちの方、アドバイス頂けませんでしょうか。

*ローカルで動かしたとき(ページリロードにそって、正しく動作している)
イメージ説明aaaaaaaaaaaa

*デプロイしたサービスを動かしたとき(ページリロードしているのに正しく動作できていない)
イメージ説明

以下にチュートリアルのコードをそのまま記載いたします。
・app.yaml

yaml

1runtime: python37 2 3handlers: 4 # This configures Google App Engine to serve the files in the app's static 5 # directory. 6- url: /static 7 static_dir: static 8 9 # This handler routes all requests not caught above to your main app. It is 10 # required when static routes are defined, but can be omitted (along with 11 # the entire handlers section) when there are no static files defined. 12- url: /.* 13 script: auto

・main.py

python3

1# Copyright 2018 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import datetime 16 17from flask import Flask, render_template 18 19# [START gae_python37_datastore_store_and_fetch_times] 20from google.cloud import datastore 21 22datastore_client = datastore.Client() 23 24# [END gae_python37_datastore_store_and_fetch_times] 25app = Flask(__name__) 26 27 28# [START gae_python37_datastore_store_and_fetch_times] 29def store_time(dt): 30 entity = datastore.Entity(key=datastore_client.key('visit')) 31 entity.update({ 32 'timestamp': dt 33 }) 34 35 datastore_client.put(entity) 36 37 38def fetch_times(limit): 39 query = datastore_client.query(kind='visit') 40 query.order = ['-timestamp'] 41 42 times = query.fetch(limit=limit) 43 44 return times 45# [END gae_python37_datastore_store_and_fetch_times] 46 47 48# [START gae_python37_datastore_render_times] 49@app.route('/') 50def root(): 51 # Store the current access time in Datastore. 52 store_time(datetime.datetime.now()) 53 54 # Fetch the most recent 10 access times from Datastore. 55 times = fetch_times(10) 56 print("this is %s" % type(times)) 57 58 return render_template( 59 'index.html', times=times) 60# [END gae_python37_datastore_render_times] 61 62 63if __name__ == '__main__': 64 # This is used when running locally only. When deploying to Google App 65 # Engine, a webserver process such as Gunicorn will serve the app. This 66 # can be configured by adding an `entrypoint` to app.yaml. 67 68 # Flask's development server will automatically serve static files in 69 # the "static" directory. See: 70 # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed, 71 # App Engine itself will serve those files as configured in app.yaml. 72 app.run(host='127.0.0.1', port=8080, debug=True)

・requirement.txt

txt

1Flask==1.0.2 2google-cloud-datastore==1.7.3

・index.html

html

1<!doctype html> 2<html> 3<head> 4 <title>Datastore and Firebase Auth Example</title> 5 <script src="{{ url_for('static', filename='script.js') }}"></script> 6 <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> 7</head> 8<body> 9 10 <h1>Datastore and Firebase Auth Example</h1> 11 <!-- [START gae_python37_datastore_print_times] --> 12 <h2>Last 10 visits</h2> 13 {% for time in times %} 14 <p>{{ time['timestamp'] }}</p> 15 {% endfor %} 16 <!-- [END gae_python37_datastore_print_times] --> 17</body> 18</html>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mistn

2019/05/02 06:43

GAEにデプロイしたアプリのログを見る方法はないのでしょうか。デプロイは正常に終了してもアプリ起動中になにかエラーを出している可能性もありそうです。
big_naminami

2019/05/02 09:04

mistnさん 返信ありがとうございます! chromeのディベロッパーツールを見てみたのですが、エラーは一つも出ておりませんでした、、、 GAEアプリのログ取りでググってみたところ、stackdriver loggingという機能が見つかったのでいま↓のページを参考に設定を試みております。 https://www.topgate.co.jp/gcp21-stackdriver-logging-collect-application-logs もし何かこの方法がオススメとかあれば教えて頂けると幸いです。
big_naminami

2019/05/02 17:32 編集

コメントありがとうございます! 解決いたしました! datastoreで保存していたtimestampの時刻情報の保存方法が原因でした。 なぜかは分からないですが、今の日本時刻をUTCとしてdatastoreに保存してしまっていたようです(正解は日本時間をJSTで保存です。いつの間にかJSTで保存が出来ていました)。 JST=UTC+9時間なので、JST時間をUTCで保存してしまうと、未来の時刻データとして保存されてしまいます。 そして、Webページでは直近の時刻データ10個を取ってくるという処理を行っています。 つまり、表示する10個の時刻データが誤って保存されてしまった未来の時刻データで埋まってしまって あたかも更新されていないように表示されていた、という結論でした。 (datastoreをよく見ると、中途半端に真ん中あたりにデータが挿入されていました。) データ保存は成功していたので、gcloud app logs read 試してみましたが結局エラーは出ず、 stackdriver logging を試してみてもエラーは出ませんでした。 (偶然ですが、私が調べた限りでは、gcloud app logs readとstackdriver logging は 内容では同じっぽい感じでした。情報共有まで。) 誠にありがとうございました!
mistn

2019/05/03 00:25

GAEのリージョンが海外だとタイムゾーンが日本じゃなくなってたのかもしれませんね。 ともかく解決できたようでよかったです。
guest

回答1

0

自己解決

datastoreで保存していたtimestampの時刻情報の保存方法が原因でした。

なぜかは分からないですが、今の日本時刻をUTCとしてdatastoreに保存してしまっていたようです(正解は日本時間をJSTで保存です。いつの間にかJSTで保存が出来ていました)。
JST=UTC+9時間なので、JST時間をUTCで保存してしまうと、未来の時刻データとして保存されてしまいます。
そして、Webページでは直近の時刻データ10個を取ってくるという処理を行っています。
つまり、表示する10個の時刻データが誤って保存されてしまった未来の時刻データで埋まってしまって
あたかも更新されていないように表示されていた、という結論でした。
(datastoreをよく見ると、中途半端に真ん中あたりにデータが挿入されていました。)

データ保存は成功していたので、gcloud app logs read 試してみましたが結局エラーは出ず、
stackdriver logging を試してみてもエラーは出ませんでした。
(偶然ですが、私が調べた限りでは、gcloud app logs readとstackdriver logging は
内容では同じっぽい感じでした。情報共有まで。)

投稿2019/05/02 17:32

big_naminami

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問