Permalink
Browse files

get rid of all the admin-initialization cruft in ServiceTracker. it d…

…oesn't need that anymore. instead, allow extra handlers to be configured in the config object.
  • Loading branch information...
Robey Pointer
Robey Pointer committed May 5, 2011
1 parent a523fc4 commit 561a8c3202039d40ab60b3bfbb8ce697423db83e
@@ -25,6 +25,11 @@ import com.twitter.conversions.time._
import com.twitter.logging.Logger
import com.twitter.util.{Duration, Time}
+/**
+ * Custom handler interface for the admin web site. The standard `render` calls are implemented in
+ * terms of a single `handle` call. For more functionality, check out subclasses like
+ * `FolderResourceHandler` and `CgiRequestHandler`.
+ */
abstract class CustomHttpHandler extends HttpHandler {
private val log = Logger.get(getClass)
@@ -77,6 +82,9 @@ class PageResourceHandler(path: String) extends CustomHttpHandler {
}
}
+/**
+ * Serve static pages as java resources.
+ */
class FolderResourceHandler(staticPath: String) extends CustomHttpHandler {
/**
* Given a requestPath (e.g. /static/digraph.js), break it up into the path and filename
@@ -25,8 +25,6 @@ import com.sun.net.httpserver.{HttpHandler, HttpExchange}
*/
object ServiceTracker {
private val services = new mutable.HashSet[Service]
- private val queuedAdminHandlers = new mutable.HashMap[String, HttpHandler]
- private var adminHttpService: Option[AdminHttpService] = None
def clearForTests() {
services.clear()
@@ -46,50 +44,13 @@ object ServiceTracker {
services.clear()
rv
}.foreach { _.shutdown() }
- stopAdmin()
}
def quiesce() {
synchronized { services.toList }.foreach { _.quiesce() }
- stopAdmin()
}
def reload() {
synchronized { services.toList }.foreach { _.reload() }
}
-
- def startAdmin(service: Option[AdminHttpService]) {
- synchronized {
- adminHttpService = service
- service.foreach { s =>
- for ((path, handler) <- queuedAdminHandlers) {
- s.addContext(path, handler)
- }
- s.start()
- }
- }
- }
-
- def stopAdmin() {
- synchronized {
- adminHttpService.map { _.shutdown() }
- adminHttpService = None
- }
- }
-
- def registerAdminHttpHandler(path: String)(generator: (List[(String, String)]) => String) = {
- val handler = new CustomHttpHandler {
- def handle(exchange: HttpExchange) {
- val parameters = CgiRequestHandler.exchangeToParameters(exchange)
- render(generator(parameters), exchange)
- }
- }
-
- synchronized {
- adminHttpService match {
- case Some(ahs) => ahs.addContext(path, handler)
- case None => queuedAdminHandlers(path) = handler
- }
- }
- }
}
@@ -18,6 +18,8 @@ package com.twitter.ostrich
package admin
package config
+import scala.collection.Map
+import scala.collection.mutable
import com.twitter.conversions.time._
import com.twitter.logging.Logger
import com.twitter.util.{Config, Duration}
@@ -84,16 +86,28 @@ class AdminServiceConfig extends Config[RuntimeEnvironment => Option[AdminHttpSe
*/
var statsNodes: List[StatsConfig] = Nil
+ /**
+ * Extra handlers for the admin web interface.
+ * Each key is a path prefix, and each value is the handler to invoke for that path. You can use
+ * this to setup extra functionality for the admin web interface.
+ */
+ var extraHandlers: Map[String, CustomHttpHandler] = new mutable.HashMap[String, CustomHttpHandler]
+
def apply() = { (runtime: RuntimeEnvironment) =>
- val adminHttpService = httpPort.map { port =>
+ httpPort.map { port =>
val admin = new AdminHttpService(port, httpBacklog, runtime)
statsNodes.foreach { config =>
config()(admin)
}
+
+ ServiceTracker.register(admin)
+ admin.start()
+
+ // handlers can't be added until the admin server is started.
+ extraHandlers.foreach { case (path, handler) =>
+ admin.addContext(path, handler)
+ }
admin
}
-
- ServiceTracker.startAdmin(adminHttpService)
- adminHttpService
}
}

1 comment on commit 561a8c3

@olix0r

This comment has been minimized.

Show comment Hide comment
@olix0r

olix0r May 5, 2011

Contributor

You can't tell, but I'm +1ing this as hard as I can.

Contributor

olix0r commented on 561a8c3 May 5, 2011

You can't tell, but I'm +1ing this as hard as I can.

Please sign in to comment.