Example #1
0
    def _HandleGet(self, path):
        channel_name, real_path = BranchUtility.SplitChannelNameFromPath(path)

        if channel_name == _DEFAULT_CHANNEL:
            self.redirect('/%s' % real_path)
            return

        if channel_name is None:
            channel_name = _DEFAULT_CHANNEL

        # TODO(kalman): Check if |path| is a directory and serve path/index.html
        # rather than special-casing apps/extensions.
        if real_path.strip('/') == 'apps':
            real_path = 'apps/index.html'
        if real_path.strip('/') == 'extensions':
            real_path = 'extensions/index.html'

        server_instance = ServerInstance.GetOrCreate(channel_name)

        canonical_path = server_instance.path_canonicalizer.Canonicalize(
            real_path)
        if real_path != canonical_path:
            self.redirect(canonical_path)
            return

        ServerInstance.GetOrCreate(channel_name).Get(real_path, self.request,
                                                     self.response)
Example #2
0
    def _HandleCron(self, path):
        # Cron strategy:
        #
        # Find all public template files and static files, and render them. Most of
        # the time these won't have changed since the last cron run, so it's a
        # little wasteful, but hopefully rendering is really fast (if it isn't we
        # have a problem).
        class MockResponse(object):
            def __init__(self):
                self.status = 200
                self.out = StringIO()
                self.headers = {}

            def set_status(self, status):
                self.status = status

            def clear(self, *args):
                pass

        class MockRequest(object):
            def __init__(self, path):
                self.headers = {}
                self.path = path
                self.url = '//localhost/%s' % path

        channel = path.split('/')[-1]
        logging.info('cron/%s: starting' % channel)

        server_instance = ServerInstance.GetOrCreate(channel)

        def run_cron_for_dir(d):
            error = None
            start_time = time.time()
            files = [
                f for f in server_instance.content_cache.GetFromFileListing(d)
                if not f.endswith('/')
            ]
            for f in files:
                try:
                    server_instance.Get(f, MockRequest(f), MockResponse())
                except error:
                    logging.error('cron/%s: error rendering %s/%s: %s' %
                                  (channel, d, f, error))
            logging.info('cron/%s: rendering %s files in %s took %s seconds' %
                         (channel, len(files), d, time.time() - start_time))
            return error

        # Don't use "or" since we want to evaluate everything no matter what.
        was_error = any((run_cron_for_dir(svn_constants.PUBLIC_TEMPLATE_PATH),
                         run_cron_for_dir(svn_constants.STATIC_PATH)))

        if was_error:
            self.response.status = 500
            self.response.out.write('Failure')
        else:
            self.response.status = 200
            self.response.out.write('Success')

        logging.info('cron/%s: finished' % channel)