Home | History | Annotate | Download | only in server2
      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.
      4 
      5 from branch_utility import BranchUtility
      6 from compiled_file_system import CompiledFileSystem
      7 from environment import IsDevServer
      8 from github_file_system_provider import GithubFileSystemProvider
      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 
     15 class InstanceServletRenderServletDelegate(RenderServlet.Delegate):
     16   '''AppEngine instances should never need to call out to SVN. That should only
     17   ever be done by the cronjobs, which then write the result into DataStore,
     18   which is as far as instances look. To enable this, crons can pass a custom
     19   (presumably online) ServerInstance into Get().
     20 
     21   Why? SVN is slow and a bit flaky. Cronjobs failing is annoying but temporary.
     22   Instances failing affects users, and is really bad.
     23 
     24   Anyway - to enforce this, we actually don't give instances access to SVN.  If
     25   anything is missing from datastore, it'll be a 404. If the cronjobs don't
     26   manage to catch everything - uhoh. On the other hand, we'll figure it out
     27   pretty soon, and it also means that legitimate 404s are caught before a round
     28   trip to SVN.
     29   '''
     30   def __init__(self, delegate):
     31     self._delegate = delegate
     32 
     33   @memoize
     34   def CreateServerInstance(self):
     35     object_store_creator = ObjectStoreCreator(start_empty=False)
     36     branch_utility = self._delegate.CreateBranchUtility(object_store_creator)
     37     # In production have offline=True so that we can catch cron errors.  In
     38     # development it's annoying to have to run the cron job, so offline=False.
     39     host_file_system_provider = self._delegate.CreateHostFileSystemProvider(
     40         object_store_creator,
     41         offline=not IsDevServer())
     42     github_file_system_provider = self._delegate.CreateGithubFileSystemProvider(
     43         object_store_creator)
     44     return ServerInstance(object_store_creator,
     45                           CompiledFileSystem.Factory(object_store_creator),
     46                           branch_utility,
     47                           host_file_system_provider,
     48                           github_file_system_provider)
     49 
     50 class InstanceServlet(object):
     51   '''Servlet for running on normal AppEngine instances.
     52   Create this via GetConstructor() so that cache state can be shared amongst
     53   them via the memoizing Delegate.
     54   '''
     55   class Delegate(object):
     56     '''Allow runtime dependencies to be overriden for testing.
     57     '''
     58     def CreateBranchUtility(self, object_store_creator):
     59       return BranchUtility.Create(object_store_creator)
     60 
     61     def CreateHostFileSystemProvider(self, object_store_creator, **optargs):
     62       return HostFileSystemProvider(object_store_creator, **optargs)
     63 
     64     def CreateGithubFileSystemProvider(self, object_store_creator):
     65       return GithubFileSystemProvider(object_store_creator)
     66 
     67   @staticmethod
     68   def GetConstructor(delegate_for_test=None):
     69     render_servlet_delegate = InstanceServletRenderServletDelegate(
     70         delegate_for_test or InstanceServlet.Delegate())
     71     return lambda request: RenderServlet(request, render_servlet_delegate)
     72 
     73   # NOTE: if this were a real Servlet it would implement a Get() method, but
     74   # GetConstructor returns an appropriate lambda function (Request -> Servlet).
     75