Nach ein paar Tagen der Suche und Suche danach, und ich konnte nicht die richtige Antwort finden.Kann Cron Job nicht auf Google App Engine (Python) ausführen
Ich versuche, einen Cron-Job auf Google App Engine (mit Python) auszuführen. Der Cron-Job selbst ist nicht so wichtig, ich möchte nur jede Minute ein Python-Skript ausführen. Momentan versuche ich nur eine Zeile mit der aktuellen Uhrzeit in einer separaten Textdatei (test.txt) einzufügen.
Ich bin ziemlich sicher, dass ich das Konzept der Handler nicht ganz verstehe, und das ist es, was mich Probleme verursacht. Aber ich habe Stunden in der Dokumentation verbracht, und ich kann es immer noch nicht herausfinden.
Ich habe das Gefühl, dass ich main.py nicht als Skript für den Cron-Job verwenden sollte, aber es fällt mir schwer zu verstehen, wie die URL in cron.yaml sein muss und wie das Handler/Script sein sollte.
Bitte helfen!
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]
Danke für die Hilfe!Ich habe die Änderungen vorgenommen, aber es funktioniert immer noch nicht. Ich habe die URL in main.py geändert, den Code in main.py hinzugefügt und sichergestellt, dass der Code zum Hinzufügen von Zeilen in der Datei text.txt aufgerufen wird, bevor die HTML-Ansicht zurückgegeben wird. –