Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 0fdb62d856a106c18e0147e30bb554cb7f5b1395 1 parent ee26eaa
@sile authored
View
9 src/main/scala/service/Daemon.scala
@@ -13,6 +13,14 @@ import jp.w3ch.psm.daemon
import jp.w3ch.psm.util.Timer;
class Daemon(command:String, address:InetSocketAddress) extends HttpService {
+ var lastRequestedAt = Time.now
+
+ val stopTimer = Timer.schedule(5.seconds) {
+ if(lastRequestedAt + 5.minutes < Time.now) {
+ executor.stop()
+ }
+ }
+
val executor = daemon.Daemon(command)
val port = new Port(address)
@@ -21,6 +29,7 @@ class Daemon(command:String, address:InetSocketAddress) extends HttpService {
def this(command:String, address: Int) = this(command, new InetSocketAddress("127.0.0.1", address))
override def apply(request:HttpRequest) = {
+ lastRequestedAt = Time.now
for {
_ <- waitForPort(1)
.rescue {
View
29 src/main/scala/util/Timer.scala
@@ -0,0 +1,29 @@
+package jp.w3ch.psm.util
+
+import com.twitter.util.Duration
+import com.twitter.util.Time
+
+class Timer(duration:Duration, fn: => Unit) {
+ @volatile var finished = false
+
+ val thread = new Thread {
+ override def run() {
+ while(! finished) {
+ fn
+ Thread.sleep(duration.inMillis);
+ }
+ }
+ }
+ thread.setDaemon(true)
+ thread.start
+
+ def stop() {
+ finished = true
+ }
+}
+
+object Timer {
+ def schedule(duration:Duration)(fn: => Unit):Timer = {
+ return new Timer(duration, fn)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.