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