2014-01-15 59 views
2

我在GAE上运行每日报告任务,因为最近使用的内存太多而无法完成。因此我想将其设置为后端任务。我已经设置的后端如下:GAE Python - 如何设置cron作业以启动后端任务

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 

我会得到什么改变每天在后端实例上调用这些作业?

回答

3

取决于如果你想有一个persistent or dynamic后端

对于一个动态的

的计划是:

  1. 在特定时间cron火灾。

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

  3. 后端开始

实施例:

app.yaml中:

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

backends.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) 

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

+0

多谢麦,这是非常有帮助!但我不确定最后一步。所以现在我的后端被激活了,但是如何让它从实际的应用文件中运行适当的类? – Vincent

+0

@Vincent它由队列激活。请点击https://developers.google.com/appengine/docs/python/taskqueue/overview-push#Python_Push_queues_and_backends –

+0

谢谢Jimmy。在一个侧面说明中,我只是检查模块的实现,就目前我所知,这可能使其更简单。 – Vincent