2017-07-12 15 views
0

これを検索して見て数日後、私は正しい答えを見つけることができませんでした。Google App Engine(Python)でCron Jobを実行できません

Google App Engine(Pythonを使用)でcronジョブを実行しようとしています。 cronの仕事そのものはそれほど重要ではありません。私はちょうど毎分Pythonのスクリプトを実行しようとしています。現時点では、別のテキストファイル(test.txt)に行を追加しようとしています。

私はハンドラの概念をあまり理解していないと確信しています。それが問題の原因です。しかし、私はドキュメンテーションで数時間を費やしましたが、私はまだそれを理解することはできません。

私はcronジョブ用のスクリプトとしてmain.pyを使用すべきではないと感じましたが、URLがcron.yamlにある必要があり、ハンドラ/スクリプトはどのようなものであるべきかを理解するのは難しいです。

助けてください!

Here's a list of my files.

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] 

答えて

1

ファイルは標準設定のHandlers要素をflexible environment構成に混ぜているため、おそらく無視されます。

あなたcron.yaml構成は、あなたのcronジョブのため/main URLを示しているが、あなたのアプリが、このようなURLパスのルートを持っていないようです、それだけで/パスを扱うようです。私はあなたが/main cron要求のログに何らかの404エラーがあると思うでしょう。

main.py/mainパスのルートを追加する必要があります。またはcron.yaml/main/に置き換えてください。

サイドノート:あなたはいくつかの非実行可能なコードを持っているhello()内(それreturn声明は以下の) - あなたはあなたが持っていない

1

...そのtest.txtファイルを探しているだけの場合には/mainのURLハンドラ。これを試してみてください:app.yamlを中

:main.pyで

handlers: 
- url: /.*  # wildcard. every url goes there 
    script: main.py 

を:

from flask import Response 

@app.route('/main') 
def main(): 
    """Return a friendly HTTP greeting.""" 
    Response("Hello main viewer", mimetype='text/plain') 

注:コード内の別のバグがスクリプトを取得する前に、HTMLビューを返すということですチャンスは.txtファイルを書くことです。それは決してそれを得ることはありません。

+0

助けてくれてありがとう!私は変更を加えましたが、まだ動作していません。私はmain.pyのURLを変更し、main.pyのコードを追加し、text.txtに行を追加するコードがHTMLビューに戻る前に呼び出されていることを確認しました。 –

関連する問題