【问题标题】:Python Google App Engine Cron Job/Scheduled task Not WorkingPython Google App Engine Cron 作业/计划任务不工作
【发布时间】:2012-08-31 02:55:24
【问题描述】:

我正在创建这个 cron 作业来获取 twitter 提要并将其存储在数据存储中。我尝试了一切,但我的 cronjob 无法正常工作。我阅读了以下文章/教程和一些 stackoverflow 问题,但我无法解决这个问题。

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

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

这是我的代码

这是 cron.ymal

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

这是我完成这项工作所需的课程。

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!')

这是 app.ymal

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

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

- url: /twittertask
  script: twittertask.app

这是 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()

嗯,我找不到错误在哪里。我在我的开发服务器上进行了测试。我没有将它上传到谷歌应用引擎。

【问题讨论】:

    标签: python google-app-engine cron python-2.7


    【解决方案1】:

    问题出在您的 app.yaml 中。 URL 从上到下匹配,但您的第一个处理程序匹配 所有 URL。移动 twittertask 条目,使其位于 handlers 下的第一个。

    【讨论】:

    • 但即使我在开发服务器中更改了它,我也会收到 404 错误消息。
    【解决方案2】:

    Cron 在本地开发服务器上不起作用。将其上传到云端,它就会工作。

    在本地访问您的应用服务器:

    http://127.0.0.1:8080/_ah/admin
    

    然后点击“Cron 作业”。

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

    它会说“在生产中,这将在这些时间运行:”

    您的日程安排在这里。

    【讨论】:

    • 好的,谢谢。我把它上传到谷歌应用引擎。现在我收到一个名为 2012/09/06 10:28:15 on time Failed 的错误
    • 让它运行到下一个周期。我有过这种情况,然后下一次效果很好。从来没有花时间解决它,但毫无疑问,日志中会有一些东西。
    • 另外一个让定时刷新在本地工作的快速解决方案是使用 Opera 浏览器并将其设置为每 N 分钟/秒刷新一次相关页面。
    • 嗯,它应该每分钟运行一次。我等了超过5分钟。你能检查我的 cron.yal 或 app.yal 文件中是否没有错误。处理类(TwitterTask)是正确的。非常感谢!
    • 检查这一行: url : /twittertask 如果你在“url”和“:”之间有一个空格,它可能不起作用。并且描述在冒号和值之间也有一个空格。检查我的 cron.yaml 我没有空格。
    猜你喜欢
    • 2015-11-19
    • 2010-11-17
    • 1970-01-01
    • 2011-12-18
    • 2010-11-04
    • 2012-08-08
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    相关资源
    最近更新 更多