| 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 from appengine_wrappers import IsDevServer | 5 from appengine_wrappers import IsDevServer |
| 6 from branch_utility import BranchUtility | 6 from branch_utility import BranchUtility |
| 7 from compiled_file_system import CompiledFileSystem | 7 from compiled_file_system import CompiledFileSystem |
| 8 from empty_dir_file_system import EmptyDirFileSystem | 8 from empty_dir_file_system import EmptyDirFileSystem |
| 9 from github_file_system import GithubFileSystem | 9 from github_file_system import GithubFileSystem |
| 10 from host_file_system_creator import HostFileSystemCreator | 10 from host_file_system_provider import HostFileSystemProvider |
| 11 from third_party.json_schema_compiler.memoize import memoize | 11 from third_party.json_schema_compiler.memoize import memoize |
| 12 from render_servlet import RenderServlet | 12 from render_servlet import RenderServlet |
| 13 from object_store_creator import ObjectStoreCreator | 13 from object_store_creator import ObjectStoreCreator |
| 14 from server_instance import ServerInstance | 14 from server_instance import ServerInstance |
| 15 | 15 |
| 16 class OfflineRenderServletDelegate(RenderServlet.Delegate): | 16 class OfflineRenderServletDelegate(RenderServlet.Delegate): |
| 17 '''AppEngine instances should never need to call out to SVN. That should only | 17 '''AppEngine instances should never need to call out to SVN. That should only |
| 18 ever be done by the cronjobs, which then write the result into DataStore, | 18 ever be done by the cronjobs, which then write the result into DataStore, |
| 19 which is as far as instances look. To enable this, crons can pass a custom | 19 which is as far as instances look. To enable this, crons can pass a custom |
| 20 (presumably online) ServerInstance into Get(). | 20 (presumably online) ServerInstance into Get(). |
| 21 | 21 |
| 22 Why? SVN is slow and a bit flaky. Cronjobs failing is annoying but temporary. | 22 Why? SVN is slow and a bit flaky. Cronjobs failing is annoying but temporary. |
| 23 Instances failing affects users, and is really bad. | 23 Instances failing affects users, and is really bad. |
| 24 | 24 |
| 25 Anyway - to enforce this, we actually don't give instances access to SVN. If | 25 Anyway - to enforce this, we actually don't give instances access to SVN. If |
| 26 anything is missing from datastore, it'll be a 404. If the cronjobs don't | 26 anything is missing from datastore, it'll be a 404. If the cronjobs don't |
| 27 manage to catch everything - uhoh. On the other hand, we'll figure it out | 27 manage to catch everything - uhoh. On the other hand, we'll figure it out |
| 28 pretty soon, and it also means that legitimate 404s are caught before a round | 28 pretty soon, and it also means that legitimate 404s are caught before a round |
| 29 trip to SVN. | 29 trip to SVN. |
| 30 ''' | 30 ''' |
| 31 def __init__(self, delegate): | 31 def __init__(self, delegate): |
| 32 self._delegate = delegate | 32 self._delegate = delegate |
| 33 | 33 |
| 34 @memoize | 34 @memoize |
| 35 def CreateServerInstance(self): | 35 def CreateServerInstance(self): |
| 36 object_store_creator = ObjectStoreCreator(start_empty=False) | 36 object_store_creator = ObjectStoreCreator(start_empty=False) |
| 37 branch_utility = self._delegate.CreateBranchUtility(object_store_creator) | 37 branch_utility = self._delegate.CreateBranchUtility(object_store_creator) |
| 38 host_file_system_creator = self._delegate.CreateHostFileSystemCreator( | 38 host_file_system_provider = self._delegate.CreateHostFileSystemProvider( |
| 39 object_store_creator) | 39 object_store_creator, |
| 40 host_file_system = host_file_system_creator.Create() | 40 offline=True) |
| 41 app_samples_file_system = self._delegate.CreateAppSamplesFileSystem( | 41 app_samples_file_system = self._delegate.CreateAppSamplesFileSystem( |
| 42 object_store_creator) | 42 object_store_creator) |
| 43 compiled_host_fs_factory = CompiledFileSystem.Factory( | 43 compiled_host_fs_factory = CompiledFileSystem.Factory( |
| 44 host_file_system, | 44 host_file_system_provider.GetTrunk(), |
| 45 object_store_creator) | 45 object_store_creator) |
| 46 return ServerInstance(object_store_creator, | 46 return ServerInstance(object_store_creator, |
| 47 host_file_system, | |
| 48 app_samples_file_system, | 47 app_samples_file_system, |
| 49 compiled_host_fs_factory, | 48 compiled_host_fs_factory, |
| 50 branch_utility, | 49 branch_utility, |
| 51 host_file_system_creator) | 50 host_file_system_provider) |
| 52 | 51 |
| 53 class InstanceServlet(object): | 52 class InstanceServlet(object): |
| 54 '''Servlet for running on normal AppEngine instances. | 53 '''Servlet for running on normal AppEngine instances. |
| 55 Create this via GetConstructor() so that cache state can be shared amongst | 54 Create this via GetConstructor() so that cache state can be shared amongst |
| 56 them via the memoizing Delegate. | 55 them via the memoizing Delegate. |
| 57 ''' | 56 ''' |
| 58 class Delegate(object): | 57 class Delegate(object): |
| 59 '''Allow runtime dependencies to be overriden for testing. | 58 '''Allow runtime dependencies to be overriden for testing. |
| 60 ''' | 59 ''' |
| 61 def CreateBranchUtility(self, object_store_creator): | 60 def CreateBranchUtility(self, object_store_creator): |
| 62 return BranchUtility.Create(object_store_creator) | 61 return BranchUtility.Create(object_store_creator) |
| 63 | 62 |
| 64 def CreateHostFileSystemCreator(self, object_store_creator): | 63 def CreateHostFileSystemProvider(self, object_store_creator, offline=None): |
| 65 return HostFileSystemCreator(object_store_creator, offline=True) | 64 return HostFileSystemProvider(object_store_creator, offline=offline) |
| 66 | 65 |
| 67 def CreateAppSamplesFileSystem(self, object_store_creator): | 66 def CreateAppSamplesFileSystem(self, object_store_creator): |
| 68 # TODO(kalman): OfflineServerInstance wrapper for GithubFileSystem, but | 67 # TODO(kalman): OfflineServerInstance wrapper for GithubFileSystem, but |
| 69 # the cron job doesn't crawl the samples yet. | 68 # the cron job doesn't crawl the samples yet. |
| 70 return (EmptyDirFileSystem() if IsDevServer() else | 69 return (EmptyDirFileSystem() if IsDevServer() else |
| 71 GithubFileSystem.Create(object_store_creator)) | 70 GithubFileSystem.Create(object_store_creator)) |
| 72 | 71 |
| 73 @staticmethod | 72 @staticmethod |
| 74 def GetConstructor(delegate_for_test=None): | 73 def GetConstructor(delegate_for_test=None): |
| 75 render_servlet_delegate = OfflineRenderServletDelegate( | 74 render_servlet_delegate = OfflineRenderServletDelegate( |
| 76 delegate_for_test or InstanceServlet.Delegate()) | 75 delegate_for_test or InstanceServlet.Delegate()) |
| 77 return lambda request: RenderServlet(request, render_servlet_delegate) | 76 return lambda request: RenderServlet(request, render_servlet_delegate) |
| 78 | 77 |
| 79 # NOTE: if this were a real Servlet it would implement a Get() method, but | 78 # NOTE: if this were a real Servlet it would implement a Get() method, but |
| 80 # GetConstructor returns an appropriate lambda function (Request -> Servlet). | 79 # GetConstructor returns an appropriate lambda function (Request -> Servlet). |
| OLD | NEW |