Skip to content

Instantly share code, notes, and snippets.

@deepakkumarnd
Last active July 9, 2020 18:10
Show Gist options
  • Save deepakkumarnd/ebccf0115c01aae7c3b1990c0af880ad to your computer and use it in GitHub Desktop.
Save deepakkumarnd/ebccf0115c01aae7c3b1990c0af880ad to your computer and use it in GitHub Desktop.
A simple ruby rake like task runner in scala
package rake
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
final class Task private (
desc: String,
dependencies: Seq[Task] = Seq.empty[Task],
body: => Unit
) {
var executed: Boolean = false
def run(): Future[Unit] = {
if (!executed) {
println(s"Executing dependencies for $desc")
Future.traverse(dependencies)(_.run()).map { _ =>
body
executed = true
}
} else Future.successful(())
}
}
object Task {
def task(desc: String, dependencies: Seq[Task] = List.empty[Task])(
body: => Unit
): Task =
new Task(desc, dependencies, body)
}
object RakeTest extends App {
import Task._
val boilWater = task("Boil water") {
println("Boiling water ")
}
val boilMilk = task("Boil milk") {
println("Boiling milk")
}
val mixWaterAndMilk =
task("Mix milk and water", Seq(boilWater, boilMilk)) {
println("Mixing milk and water")
}
val addCoffeePowder = task("Add coffee powder", Seq(mixWaterAndMilk)) {
println("Adding coffee powder")
}
val addSugar = task("Add sugar", Seq(addCoffeePowder)) {
println("Adding sugar")
}
val makeCoffee = task("Make coffee", Seq(addSugar)) {
println("Coffee is ready to serve")
}
Await.result(makeCoffee.run(), 10.seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment