Last active
February 21, 2018 19:20
-
-
Save semibratov/a0121fe73883a3b6216735ca20f75e63 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
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) | |
} |
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
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) | |
} | |
} |
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
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