Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Forked from alexandru/scheduler.scala
Created February 1, 2017 18:23
Show Gist options
  • Save kevinmeredith/10507a6159d7c0616c7efdc8f674e138 to your computer and use it in GitHub Desktop.
Save kevinmeredith/10507a6159d7c0616c7efdc8f674e138 to your computer and use it in GitHub Desktop.
import org.joda.time._
import monix.execution._
import monix.execution.Scheduler.Implicits.global
import java.util.concurrent.TimeUnit
import scala.util.control.NonFatal
def scheduleOncePerDay(time: LocalTime, now: DateTime = DateTime.now())(cb: () => Unit)
(implicit s: Scheduler): Cancelable = {
val nextTick = {
val next = now.toDateTime(DateTimeZone.UTC).withTime(time)
if (next.compareTo(now) <= 0) next.plusDays(1).getMillis else next.getMillis
}
s.scheduleOnce(nextTick - now.getMillis, TimeUnit.MILLISECONDS,
new Runnable {
def run(): Unit = {
try cb() catch {
case NonFatal(ex) => s.reportFailure(ex)
}
// Next please!
scheduleOncePerDay(time)(cb)(s)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment