Skip to content

Instantly share code, notes, and snippets.

@stephenjudkins
Created February 2, 2012 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenjudkins/1725668 to your computer and use it in GitHub Desktop.
Save stephenjudkins/1725668 to your computer and use it in GitHub Desktop.
Start and stop background tasks in SBT
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) }
)
@mcherb
Copy link

mcherb commented Jun 6, 2020

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment