Skip to content

Commit

Permalink
プロセスの終了時に起動したデーモンを終了するように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
wm3 committed Jun 17, 2012
1 parent da6e84e commit 2ccf2c4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/main/scala/Psm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/scala/config/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]()


// ----------------------------------------------------------------
Expand All @@ -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)
)
}


Expand All @@ -58,6 +63,10 @@ class Configuration extends com.twitter.util.Config[Server] with ConfigurationUt
}
}

override def addDaemon(d:Daemon) {
pool += d
}

}


Expand Down
11 changes: 10 additions & 1 deletion src/main/scala/config/ConfigurationUtil.scala
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions src/main/scala/daemon/Pool.scala
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 3 deletions src/main/scala/service/Daemon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/test/scala/daemon/PoolSpec.scala
Original file line number Diff line number Diff line change
@@ -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:

0 comments on commit 2ccf2c4

Please sign in to comment.