I have a Scala play app which I configured a scheduled job using the docs and examples from the play website.
@Singleton
class InventorySyncTask @Inject()(
appConfigService: AppConfigService,
inventoryRepository: inventoryRepository,
actorSystem: ActorSystem,
lifecycle: ApplicationLifecycle
)(
implicit executionContext: RepositoryDispatcherContext
) {
private lazy val logger = Logger(this.getClass)
private lazy val intervalSeconds = appConfigService.getIntProperty("syncInvJobFrequencyNumSeconds")
actorSystem.scheduler.schedule(initialDelay = intervalSeconds.second, interval = intervalSeconds.second) {
logger.info("Inventory Sync Job starting...")
val future = inventoryRepository.syncIps()
val timeout = appConfigService.properties("timeoutExternalAPI")
val syncResult = Await.result(future, timeout.toInt millis)
syncResult match {
case Some(x) => logger.info(s"Inventory merged successfully: $x")
case _ => logger.info("Nothing merged")
}
logger.info("Inventory Sync Job completed")
}
// This is necessary to avoid thread leaks, specially if you are using a custom ExecutionContext
lifecycle.addStopHook { () =>
Future.successful(
Await.result(actorSystem.terminate(), 60.seconds)
)
}
}
in My module the task is setup to be eager:
// ....
bind(classOf[InventorySyncTask]).asEagerSingleton()
my scheduler is picking up a config value which is set to 86400 (1 day in seconds) so the initialDelay and the interval are set with 86400.seconds
Expected result is that the task would only run after the initial Delay (1 day) but when I run my tests it starts right away not obeying the initialDelay param.
Aucun commentaire:
Enregistrer un commentaire