| 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 time | 5 import time |
| 6 import traceback | 6 import traceback |
| 7 | 7 |
| 8 from app_yaml_helper import AppYamlHelper | 8 from app_yaml_helper import AppYamlHelper |
| 9 from appengine_wrappers import IsDeadlineExceededError, logservice, taskqueue | 9 from appengine_wrappers import IsDeadlineExceededError, logservice, taskqueue |
| 10 from branch_utility import BranchUtility | 10 from branch_utility import BranchUtility |
| 11 from compiled_file_system import CompiledFileSystem | 11 from compiled_file_system import CompiledFileSystem |
| 12 from custom_logger import CustomLogger | 12 from custom_logger import CustomLogger |
| 13 from data_source_registry import CreateDataSources | 13 from data_source_registry import CreateDataSources |
| 14 from environment import GetAppVersion | 14 from environment import GetAppVersion |
| 15 from gcs_file_system_provider import CloudStorageFileSystemProvider | 15 from gcs_file_system_provider import CloudStorageFileSystemProvider |
| 16 from github_file_system_provider import GithubFileSystemProvider | 16 from github_file_system_provider import GithubFileSystemProvider |
| 17 from host_file_system_provider import HostFileSystemProvider | 17 from host_file_system_provider import HostFileSystemProvider |
| 18 from object_store_creator import ObjectStoreCreator | 18 from object_store_creator import ObjectStoreCreator |
| 19 from refresh_tracker import RefreshTracker |
| 19 from render_refresher import RenderRefresher | 20 from render_refresher import RenderRefresher |
| 20 from server_instance import ServerInstance | 21 from server_instance import ServerInstance |
| 21 from servlet import Servlet, Request, Response | 22 from servlet import Servlet, Request, Response |
| 22 from timer import Timer | 23 from timer import Timer |
| 23 | 24 |
| 24 | 25 |
| 25 _log = CustomLogger('cron') | 26 _log = CustomLogger('cron') |
| 26 | 27 |
| 27 | 28 |
| 28 class CronServlet(Servlet): | 29 class CronServlet(Servlet): |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 # and spin up taskqueue tasks which will refresh any cached data relevant | 78 # and spin up taskqueue tasks which will refresh any cached data relevant |
| 78 # to these assets. | 79 # to these assets. |
| 79 # | 80 # |
| 80 # TODO(rockot/kalman): At the moment examples are not actually refreshed | 81 # TODO(rockot/kalman): At the moment examples are not actually refreshed |
| 81 # because they're too slow. | 82 # because they're too slow. |
| 82 | 83 |
| 83 _log.info('starting') | 84 _log.info('starting') |
| 84 | 85 |
| 85 server_instance = self._GetSafeServerInstance() | 86 server_instance = self._GetSafeServerInstance() |
| 86 master_fs = server_instance.host_file_system_provider.GetMaster() | 87 master_fs = server_instance.host_file_system_provider.GetMaster() |
| 87 master_commit = master_fs.GetCommitID().Get() | 88 if 'commit' in self._request.arguments: |
| 89 master_commit = self._request.arguments['commit'] |
| 90 else: |
| 91 master_commit = master_fs.GetCommitID().Get() |
| 88 | 92 |
| 89 # This is the guy that would be responsible for refreshing the cache of | 93 # This is the guy that would be responsible for refreshing the cache of |
| 90 # examples. Here for posterity, hopefully it will be added to the targets | 94 # examples. Here for posterity, hopefully it will be added to the targets |
| 91 # below someday. | 95 # below someday. |
| 92 render_refresher = RenderRefresher(server_instance, self._request) | 96 render_refresher = RenderRefresher(server_instance, self._request) |
| 93 | 97 |
| 98 # Used to register a new refresh cycle keyed on |master_commit|. |
| 99 refresh_tracker = RefreshTracker(server_instance.object_store_creator) |
| 100 |
| 94 # Get the default taskqueue | 101 # Get the default taskqueue |
| 95 queue = taskqueue.Queue() | 102 queue = taskqueue.Queue() |
| 96 | 103 |
| 97 # GAE documentation specifies that it's bad to add tasks to a queue | 104 # GAE documentation specifies that it's bad to add tasks to a queue |
| 98 # within one second of purging. We wait 2 seconds, because we like | 105 # within one second of purging. We wait 2 seconds, because we like |
| 99 # to go the extra mile. | 106 # to go the extra mile. |
| 100 queue.purge() | 107 queue.purge() |
| 101 time.sleep(2) | 108 time.sleep(2) |
| 102 | 109 |
| 103 success = True | 110 success = True |
| 104 try: | 111 try: |
| 105 data_sources = CreateDataSources(server_instance) | 112 data_sources = CreateDataSources(server_instance) |
| 106 targets = (data_sources.items() + | 113 targets = (data_sources.items() + |
| 107 [('content_providers', server_instance.content_providers), | 114 [('content_providers', server_instance.content_providers), |
| 108 ('platform_bundle', server_instance.platform_bundle)]) | 115 ('platform_bundle', server_instance.platform_bundle)]) |
| 109 title = 'initializing %s parallel targets' % len(targets) | 116 title = 'initializing %s parallel targets' % len(targets) |
| 110 _log.info(title) | 117 _log.info(title) |
| 111 timer = Timer() | 118 timer = Timer() |
| 119 tasks = [] |
| 112 for name, target in targets: | 120 for name, target in targets: |
| 113 refresh_paths = target.GetRefreshPaths() | 121 refresh_paths = target.GetRefreshPaths() |
| 114 for path in refresh_paths: | 122 tasks += [('%s/%s' % (name, path)).strip('/') for path in refresh_paths] |
| 115 queue.add(taskqueue.Task(url='/_refresh/%s/%s' % (name, path), | 123 |
| 116 params={'commit': master_commit})) | 124 # Start a new refresh cycle. In order to detect the completion of a full |
| 125 # cache refresh, the RefreshServlet (which handles individual refresh |
| 126 # tasks) will mark each task complete and check the set of completed tasks |
| 127 # against the set registered here. |
| 128 refresh_tracker.StartRefresh(master_commit, tasks).Get() |
| 129 for task in tasks: |
| 130 queue.add(taskqueue.Task(url='/_refresh/%s' % task, |
| 131 params={'commit': master_commit})) |
| 132 |
| 117 _log.info('%s took %s' % (title, timer.Stop().FormatElapsed())) | 133 _log.info('%s took %s' % (title, timer.Stop().FormatElapsed())) |
| 118 except: | 134 except: |
| 119 # This should never actually happen (each cron step does its own | 135 # This should never actually happen (each cron step does its own |
| 120 # conservative error checking), so re-raise no matter what it is. | 136 # conservative error checking), so re-raise no matter what it is. |
| 121 _log.error('uncaught error: %s' % traceback.format_exc()) | 137 _log.error('uncaught error: %s' % traceback.format_exc()) |
| 122 success = False | 138 success = False |
| 123 raise | 139 raise |
| 124 finally: | 140 finally: |
| 125 _log.info('finished (%s)', 'success' if success else 'FAILED') | 141 _log.info('finished (%s)', 'success' if success else 'FAILED') |
| 126 return (Response.Ok('Success') if success else | 142 return (Response.Ok('Success') if success else |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 github_file_system_provider = self._delegate.CreateGithubFileSystemProvider( | 195 github_file_system_provider = self._delegate.CreateGithubFileSystemProvider( |
| 180 object_store_creator) | 196 object_store_creator) |
| 181 gcs_file_system_provider = self._delegate.CreateGCSFileSystemProvider( | 197 gcs_file_system_provider = self._delegate.CreateGCSFileSystemProvider( |
| 182 object_store_creator) | 198 object_store_creator) |
| 183 return ServerInstance(object_store_creator, | 199 return ServerInstance(object_store_creator, |
| 184 CompiledFileSystem.Factory(object_store_creator), | 200 CompiledFileSystem.Factory(object_store_creator), |
| 185 branch_utility, | 201 branch_utility, |
| 186 host_file_system_provider, | 202 host_file_system_provider, |
| 187 github_file_system_provider, | 203 github_file_system_provider, |
| 188 gcs_file_system_provider) | 204 gcs_file_system_provider) |
| OLD | NEW |