如何为GAE cron任务解决我的deadlineexceedederror?

时间:2012-01-30 18:41:13

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

尝试通过启动任务的cron创建kml文件时,我遇到了截止日期错误。错误消息是

    2012-01-30 16:07:47.902 /createkml/ 500 5012ms 0kb AppEngine-Google; (+http://code.google.com/appengine)

    0.1.0.2 - - [30/Jan/2012:10:07:47 -0800] "POST /createkml/ HTTP/1.1" 500 0 "http://www.koolbusiness.com/createkmltask/" "AppEngine-Google; (+http://code.google.com/appengine)" "www.koolbusiness.com" ms=5013 cpu_ms=3305 api_cpu_ms=295 cpm_usd=0.091855 queue_name=default task_name=7177535986681131286 instance=00c61b117cf890b99ca52a6b9e7c5f048e72

    I 2012-01-30 16:07:42.959

    creating file

    E 2012-01-30 16:07:47.853

    ApplicationError: 5 
    Traceback (most recent call last):
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
        rv = self.handle_exception(request, response, e)
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
        rv = self.router.dispatch(request, response)
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
        return route.handler_adapter(request, response)
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
        return handler.dispatch()
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
        return self.handle_exception(e, self.app.debug)
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
        return method(*args, **kwargs)
      File "/base/data/home/apps/s~montaoproject/gralumo.356457066048686089/main.py", line 1575, in post
        result = urlfetch.fetch(url)
      File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 263, in fetch
        return rpc.get_result()
      File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
        return self.__get_result_hook(self)
      File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 371, in _get_fetch_result
        raise DeadlineExceededError(str(err))

DeadlineExceededError: ApplicationError: 5 

我的代码是

class CreateKMLTask(webapp2.RequestHandler):

    def get(self):
        logging.info('creating KML task')
        taskqueue.add(url=r'/createkml/')
        self.redirect('/')

class CreateKMLHandler(webapp2.RequestHandler):

    def post(self):

        # Create the file
        logging.info('creating file')
        file_name = \
            files.blobstore.create(mime_type='application/octet-stream')

        url = 'http://montaoproject.appspot.com/list.kml'

        result = urlfetch.fetch(url)
        if not result.content:
            return

        # Open the file and write to it
        logging.info('opening file')

        with files.open(file_name, 'a') as f:
            f.write(result.content)

        # Finalize the file. Do this before attempting to read it.

        files.finalize(file_name)

        # Get the file's blob key
        logging.info('getting blob key of file')

        blob_key = files.blobstore.get_blob_key(file_name)
        logging.info('new blob key:'+str(blob_key))
        im = Image.get_by_id(4468185)
        im.primary_image = blob_key
        im.put()
        logging.info('new image key:'+str(im.primary_image))
        logging.info('finished KML handling and put image')


    webapp2.Route(r'/createkml/', handler=CreateKMLHandler,
                  name='createkml'),
    webapp2.Route(r'/createkmltask/', handler=CreateKMLTask,
                  name='createkmltask')

我的cron.yaml:

cron:

 - description: daily mailout

   url: /report/daily

   schedule: every 24 hours

 - description: daily mailout

   url: /report/montaodaily

   schedule: every 24 hours

 - description: refresh kml

   url: /createkmltask/

   schedule: every 24 hours

我要做的是刷新KML文件到一个新的blob,它具有与以前相同的文件ID,因此调用将是相同的。我该怎么做才能解决我的超时问题?我认为它应该有效。

由于

1 个答案:

答案 0 :(得分:4)

此超时正在获取网址;你可以在堆栈跟踪中看到这一点。获取功能默认为5秒超时。

URL Fetch docs表示你可以通过将截止日期作为附加参数传递来增加超时。例如,截止日期= 60会给你60秒的超时。

所以,试试result = urlfetch.fetch(url, deadline=60)