| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging |
| 6 import time |
| 7 import traceback |
| 8 |
| 9 from appengine_wrappers import DeadlineExceededError, logservice |
| 10 from branch_utility import BranchUtility |
| 11 from render_servlet import RenderServlet |
| 12 from server_instance import ServerInstance |
| 13 from servlet import Servlet, Request, Response |
| 14 import svn_constants |
| 15 |
| 16 class CronServlet(Servlet): |
| 17 '''Servlet which runs a cron job. |
| 18 ''' |
| 19 def Get(self): |
| 20 # Crons often time out, and when they do *and* then eventually try to |
| 21 # flush logs they die. Turn off autoflush and manually do so at the end. |
| 22 logservice.AUTOFLUSH_ENABLED = False |
| 23 try: |
| 24 return self._GetImpl() |
| 25 finally: |
| 26 logservice.flush() |
| 27 |
| 28 def _GetImpl(self): |
| 29 # Cron strategy: |
| 30 # |
| 31 # Find all public template files and static files, and render them. Most of |
| 32 # the time these won't have changed since the last cron run, so it's a |
| 33 # little wasteful, but hopefully rendering is really fast (if it isn't we |
| 34 # have a problem). |
| 35 channel = self._request.path.strip('/') |
| 36 logging.info('cron/%s: starting' % channel) |
| 37 |
| 38 server_instance = ServerInstance.CreateOnline(channel) |
| 39 |
| 40 def run_cron_for_dir(d, path_prefix=''): |
| 41 success = True |
| 42 start_time = time.time() |
| 43 files = [f for f in server_instance.content_cache.GetFromFileListing(d) |
| 44 if not f.endswith('/')] |
| 45 logging.info('cron/%s: rendering %s files from %s...' % ( |
| 46 channel, len(files), d)) |
| 47 try: |
| 48 for i, f in enumerate(files): |
| 49 error = None |
| 50 path = '%s%s' % (path_prefix, f) |
| 51 try: |
| 52 response = RenderServlet(Request(path, self._request.headers)).Get( |
| 53 server_instance=server_instance) |
| 54 if response.status != 200: |
| 55 error = 'Got %s response' % response.status |
| 56 except DeadlineExceededError: |
| 57 logging.error( |
| 58 'cron/%s: deadline exceeded rendering %s (%s of %s): %s' % ( |
| 59 channel, path, i + 1, len(files), traceback.format_exc())) |
| 60 raise |
| 61 except error: |
| 62 pass |
| 63 if error: |
| 64 logging.error('cron/%s: error rendering %s: %s' % ( |
| 65 channel, path, error)) |
| 66 success = False |
| 67 finally: |
| 68 logging.info('cron/%s: rendering %s files from %s took %s seconds' % ( |
| 69 channel, len(files), d, time.time() - start_time)) |
| 70 return success |
| 71 |
| 72 success = True |
| 73 for path, path_prefix in ( |
| 74 # Note: rendering the public templates will pull in all of the private |
| 75 # templates. |
| 76 (svn_constants.PUBLIC_TEMPLATE_PATH, ''), |
| 77 # Note: rendering the public templates will have pulled in the .js and |
| 78 # manifest.json files (for listing examples on the API reference pages), |
| 79 # but there are still images, CSS, etc. |
| 80 (svn_constants.STATIC_PATH, 'static/'), |
| 81 (svn_constants.EXAMPLES_PATH, 'extensions/examples/')): |
| 82 try: |
| 83 # Note: don't try to short circuit any of this stuff. We want to run |
| 84 # the cron for all the directories regardless of intermediate failures. |
| 85 success = run_cron_for_dir(path, path_prefix=path_prefix) and success |
| 86 except DeadlineExceededError: |
| 87 success = False |
| 88 break |
| 89 |
| 90 logging.info('cron/%s: finished' % channel) |
| 91 |
| 92 return (Response.Ok('Success') if success else |
| 93 Response.InternalError('Failure')) |
| OLD | NEW |