cron job in google app engine not working

ぐ巨炮叔叔 提交于 2020-01-09 11:43:25

问题


I have taken the basic python 3 tutorial website on flask from this google cloud tutorial and I am able to set this up and the website works just fine.

In addition , I also wanted to run a python script which runs everyday to collect some data, but the cron job just doesn't work. I also added login: admin to restrict anyone to directly use that url

cron.yaml

cron:
- description: test dispatch vs target
  url: /cronapp
  schedule: every 5 hours

app.yaml

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 3

handlers:
- url: /cronapp
  script: cronapp.py
  login: admin

I am calling this as http://myproject.appspot.com/cronapp also doesn't work and returns a 404. what am I doing wrong ? any help is appreciated


回答1:


Your app.yaml file is mixing up the standard environment Handlers element into a flexible environment configuration, so it is probably ignored. You can probably see the cron requests in the app's logs in the developer console (likely with errors, though).

You need to add a handler for /cronapp inside your app code, not in app.yaml. Not entirely sure how you do that (I'm still using only standard environment), it depends on your app and/or its framework. Take a look at the Hello World code review for a flask example.

Update:

I may not be entirely correct, I based my answer on documentation but I just noticed some inconsistencies (I sent some documentation feedback to Google for it).

The flexible environment Securing URLs for cron (which appears mostly copied from the standard environment equivalent) mentions a couple of solutions:

  • one indeed based on the login: admin option to handler:

You can restrict a URL by adding login: admin to the handler configuration section in app.yaml. For more information see Securing URLs

But handler is not mentioned in the Configuring your App with app.yaml and the Securing URLs is pointing to an inexistent tag. So I'm not sure if this is indeed working or not.

  • the second one is based on the X-Appengine-Cron header (same as in the standard environment):

Requests from the Cron Service will also contain a HTTP header:

X-Appengine-Cron: true

The X-Appengine-Cron header is set internally by Google App Engine. If your request handler finds this header it can trust that the request is a cron request. If the header is present in an external user request to your app, it is stripped, except for requests from logged in administrators of the application, who are allowed to set the header for testing purposes.

But in Removed headers it is mentioned that:

In addition, some selected headers that match the following pattern are removed from the request:

  • X-Appengine-*

It's unclear if this extends to X-Appengine-Cron or not. It's worth a try. This is my check in the (standard env, webapp2-based) cron handler code:

    if self.request.headers.get('X-AppEngine-Cron') is None:
        self.abort(403)  # HTTPForbidden


来源:https://stackoverflow.com/questions/45363766/cron-job-in-google-app-engine-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!