Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / instance_servlet.py
blob6528aa77f9a7cfb9ed5322380f6390937bfde544
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.
5 from branch_utility import BranchUtility
6 from commit_tracker import CommitTracker
7 from compiled_file_system import CompiledFileSystem
8 from environment import IsDevServer, IsReleaseServer
9 from host_file_system_provider import HostFileSystemProvider
10 from third_party.json_schema_compiler.memoize import memoize
11 from render_servlet import RenderServlet
12 from object_store_creator import ObjectStoreCreator
13 from server_instance import ServerInstance
14 from gcs_file_system_provider import CloudStorageFileSystemProvider
17 class InstanceServletRenderServletDelegate(RenderServlet.Delegate):
18 '''AppEngine instances should never need to call out to Gitiles. That should
19 only ever be done by the cronjobs, which then write the result into
20 DataStore, which is as far as instances look. To enable this, crons can pass
21 a custom (presumably online) ServerInstance into Get().
23 Why? Gitiles is slow and a bit flaky. Refresh jobs failing is annoying but
24 temporary. Instances failing affects users, and is really bad.
26 Anyway - to enforce this, we actually don't give instances access to
27 Gitiles. If anything is missing from datastore, it'll be a 404. If the
28 cronjobs don't manage to catch everything - uhoh. On the other hand, we'll
29 figure it out pretty soon, and it also means that legitimate 404s are caught
30 before a round trip to Gitiles.
31 '''
32 def __init__(self, delegate):
33 self._delegate = delegate
35 @memoize
36 def CreateServerInstance(self):
37 object_store_creator = ObjectStoreCreator(start_empty=False)
38 branch_utility = self._delegate.CreateBranchUtility(object_store_creator)
39 commit_tracker = CommitTracker(object_store_creator)
40 # In production have offline=True so that we can catch cron errors. In
41 # development it's annoying to have to run the cron job, so offline=False.
42 # Note that offline=True if running on any appengine server due to
43 # http://crbug.com/345361.
44 host_file_system_provider = self._delegate.CreateHostFileSystemProvider(
45 object_store_creator,
46 offline=not (IsDevServer() or IsReleaseServer()),
47 pinned_commit=commit_tracker.Get('master').Get(),
48 cache_only=True)
49 return ServerInstance(object_store_creator,
50 CompiledFileSystem.Factory(object_store_creator),
51 branch_utility,
52 host_file_system_provider,
53 CloudStorageFileSystemProvider(object_store_creator))
55 class InstanceServlet(object):
56 '''Servlet for running on normal AppEngine instances.
57 Create this via GetConstructor() so that cache state can be shared amongst
58 them via the memoizing Delegate.
59 '''
60 class Delegate(object):
61 '''Allow runtime dependencies to be overriden for testing.
62 '''
63 def CreateBranchUtility(self, object_store_creator):
64 return BranchUtility.Create(object_store_creator)
66 def CreateHostFileSystemProvider(self, object_store_creator, **optargs):
67 return HostFileSystemProvider(object_store_creator, **optargs)
69 def CreateGithubFileSystemProvider(self, object_store_creator):
70 return GithubFileSystemProvider(object_store_creator)
72 @staticmethod
73 def GetConstructor(delegate_for_test=None):
74 render_servlet_delegate = InstanceServletRenderServletDelegate(
75 delegate_for_test or InstanceServlet.Delegate())
76 return lambda request: RenderServlet(request, render_servlet_delegate)
78 # NOTE: if this were a real Servlet it would implement a Get() method, but
79 # GetConstructor returns an appropriate lambda function (Request -> Servlet).