Cron In Google App Engine

Cron in google app engine is similar to cron we use in Linux. With cron we can schedule task to run at specified time automatically.

To write cron in google app engine application we basically need to edit cron.yaml app.yaml and the main cron file with the code.

cron.yaml has the following format:
cron:
- description: daily summary job
  url: /tasks/summary
  schedule: every 24 hours
  timezone: Australia/NSW
  target: version-2
description is just a description to your job. (optional)
url is what will run.
schedule is when the job will run.
timezone if provided, the job will run at the schedule of that time zone otherwise according to UTC (optional)
target is the application version target you want the job to run. (optional)

You can use any of the following format in schedule option to run your job.
every 12 hours
every 5 minutes from 10:00 to 14:00
2nd,third mon,wed,thu of march 17:00
every monday 09:00
1st monday of sep,oct,nov 17:00
every day 00:00
If you want it to run on regular interval you can use:

every N (hours|mins|minutes) ["from" (time) "to" (time)]
For eg:
every 2 hours from 10:00 to 14:00
every 2 hours synchronized
("every"|ordinal) (days) ["of" (monthspec)] (time)
After setting up cron.yaml you need to setup app.yaml.
Add your url just like any other page.
For eg:
handlers:
- url: /task/daily
script: work.py
login: admin
You can also add login:admin as shown in the above example to restrict the task to only admin and prevent it from normal user running it. Furthermore in your code you can check for the header X-AppEngine-Cron: true to see if the url is run by cron engine.

So in conclusion:

in cron.yaml

cron:
  - url: /tasks/daily
    schedule: every 24 hours

in app.yaml

handlers:
- url: /task/daily
script: work.py

in work.py
#your python code

Comments

Popular posts from this blog

Perm Root HTC Desire Gingerbread 2.3.3

[Solved] invalid partition table on /dev/sda -- wrong signature 0.

Adobe Stand Alone Flash Player for Linux