3

I need help in implementing a simple cron job in GAE (python).

According to what I understood from appengine documentation, I made a file cron.yaml in the application root directory with the following content:

cron:
- description: blah blah
  url: /crontask
  schedule: every 1 minute

And my app.yaml file has the following content:

application: template-123
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: template-123.app

I made all my application code (cron and other parts) into a single file "template-123.py". In the code I implement cron in the following way:

class CronTask(Handler):

    def  post(self):
        i=25
        number = Birthday(day = i)
        number.put()

And I tell the code to use this class for cron by stating: ('/crontask', CronTask).

However no new entries are uploaded to the datastore (as I believe they should be). And I know that it isn't a problem with the way I'm accessing the data store because when I try to do the same thing manually (upload entries to the data store in my non-cron part of application) it returns with appropriate results.

So I need some guidance as to what might I be doing wrong or missing? Do have to make some more changes to the yaml files or add some other libraries?

2 Answers 2

8

Cron uses GET, not POST. Change your def post(self) to def get(self) (or whatever else is appropriate).

Sign up to request clarification or add additional context in comments.

Comments

2

Usually python files end in py. the handler should say

script: template-123.py

Test your cron job locally first. You should be able to access localhost:8000/_ah/admin, and click on the Cron Jobs link on the left. It should list all your cron jobs, and you should be able to test them with a link on the page. Debug on dev_appserver first.

3 Comments

He uses an example from python27 runtime. Your answer works for python 2.5. You should clarify it, especially when replying to users that just getting started with App Engine.
Ok so i did what you said and apparently there was an issue with the way i had written "schedule" in cron.yaml file. Instead of "every 1 minute" it should have been "every 1 minutes". The code is working fine now, thanks for the help! (:
oops, I missed that. Hard to catch. Glad you figured it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.