Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cron job in google app engine not working

Tags:

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

like image 222
user1251323 Avatar asked Jul 28 '17 02:07

user1251323


People also ask

What is cron job in Google App Engine?

The App Engine Cron Service allows you to configure regularly scheduled tasks that operate at defined times or regular intervals. These tasks are commonly known as cron jobs.

Do CronJobs run automatically?

The cron reads the crontab (cron tables) for running predefined scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.


1 Answers

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
like image 173
Dan Cornilescu Avatar answered Sep 30 '22 00:09

Dan Cornilescu