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