-
-
Save kevinmeredith/10507a6159d7c0616c7efdc8f674e138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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