| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging | 5 import logging |
| 6 import time | 6 import time |
| 7 import traceback | 7 import traceback |
| 8 | 8 |
| 9 from appengine_wrappers import DeadlineExceededError, logservice | 9 from appengine_wrappers import DeadlineExceededError, logservice |
| 10 from branch_utility import BranchUtility | 10 from branch_utility import BranchUtility |
| 11 from render_servlet import RenderServlet | 11 from render_servlet import RenderServlet |
| 12 from server_instance import ServerInstance | 12 from server_instance import ServerInstance |
| 13 from servlet import Servlet, Request, Response | 13 from servlet import Servlet, Request, Response |
| 14 import svn_constants | 14 import svn_constants |
| 15 | 15 |
| 16 class CronServlet(Servlet): | 16 class CronServlet(Servlet): |
| 17 '''Servlet which runs a cron job. | 17 '''Servlet which runs a cron job. |
| 18 ''' | 18 ''' |
| 19 def Get(self): | 19 def Get(self, server_instance=None): |
| 20 # Crons often time out, and when they do *and* then eventually try to | 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. | 21 # flush logs they die. Turn off autoflush and manually do so at the end. |
| 22 logservice.AUTOFLUSH_ENABLED = False | 22 logservice.AUTOFLUSH_ENABLED = False |
| 23 try: | 23 try: |
| 24 return self._GetImpl() | 24 return self._GetImpl(server_instance) |
| 25 finally: | 25 finally: |
| 26 logservice.flush() | 26 logservice.flush() |
| 27 | 27 |
| 28 def _GetImpl(self): | 28 def _GetImpl(self, server_instance): |
| 29 # Cron strategy: | 29 # Cron strategy: |
| 30 # | 30 # |
| 31 # Find all public template files and static files, and render them. Most of | 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 | 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 | 33 # little wasteful, but hopefully rendering is really fast (if it isn't we |
| 34 # have a problem). | 34 # have a problem). |
| 35 channel = self._request.path.strip('/') | 35 channel = self._request.path.strip('/') |
| 36 logging.info('cron/%s: starting' % channel) | 36 logging.info('cron/%s: starting' % channel) |
| 37 | 37 |
| 38 server_instance = ServerInstance.CreateOnline(channel) | 38 if server_instance is None: |
| 39 server_instance = ServerInstance.CreateOnline(channel) |
| 39 | 40 |
| 40 def run_cron_for_dir(d, path_prefix=''): | 41 def run_cron_for_dir(d, path_prefix=''): |
| 41 success = True | 42 success = True |
| 42 start_time = time.time() | 43 start_time = time.time() |
| 43 files = [f for f in server_instance.content_cache.GetFromFileListing(d) | 44 files = [f for f in server_instance.content_cache.GetFromFileListing(d) |
| 44 if not f.endswith('/')] | 45 if not f.endswith('/')] |
| 45 logging.info('cron/%s: rendering %s files from %s...' % ( | 46 logging.info('cron/%s: rendering %s files from %s...' % ( |
| 46 channel, len(files), d)) | 47 channel, len(files), d)) |
| 47 try: | 48 try: |
| 48 for i, f in enumerate(files): | 49 for i, f in enumerate(files): |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 # the cron for all the directories regardless of intermediate failures. | 85 # the cron for all the directories regardless of intermediate failures. |
| 85 success = run_cron_for_dir(path, path_prefix=path_prefix) and success | 86 success = run_cron_for_dir(path, path_prefix=path_prefix) and success |
| 86 except DeadlineExceededError: | 87 except DeadlineExceededError: |
| 87 success = False | 88 success = False |
| 88 break | 89 break |
| 89 | 90 |
| 90 logging.info('cron/%s: finished' % channel) | 91 logging.info('cron/%s: finished' % channel) |
| 91 | 92 |
| 92 return (Response.Ok('Success') if success else | 93 return (Response.Ok('Success') if success else |
| 93 Response.InternalError('Failure')) | 94 Response.InternalError('Failure')) |
| OLD | NEW |