chronos-scheduler-scala

A job scheduler for Scala.

CI Maven Central Renovate License Tokei

Installation

Add the following to your sbt build (2.13.x):

val version = "..."

libraryDependencies += Seq(
  "com.github.j5ik2o" %% "chronos-scheduler-scala-core" % version,
  "com.github.j5ik2o" %% "chronos-scheduler-scala-akka-actor" % version
)

Usage

Core

The core module provides a simple synchronous API.

var counter = 0

val jobScheduler = JobScheduler(UUID.randomUUID()).addJob(
  Job(
    id = UUID.randomUUID(),
    schedule = CronSchedule("*/1 * * * *", ZoneId.systemDefault()),
    run = { () =>
      println(s"run job: $counter")
      counter += 1
    }
  )
)

while(true) {
  jobScheduler.tick()
  Thread.sleep(1000 * 60)
}

Actor

The actor module provides an asynchronous non-blocking API.

object Main extends App {

  val system = ActorSystem(apply, "job-scheduler-actor-main")

  sealed trait Command
  case class WrappedAddJobReply(reply: JobSchedulerProtocol.AddJobReply) extends Command

  def apply: Behavior[Command] = Behaviors.setup[Command] { ctx =>

    var counter = 0
    val id      = UUID.randomUUID()

    val jobSchedulerActorRef = ctx.spawn(
      JobSchedulerActor(id, Some(1.seconds)), 
      "job-scheduler-actor"
    )

    jobSchedulerActorRef ! JobSchedulerProtocol.AddJob(
      id,
      Job(
        id = UUID.randomUUID(),
        schedule = CronSchedule("*/1 * * * *", ZoneId.systemDefault()),
        run = { () =>
          println(s"run job: $counter")
          counter += 1
        }
      ),
      ctx.messageAdapter[JobSchedulerProtocol.AddJobReply](ref => WrappedAddJobReply(ref))
    )
    Behaviors.receiveMessagePartial[Command] { case WrappedAddJobReply(AddJobSucceeded) =>
      Behaviors.same
    }
  }

}

License

MIT license (LICENSE or https://opensource.org/licenses/MIT)