Adding the orphaned options pages to the navigation
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / admin_servlets.py
blob7990761e02d1cb9928580904ccd657a0d13b4a44
1 # Copyright 2014 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 appengine_wrappers import taskqueue
6 from commit_tracker import CommitTracker
7 from future import All
8 from object_store_creator import ObjectStoreCreator
9 from refresh_tracker import RefreshTracker
10 from servlet import Servlet, Response
13 class EnqueueServlet(Servlet):
14 '''This Servlet can be used to manually enqueue tasks on the default
15 taskqueue. Useful for when an admin wants to manually force a specific
16 DataSource refresh, but the refresh operation takes longer than the 60 sec
17 timeout of a non-taskqueue request. For example, you might query
19 /_enqueue/_refresh/content_providers/cr-native-client?commit=123ff65468dcafff0
21 which will enqueue a task (/_refresh/content_providers/cr-native-client) to
22 refresh the NaCl documentation cache for commit 123ff65468dcafff0.
24 Access to this servlet should always be restricted to administrative users.
25 '''
26 def __init__(self, request):
27 Servlet.__init__(self, request)
29 def Get(self):
30 queue = taskqueue.Queue()
31 queue.add(taskqueue.Task(url='/%s' % self._request.path,
32 params=self._request.arguments))
33 return Response.Ok('Task enqueued.')
36 class QueryCommitServlet(Servlet):
37 '''Provides read access to the commit ID cache within the server. For example:
39 /_query_commit/master
41 will return the commit ID stored under the commit key "master" within the
42 commit cache. Currently "master" is the only named commit we cache, and it
43 corresponds to the commit ID whose data currently populates the data cache
44 used by live instances.
45 '''
46 def __init__(self, request):
47 Servlet.__init__(self, request)
49 def Get(self):
50 object_store_creator = ObjectStoreCreator(start_empty=False)
51 commit_tracker = CommitTracker(object_store_creator)
53 def generate_response(result):
54 commit_id, history = result
55 history_log = ''.join('%s: %s<br>' % (entry.datetime, entry.commit_id)
56 for entry in reversed(history))
57 response = 'Current commit: %s<br><br>Most recent commits:<br>%s' % (
58 commit_id, history_log)
59 return response
61 commit_name = self._request.path
62 id_future = commit_tracker.Get(commit_name)
63 history_future = commit_tracker.GetHistory(commit_name)
64 return Response.Ok(
65 All((id_future, history_future)).Then(generate_response).Get())
68 class DumpRefreshServlet(Servlet):
69 def __init__(self, request):
70 Servlet.__init__(self, request)
72 def Get(self):
73 object_store_creator = ObjectStoreCreator(start_empty=False)
74 refresh_tracker = RefreshTracker(object_store_creator)
75 commit_id = self._request.path
76 work_order = refresh_tracker._GetWorkOrder(commit_id).Get()
77 task_names = ['%s@%s' % (commit_id, task) for task in work_order.tasks]
78 completions = refresh_tracker._task_completions.GetMulti(task_names).Get()
79 missing = []
80 for task in task_names:
81 if task not in completions:
82 missing.append(task)
83 response = 'Missing:<br>%s' % ''.join('%s<br>' % task for task in missing)
84 return Response.Ok(response)
86 class ResetCommitServlet(Servlet):
87 '''Writes a new commit ID to the commit cache. For example:
89 /_reset_commit/master/123456
91 will reset the 'master' commit ID to '123456'. The provided commit MUST be
92 in the named commit's recent history or it will be ignored.
93 '''
95 class Delegate(object):
96 def CreateCommitTracker(self):
97 return CommitTracker(ObjectStoreCreator(start_empty=False))
99 def __init__(self, request, delegate=Delegate()):
100 Servlet.__init__(self, request)
101 self._delegate = delegate
103 def Get(self):
104 commit_tracker = self._delegate.CreateCommitTracker()
105 commit_name, commit_id = self._request.path.split('/', 1)
106 history = commit_tracker.GetHistory(commit_name).Get()
107 if not any(entry.commit_id == commit_id for entry in history):
108 return Response.BadRequest('Commit %s not cached.' % commit_id)
109 commit_tracker.Set(commit_name, commit_id).Get()
110 return Response.Ok('Commit "%s" updated to %s' % (commit_name, commit_id))