【问题标题】:GAE Python - How to set a cron job to launch a backend taskGAE Python - 如何设置 cron 作业以启动后端任务
【发布时间】:2014-02-03 13:51:31
【问题描述】:

我正在 GAE 上运行一项每日报告任务,该任务最近使用了太多内存无法完成。因此,我想将其设置为后端任务。我已将后端设置如下:

backends:
- name: reporting
  class: B4_1G
  options: dynamic
  start: reporting.app

在reporting.py 中定义了许多类,它们调用不同的报告。我的 cron.yaml 目前看起来像这样:

cron:
- description: update report 1
  url: /reports/report1
  schedule: every day 03:00
- description: update report 2
  url: /reports/report2
  schedule: every day 03:30

但是从逻辑上讲,这只是通过 app.yaml 调用前端实例上的作业,目前看起来像这样:

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

handlers:
- url: /(robots\.txt)
  static_files: \1
  upload: (robots\.txt)
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico
- url: /sitemap\.xml
  static_files: sitemap.xml
  upload: sitemap\.xml
- url: /images
  static_dir: images
- url: /js
  static_dir: js
- url: /css
  static_dir: css
- url: /reports/.*
  script: reporting.app
  login: admin

每天在后端实例上调用这些作业需要进行哪些更改?

【问题讨论】:

    标签: python google-app-engine cron backend


    【解决方案1】:

    取决于您是否需要persistent or dynamic 后端

    动态的

    计划是:

    1. cron 在特定时间触发。

    2. 添加将启动后端的task on a queue

    3. 后端启动

    例子:

    app.yaml:

    - url: /crons/startgooglepluscrawler/
      script: crons.startgooglepluscrawler.app
      login: admin
    

    后端.yaml:

    backends: 
    - name: google-plus-crawler
      class: B2
      start: backends.googlepluscrawler.app
      options: dynamic, failfast
      instances: 1
    

    crons.yaml:

    cron:
    - description: get daily google plus user followers and followings
      url: /crons/startgooglepluscrawler/
      schedule: every day 09:00
    

    queue.yaml:

    total_storage_limit: 10M
    queue:
    - name: google-plus-daily-crawling
      rate: 1/s
      retry_parameters:
        task_retry_limit: 0
        task_age_limit: 1s
    

    在 startgooglepluscrawler.app 上,您需要使用任务队列启动后端:

    class StartGooglePlusCrawlerHandler(webapp2.RequestHandler):
    
        def get(self):
            logging.info("Running daily Cron")
            taskqueue.add(queue_name = "google-plus-daily-crawling",
                        url="/_ah/start",
                        method='GET',
                        target=(None if self.is_dev_server() else 'google-plus-crawler'),
                        headers={"X-AppEngine-FailFast":"true"}
                        )
            logging.info("Daily Cron finished")
    
        def is_dev_server(self):
            return os.environ['SERVER_SOFTWARE'].startswith('Dev')
    
    
    app = webapp2.WSGIApplication([
            ("/crons/startgooglepluscrawler/",StartGooglePlusCrawlerHandler)
    
        ],debug=True)
    

    backends/googlepluscrawler.py 就像一个应用程序一样,并且是/_ah/start 的处理程序:

    app = webapp2.WSGIApplication(
                [('/_ah/start', StartHandler)],
                debug=True,
                config=config.config)
    

    上面的例子将启动后端实例。

    【讨论】:

    • 非常感谢 Jimmy,这很有帮助!我不确定最后一步。所以现在我的后端被激活了,但是我如何让它从实际的应用程序文件中运行适当的类呢?
    • @Vincent 它被队列激活。在这里查看developers.google.com/appengine/docs/python/taskqueue/…
    • 谢谢吉米。顺便说一句,我只是在检查模块的实现,就我目前的理解而言,这可以使这变得更加简单。
    • @Vincent 当然。我没有使用过新模块。自应用引擎早期以来,我一直使用我提出的方式。可能有更简单的方法。也许有人可以回答得更好,或者如果您设法让它更容易,请不要忘记将其发布为答案。
    • 如果我这周弄明白的话,一定会告诉你的。它看起来相当简单。你的建议仍然很有帮助。谢谢!
    【解决方案2】:

    更简单的方法是将应用程序迁移到模块。在这里解释:https://developers.google.com/appengine/docs/python/modules/

    完成后,您只需在 cron.yaml 中添加以下行:

    target: yourmodule
    

    这允许 cron 作业在 yourmodule.yaml 中定义的实例上运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-16
      • 2012-06-08
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多