これを検索して見て数日後、私は正しい答えを見つけることができませんでした。Google App Engine(Python)でCron Jobを実行できません
Google App Engine(Pythonを使用)でcronジョブを実行しようとしています。 cronの仕事そのものはそれほど重要ではありません。私はちょうど毎分Pythonのスクリプトを実行しようとしています。現時点では、別のテキストファイル(test.txt)に行を追加しようとしています。
私はハンドラの概念をあまり理解していないと確信しています。それが問題の原因です。しかし、私はドキュメンテーションで数時間を費やしましたが、私はまだそれを理解することはできません。
私はcronジョブ用のスクリプトとしてmain.pyを使用すべきではないと感じましたが、URLがcron.yamlにある必要があり、ハンドラ/スクリプトはどのようなものであるべきかを理解するのは難しいです。
助けてください!
app.yamlを
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3
handlers:
- url: /main
script: main.py
cron.yaml
cron:
- description : most recent test
url : /main
schedule: every 1 minutes
main.py
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START app]
import logging
from datetime import datetime
current_time = str(datetime.now())
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello World! The time is ' + current_time
message = "this worked, as of " + current_time + "\n"
with open("test.txt", "a") as myfile:
myfile.write(message)
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
# [END app]
助けてくれてありがとう!私は変更を加えましたが、まだ動作していません。私はmain.pyのURLを変更し、main.pyのコードを追加し、text.txtに行を追加するコードがHTMLビューに戻る前に呼び出されていることを確認しました。 –