python - GAE Python - 如何设置 cron 作业来启动后端任务

标签 python google-app-engine cron backend

我正在 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

我需要更改什么才能每天在后端实例上调用这些作业?

最佳答案

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

动态的

计划是:

  1. A 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

队列.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)

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

关于python - GAE Python - 如何设置 cron 作业来启动后端任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21131585/

相关文章:

python - 当项目更改时对嵌套列表中的值求和

python - 如何在 Travis CI 上构建 MacOSX 可执行文件?

linux - 服务器脚本在不应该发送电子邮件的时候不断发送电子邮件

linux - 如何在 crontab 中快速禁用单个作业

python - 获取 ndArray/list 的所有其他索引

python - 在每次更新期间迭代绘制图表

android - 两个不同的 Android 应用程序具有相同 API 后端的 GAE 端点?

firebase - Cloud Functions for Firebase 中的套接字挂断错误

python - 如何测试 Appengine 中 ReferenceProperty 的有效性?

php - 在用户定义的时间范围内执行脚本 n 分钟