Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

一定時間使用されていないサービスの自動停止に対応

  • Loading branch information...
commit 0fdb62d856a106c18e0147e30bb554cb7f5b1395 1 parent ee26eaa
authored
9 src/main/scala/service/Daemon.scala
@@ -13,6 +13,14 @@ import jp.w3ch.psm.daemon
13 13 import jp.w3ch.psm.util.Timer;
14 14
15 15 class Daemon(command:String, address:InetSocketAddress) extends HttpService {
  16 + var lastRequestedAt = Time.now
  17 +
  18 + val stopTimer = Timer.schedule(5.seconds) {
  19 + if(lastRequestedAt + 5.minutes < Time.now) {
  20 + executor.stop()
  21 + }
  22 + }
  23 +
16 24 val executor = daemon.Daemon(command)
17 25 val port = new Port(address)
18 26
@@ -21,6 +29,7 @@ class Daemon(command:String, address:InetSocketAddress) extends HttpService {
21 29 def this(command:String, address: Int) = this(command, new InetSocketAddress("127.0.0.1", address))
22 30
23 31 override def apply(request:HttpRequest) = {
  32 + lastRequestedAt = Time.now
24 33 for {
25 34 _ <- waitForPort(1)
26 35 .rescue {
29 src/main/scala/util/Timer.scala
... ... @@ -0,0 +1,29 @@
  1 +package jp.w3ch.psm.util
  2 +
  3 +import com.twitter.util.Duration
  4 +import com.twitter.util.Time
  5 +
  6 +class Timer(duration:Duration, fn: => Unit) {
  7 + @volatile var finished = false
  8 +
  9 + val thread = new Thread {
  10 + override def run() {
  11 + while(! finished) {
  12 + fn
  13 + Thread.sleep(duration.inMillis);
  14 + }
  15 + }
  16 + }
  17 + thread.setDaemon(true)
  18 + thread.start
  19 +
  20 + def stop() {
  21 + finished = true
  22 + }
  23 +}
  24 +
  25 +object Timer {
  26 + def schedule(duration:Duration)(fn: => Unit):Timer = {
  27 + return new Timer(duration, fn)
  28 + }
  29 +}

0 comments on commit 0fdb62d

Please sign in to comment.
Something went wrong with that request. Please try again.