Created
February 2, 2012 20:47
-
-
Save stephenjudkins/1725668 to your computer and use it in GitHub Desktop.
Start and stop background tasks in SBT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val runner = AttributeKey[Runner]("runner") | |
val start = TaskKey[Unit]("start", "start web runner") | |
val stop = TaskKey[Unit]("stop", "stop web runner") | |
trait Startable { | |
def start() | |
def stop() | |
} | |
class Runner { | |
private var currentSubject:Option[Startable] = None | |
def start(subject: Startable) { | |
stop() | |
subject.start() | |
currentSubject = Some(subject) | |
} | |
def stop() { | |
currentSubject foreach { _.stop() } | |
currentSubject = None | |
} | |
} | |
implicit def stateToRunner(s: State) = s.get(runner).get | |
def onRecompile(s: State) { | |
println("recompiled! " + s) | |
} | |
val webSettings = Seq( | |
start <<= (state, fullClasspath in Runtime, scalaInstance in Runtime) map { (s, cp:Classpath, si:ScalaInstance) => | |
val loader = ClasspathUtilities.makeLoader(cp map {_.data}, si) | |
val klass = loader.loadClass("com.btas.Launcher") | |
s.start(new Startable { | |
def start() { klass.getDeclaredMethod("start").invoke(null) } | |
def stop() { klass.getDeclaredMethod("stop").invoke(null) } | |
}) | |
}, | |
stop <<= state map { (s:State) => | |
s.stop() | |
}, | |
onLoad in Global := { (state) => state.put(runner, new Runner) } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I'm looking for running some tasks in background, this code seems to do what i'm looking for.
But how to use it ? how to call start and stop methods ?