3
votes

I am creating this cron job to get twitter feeds and store it in datastore. I tried everything, but my cronjob is not working. I read following articles/tutorials and some stackoverflow questions, but I can't solve this out.

https://developers.google.com/appengine/docs/python/config/cron

http://cloudartisan.com/posts/2010-06-02-scheduled-tasks-with-google-app-engine-python/

Here is my code

This is cron.ymal

cron:
- description : capture twitter feed 
  url : /twittertask
  schedule: every 1 minutes
  target: version-2

This is the class I need to do the job.

import webapp2
from google.appengine.ext import db

class Twitt(db.Model):
    created_at = db.IntegerProperty(required = True)
    id = db.StringProperty(required = True)
    text = db.StringProperty(required = True)

class TwitterTask(webapp2.RequestHandler):

    def get(self):
        url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=BeijingAir&count=10"

        json_string = urllib2.urlopen(url).read()

        data = json.loads(json_string)

        for item in data:
            created_at_item = item['created_at']
            text_item = item['text']
            id_item = item['id']

            e = Twitt(id = created_at_item, text = text_item, id = id_item)
            e.put()

        self.response.out.write('Hello prueba!')

This is app.ymal

application: cronjob
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: index.app

- url: /twittertask
  script: twittertask.app

This is index.py

import webapp2

class MainPage(webapp2.RequestHandler):
  def get(self):
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.write('Hello, Check Admin')

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

def main():

    run_wsgi_app(application)

if __name__ == "__main__":
    main()

Well, I can't find where is the error. I tested on my dev server. I didn't upload it to google app engine.

2

2 Answers

4
votes

The problem is in your app.yaml. URLs are matched from top to bottom, but your first handler matches all URLs. Move the twittertask entry so it's first under handlers.

1
votes

Cron does not work on the local dev sever. Upload it to the cloud and it'll work.

Visit your appserver locally:

http://127.0.0.1:8080/_ah/admin

and click on "Cron jobs".

http://127.0.0.1:8080/_ah/admin/cron

And it will say "In production, this would run at these times:"

your schedule here.