Skip to content

Instantly share code, notes, and snippets.

@alexandru
Last active February 12, 2017 22:47
Show Gist options
  • Select an option

  • Save alexandru/7f7d80e7e70e2d58c9d02b9cdc5f5a2a to your computer and use it in GitHub Desktop.

Select an option

Save alexandru/7f7d80e7e70e2d58c9d02b9cdc5f5a2a to your computer and use it in GitHub Desktop.

Revisions

  1. alexandru revised this gist Feb 1, 2017. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions scheduler.scala
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,7 @@ def scheduleOncePerDay(time: LocalTime, now: DateTime = DateTime.now())(cb: () =
    if (next.compareTo(now) <= 0) next.plusDays(1).getMillis else next.getMillis
    }

    val delay = nextTick - now.getMillis

    s.scheduleOnce(remainingMs, TimeUnit.MILLISECONDS,
    s.scheduleOnce(nextTick - now.getMillis, TimeUnit.MILLISECONDS,
    new Runnable {
    def run(): Unit = {
    try cb() catch {
  2. alexandru revised this gist Feb 1, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion scheduler.scala
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,12 @@ import scala.util.control.NonFatal
    def scheduleOncePerDay(time: LocalTime, now: DateTime = DateTime.now())(cb: () => Unit)
    (implicit s: Scheduler): Cancelable = {

    val remainingMs = now.getMillis - {
    val nextTick = {
    val next = now.toDateTime(DateTimeZone.UTC).withTime(time)
    if (next.compareTo(now) <= 0) next.plusDays(1).getMillis else next.getMillis
    }

    val delay = nextTick - now.getMillis

    s.scheduleOnce(remainingMs, TimeUnit.MILLISECONDS,
    new Runnable {
  3. alexandru created this gist Feb 1, 2017.
    26 changes: 26 additions & 0 deletions scheduler.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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 remainingMs = now.getMillis - {
    val next = now.toDateTime(DateTimeZone.UTC).withTime(time)
    if (next.compareTo(now) <= 0) next.plusDays(1).getMillis else next.getMillis
    }

    s.scheduleOnce(remainingMs, TimeUnit.MILLISECONDS,
    new Runnable {
    def run(): Unit = {
    try cb() catch {
    case NonFatal(ex) => s.reportFailure(ex)
    }

    // Next please!
    scheduleOncePerDay(time)(cb)(s)
    }
    })
    }