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/d2968b7bd66198e636d64b9d0e35ec6b to your computer and use it in GitHub Desktop.
Save semibratov/d2968b7bd66198e636d64b9d0e35ec6b to your computer and use it in GitHub Desktop.
Планировщик с долгоживущим актором
class ConfirmationCodeCleanTask(
val connection: DBConnection
) extends ServiceActor
with RegistrationConfirmation
with ActorLogging {
override def process: Receive = {
case ConfirmationCodeCleanTask.Start =>
deleteExpired()
}
}
object ConfirmationCodeCleanTask extends ClassActorName {
sealed trait Command
case object Start extends Command
case class Done(replyTo: ActorRef) extends Command
// safe constructor
def props(
connection: DBConnection
): ActorProps =
ActorProps(Props(new ConfirmationCodeCleanTask(connection)), name)
}
class UserScheduler(val connection: DBConnection, val settings: Settings) extends ServiceActor {
override def process: Receive = Actor.emptyBehavior
val codeCleanerProps: ActorProps = ConfirmationCodeCleanTask.props(connection)
val codeCleaner: ActorRef = context.actorOf(codeCleanerProps.props, codeCleanerProps.name)
override def preStart(): Unit = {
super.preStart()
initCodeCleaner()
}
def initCodeCleaner(duration: Duration = settings.Registration.clearPeriod): Unit = {
val finPeriod = FiniteDuration(duration.toSeconds, TimeUnit.SECONDS)
context.system.scheduler.schedule(finPeriod, finPeriod, codeCleaner, ConfirmationCodeCleanTask.Start)
}
}
object UserScheduler extends ClassActorName {
sealed trait Command
case object Start extends Command
// safe constructor
def props(
connection: DBConnection,
settings: Settings
): ActorProps =
ActorProps(Props(new UserScheduler(connection, settings)), name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment