Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

プロセスの終了時に起動したデーモンを終了するように修正

  • Loading branch information...
commit 2ccf2c495303a0f24303f4e7b1ef7dba62234dd4 1 parent da6e84e
Motoi Washida authored
5 src/main/scala/Psm.scala
View
@@ -17,14 +17,13 @@ object Psm {
def main(args:Array[String]) {
var config = new Eval().apply[Configuration](new File(configPath))
- val server = {
- config()
- }
+ val (server, pool) = config()
println("Listening on port " + config.listen.toInt + ". Press any key to quit.")
System.in.read()
server.close(4.seconds)
+ pool.stop()
}
}
13 src/main/scala/config/Configuration.scala
View
@@ -10,9 +10,10 @@ import org.jboss.netty.handler.codec.http._
import jp.w3ch.psm.service.HttpService
import jp.w3ch.psm.DispatchingServer
+import jp.w3ch.psm.daemon.{Daemon,Pool}
-class Configuration extends com.twitter.util.Config[Server] with ConfigurationUtil {
+class Configuration extends com.twitter.util.Config[(Server, Pool)] with ConfigurationUtil {
// ----------------------------------------------------------------
// Parameters
@@ -21,6 +22,7 @@ class Configuration extends com.twitter.util.Config[Server] with ConfigurationUt
var listen = required[Int]
var defaultProxy = required[HttpService]
val proxyHandler = mutable.Buffer[DispatchingServer.ProxyEntry]()
+ val pool = mutable.Buffer[Daemon]()
// ----------------------------------------------------------------
@@ -37,7 +39,10 @@ class Configuration extends com.twitter.util.Config[Server] with ConfigurationUt
.hostConnectionMaxLifeTime (5.minutes)
.readTimeout (2.minutes)
- sb.build(new DispatchingServer(proxyHandler.readOnly, defaultProxy))
+ (
+ sb.build(new DispatchingServer(proxyHandler.readOnly, defaultProxy)),
+ new Pool(pool.readOnly)
+ )
}
@@ -58,6 +63,10 @@ class Configuration extends com.twitter.util.Config[Server] with ConfigurationUt
}
}
+ override def addDaemon(d:Daemon) {
+ pool += d
+ }
+
}
11 src/main/scala/config/ConfigurationUtil.scala
View
@@ -1,13 +1,22 @@
package jp.w3ch.psm.config
import jp.w3ch.psm.service
+import jp.w3ch.psm.daemon.Daemon
trait ConfigurationUtil {
+ def addDaemon(d:Daemon):Unit
+
val port = new service.Port(_:Int)
- val daemon = new service.Daemon(_:String, _:Int)
val textResponse = new service.TextResponse(_)
+
+ def daemon(command:String, port:Int) = {
+ val d = Daemon(command)
+ addDaemon(d)
+
+ new service.Daemon(d, port)
+ }
}
// vim: set shiftwidth=2 expandtab :
15 src/main/scala/daemon/Pool.scala
View
@@ -0,0 +1,15 @@
+package jp.w3ch.psm.daemon
+
+
+/**
+ * 各種プロセスを管理します。
+ */
+class Pool(daemons: Seq[Daemon]) {
+
+ def stop() {
+ daemons.foreach { _.stop() }
+ }
+
+}
+
+// vim: set shiftwidth=2 expandtab:
6 src/main/scala/service/Daemon.scala
View
@@ -12,7 +12,7 @@ import org.jboss.netty.handler.codec.http._
import jp.w3ch.psm.daemon
import jp.w3ch.psm.util.Timer;
-class Daemon(command:String, address:InetSocketAddress) extends HttpService {
+class Daemon(dm:daemon.Daemon, address:InetSocketAddress) extends HttpService {
var lastRequestedAt = Time.now
val stopTimer = Timer.schedule(5.seconds) {
@@ -21,12 +21,12 @@ class Daemon(command:String, address:InetSocketAddress) extends HttpService {
}
}
- val executor = daemon.Daemon(command)
+ val executor = dm
val port = new Port(address)
def exec = executor.exec()
- def this(command:String, address: Int) = this(command, new InetSocketAddress("127.0.0.1", address))
+ def this(dm:daemon.Daemon, address: Int) = this(dm, new InetSocketAddress("127.0.0.1", address))
override def apply(request:HttpRequest) = {
lastRequestedAt = Time.now
40 src/test/scala/daemon/PoolSpec.scala
View
@@ -0,0 +1,40 @@
+package jp.w3ch.psm.daemon
+
+import org.jboss.netty.handler.codec.http._
+import com.twitter.finagle.Service
+import org.specs2.mutable._
+import org.specs2.mock._
+
+import jp.w3ch.psm.DispatchingServer
+import jp.w3ch.psm.service.HttpService
+
+
+class PoolSpec extends Specification with Mockito {
+
+ "stops all daemons" >> {
+
+ "when single daemon is running" in {
+ val daemon = mock[Daemon]
+
+ val subject = new Pool(Seq(daemon))
+
+ subject.stop()
+
+ there was one(daemon).stop()
+ }
+
+ "when multiple daemons are running" in {
+ val daemons = Seq(mock[Daemon], mock[Daemon])
+
+ val subject = new Pool(daemons)
+
+ subject.stop()
+
+ there was one(daemons(0)).stop()
+ there was one(daemons(1)).stop()
+ }
+ }
+
+}
+
+// vim: set ts=2 sw=2 et:
Please sign in to comment.
Something went wrong with that request. Please try again.