Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6125)

Unified Diff: chrome/common/extensions/docs/server2/cron_servlet.py

Issue 14273035: Docserver: refactor the Handler/ServerInstance relationship into a servlet (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/cron_servlet.py
diff --git a/chrome/common/extensions/docs/server2/cron_servlet.py b/chrome/common/extensions/docs/server2/cron_servlet.py
new file mode 100644
index 0000000000000000000000000000000000000000..d035032c99be4b04c7254a0635c6d6c4c90e3755
--- /dev/null
+++ b/chrome/common/extensions/docs/server2/cron_servlet.py
@@ -0,0 +1,100 @@
+# Copyright 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+import time
+import traceback
+
+from appengine_wrappers import (DeadlineExceededError,
+ FakeRequest,
+ FakeResponse,
+ logservice)
+from branch_utility import BranchUtility
+from render_servlet import RenderServlet
+from server_instance import ServerInstance
+from servlet import Servlet
+import svn_constants
+
+class CronServlet(Servlet):
+ '''Servlet which runs a cron job.
+ '''
+ def Get(self):
+ # Crons often time out, and when they do *and* then eventually try to
+ # flush logs they die. Turn off autoflush and manually do so at the end.
+ logservice.AUTOFLUSH_ENABLED = False
+ try:
+ self._GetImpl()
+ finally:
+ logservice.flush()
+
+ def _GetImpl(self):
+ # 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).
+ channel = self._path.strip('/')
+ logging.info('cron/%s: starting' % channel)
+
+ server_instance = ServerInstance.CreateOnline(channel)
+
+ def run_cron_for_dir(d, path_prefix=''):
+ success = True
+ start_time = time.time()
+ files = [f for f in server_instance.content_cache.GetFromFileListing(d)
+ if not f.endswith('/')]
+ logging.info('cron/%s: rendering %s files from %s...' % (
+ channel, len(files), d))
+ for i, f in enumerate(files):
+ error = None
+ path = '%s%s' % (path_prefix, f)
+ try:
+ response = FakeResponse()
+ RenderServlet(path, FakeRequest(path), response).Get(
+ server_instance=server_instance)
+ if response.status != 200:
+ error = 'Got %s response' % response.status
+ except DeadlineExceededError:
+ logging.error(
+ 'cron/%s: deadline exceeded rendering %s (%s of %s): %s' % (
+ channel, path, i + 1, len(files), traceback.format_exc()))
+ raise
+ except error:
+ pass
+ if error:
+ logging.error('cron/%s: error rendering %s: %s' % (
+ channel, path, error))
+ success = False
+ logging.info('cron/%s: rendering %s files from %s took %s seconds' % (
+ channel, len(files), d, time.time() - start_time))
+ return success
+
+ success = True
+ for path, path_prefix in (
+ # Note: rendering the public templates will pull in all of the private
+ # templates.
+ (svn_constants.PUBLIC_TEMPLATE_PATH, ''),
+ # Note: rendering the public templates will have pulled in the .js and
+ # manifest.json files (for listing examples on the API reference pages),
+ # but there are still images, CSS, etc.
+ (svn_constants.STATIC_PATH, 'static/'),
+ (svn_constants.EXAMPLES_PATH, 'extensions/examples/')):
+ try:
+ # Note: don't try to short circuit any of this stuff. We want to run
+ # the cron for all the directories regardless of intermediate failures.
+ success = run_cron_for_dir(path, path_prefix=path_prefix) and success
+ except DeadlineExceededError:
+ success = False
+ break
+
+ response = self._response
+ if success:
+ response.status = 200
+ response.out.write('Success')
+ else:
+ response.status = 500
+ response.out.write('Failure')
+
+ logging.info('cron/%s: finished' % channel)

Powered by Google App Engine
This is Rietveld 408576698