Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@semibratov
Last active February 21, 2018 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semibratov/a0121fe73883a3b6216735ca20f75e63 to your computer and use it in GitHub Desktop.
Save semibratov/a0121fe73883a3b6216735ca20f75e63 to your computer and use it in GitHub Desktop.
Планировщик с одноразовым актором
class ConfirmationCodeCleanTask2(
val connection: DBConnection,
val replyTo: ActorRef
) extends ActionActor
with RegistrationConfirmation
with ActorLogging {
override def awaitingStart: Receive = {
case ConfirmationCodeCleanTask2.Start =>
val replyTo = sender()
deleteExpired().map(_ => ConfirmationCodeCleanTask2.Done) pipeTo self
case ConfirmationCodeCleanTask2.Done =>
replyTo ! ActionResult(Right(Unit))
stopAction()
}
}
object ConfirmationCodeCleanTask2 extends ClassActorName {
sealed trait Command
case object Start extends Command
case object Done extends Command
def props(
connection: DBConnection,
replyTo: ActorRef
): ActorProps =
ActorProps(Props(new ConfirmationCodeCleanTask2(connection, replyTo)), name)
}
class UserScheduler2(val connection: DBConnection, val settings: Settings) extends ServiceActor {
override def process: Receive = Actor.emptyBehavior
val shedulerServiceProps: ActorProps = UserSchedulerService2.props(connection)
val shedulerService: ActorRef = context.actorOf(shedulerServiceProps.props, shedulerServiceProps.name)
override def preStart(): Unit = {
super.preStart()
initCodeCleaner()
}
def initCodeCleaner(duration: Duration = settings.Registration.clearPeriod): Unit = {
val actor = createChild(
UserSchedulerService2.props(connection)
)
actor ! RegistrationRequestAction.Start
val finPeriod = FiniteDuration(duration.toSeconds, TimeUnit.SECONDS)
context.system.scheduler.schedule(finPeriod, finPeriod, shedulerService, UserSchedulerService2.RunConfCodeClean)
}
}
class UserSchedulerService2(val connnection: DBConnection) extends ServiceActor {
def cleanExpiredCodes(): Unit = {
val actor = createChild(
ConfirmationCodeCleanTask2.props(connnection, self)
)
actor ! ConfirmationCodeCleanTask2.Start
}
override def process: Receive = {
case RegistrationCommand => cleanExpiredCodes()
}
}
object UserSchedulerService2 extends ClassActorName {
sealed trait Command
case class RunConfCodeClean() extends Command
def props(connection: DBConnection): ActorProps =
ActorProps(Props(new UserSchedulerService2(connection)), name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment