-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from pawelkaczor/akka-timers-without-trigger
Add calev-akka (Akka Typed Support) - without using Trigger directly
- Loading branch information
Showing
13 changed files
with
417 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
38 changes: 38 additions & 0 deletions
38
modules/akka/src/main/scala/com/github/eikek/calev/akka/CalevActorScheduling.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.eikek.calev.akka | ||
|
||
import java.time.{Clock, ZonedDateTime} | ||
import java.util.concurrent.TimeUnit | ||
|
||
import scala.concurrent.duration.{Duration, FiniteDuration} | ||
|
||
import akka.actor.typed.ActorRef | ||
import akka.actor.typed.scaladsl.ActorContext | ||
import com.github.eikek.calev.CalEvent | ||
import com.typesafe.config.Config | ||
|
||
object CalevActorScheduling { | ||
|
||
implicit class CalevActorScheduling(ctx: ActorContext[_]) { | ||
def scheduleUpcoming[T]( | ||
calEvent: CalEvent, | ||
target: ActorRef[T], | ||
triggerFactory: ZonedDateTime => T, | ||
clock: Clock = Clock.systemDefaultZone() | ||
): Unit = new UpcomingEventProvider(clock)(calEvent).foreach { | ||
case (instant, delay) => | ||
ctx.scheduleOnce(delay, target, triggerFactory(instant)) | ||
} | ||
} | ||
|
||
implicit final private[akka] class ConfigOps(val config: Config) extends AnyVal { | ||
def getDurationMillis(path: String): FiniteDuration = | ||
getDuration(path, TimeUnit.MILLISECONDS) | ||
|
||
def getDurationNanos(path: String): FiniteDuration = | ||
getDuration(path, TimeUnit.NANOSECONDS) | ||
|
||
private def getDuration(path: String, unit: TimeUnit): FiniteDuration = | ||
Duration(config.getDuration(path, unit), unit) | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
modules/akka/src/main/scala/com/github/eikek/calev/akka/CalevInterceptor.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.eikek.calev.akka | ||
|
||
import java.time.{Clock, ZonedDateTime} | ||
|
||
import scala.reflect.ClassTag | ||
|
||
import akka.actor.typed.scaladsl.Behaviors | ||
import akka.actor.typed.{Behavior, BehaviorInterceptor, TypedActorContext} | ||
import com.github.eikek.calev.CalEvent | ||
import com.github.eikek.calev.akka.CalevActorScheduling.ConfigOps | ||
|
||
private[akka] class CalevInterceptor[B, T <: B: ClassTag]( | ||
clock: Clock, | ||
calEvent: CalEvent, | ||
triggerFactory: ZonedDateTime => T | ||
) extends BehaviorInterceptor[T, B] { | ||
|
||
override def aroundStart( | ||
ctx: TypedActorContext[T], | ||
target: BehaviorInterceptor.PreStartTarget[B] | ||
): Behavior[B] = | ||
scheduleUpcoming(target.start(ctx)) | ||
|
||
override def aroundReceive( | ||
ctx: TypedActorContext[T], | ||
msg: T, | ||
target: BehaviorInterceptor.ReceiveTarget[B] | ||
): Behavior[B] = | ||
scheduleUpcoming(target(ctx, msg)) | ||
|
||
private def scheduleUpcoming(target: Behavior[B]): Behavior[B] = Behaviors.setup { | ||
ctx => | ||
val config = ctx.system.settings.config | ||
val minInterval = config.getDurationMillis("akka.scheduler.tick-duration") * 4 | ||
|
||
CalevTimerScheduler.withCalevTimers(Some(minInterval), clock) { scheduler => | ||
scheduler.scheduleUpcoming(calEvent, triggerFactory) | ||
target | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
modules/akka/src/main/scala/com/github/eikek/calev/akka/CalevScheduler.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.eikek.calev.akka | ||
|
||
import java.time.Clock | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
import akka.actor.typed.Scheduler | ||
import com.github.eikek.calev.CalEvent | ||
|
||
class CalevScheduler(scheduler: Scheduler, clock: Clock = Clock.systemDefaultZone()) { | ||
private val upcomingEventProvider = new UpcomingEventProvider(clock) | ||
|
||
def scheduleUpcoming(calEvent: CalEvent, runnable: Runnable)(implicit | ||
executor: ExecutionContext | ||
): Unit = | ||
upcomingEventProvider(calEvent).foreach { case (_, delay) => | ||
scheduler.scheduleOnce(delay, runnable) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
modules/akka/src/main/scala/com/github/eikek/calev/akka/CalevTimerScheduler.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.github.eikek.calev.akka | ||
|
||
import java.time.{Clock, ZonedDateTime} | ||
|
||
import scala.concurrent.duration._ | ||
import scala.reflect.ClassTag | ||
|
||
import akka.actor.typed.Behavior | ||
import akka.actor.typed.scaladsl.{Behaviors, TimerScheduler} | ||
import com.github.eikek.calev.CalEvent | ||
|
||
object CalevTimerScheduler { | ||
|
||
def withCalevTimers[T]( | ||
minInterval: Option[FiniteDuration] = None, | ||
clock: Clock = Clock.systemDefaultZone() | ||
)(factory: CalevTimerScheduler[T] => Behavior[T]): Behavior[T] = | ||
Behaviors.withTimers { scheduler => | ||
factory(new CalevTimerSchedulerImpl[T](scheduler, clock, minInterval)) | ||
} | ||
|
||
def withCalendarEvent[B, T <: B: ClassTag]( | ||
calEvent: CalEvent, | ||
triggerFactory: ZonedDateTime => T, | ||
clock: Clock = Clock.systemDefaultZone() | ||
)(inner: Behavior[B]): Behavior[T] = | ||
Behaviors.intercept(() => | ||
new CalevInterceptor[B, T](clock, calEvent, triggerFactory) | ||
)(inner) | ||
|
||
} | ||
|
||
trait CalevTimerScheduler[T] { | ||
def scheduleUpcoming(calEvent: CalEvent, triggerFactory: ZonedDateTime => T): Unit | ||
} | ||
|
||
private[akka] class CalevTimerSchedulerImpl[T]( | ||
scheduler: TimerScheduler[T], | ||
clock: Clock, | ||
minInterval: Option[FiniteDuration] | ||
) extends CalevTimerScheduler[T] { | ||
|
||
private val upcomingEventProvider = | ||
new UpcomingEventProvider(clock, minInterval) | ||
|
||
def scheduleUpcoming(calEvent: CalEvent, triggerFactory: ZonedDateTime => T): Unit = | ||
upcomingEventProvider(calEvent) | ||
.foreach { case (instant, delay) => | ||
scheduler.startSingleTimer(triggerFactory.apply(instant), delay) | ||
} | ||
|
||
} |
Oops, something went wrong.