Skip to content

Commit

Permalink
alerts-server: Add ShutdownableTaskRunner
Browse files Browse the repository at this point in the history
Using this class we can create task runners that work with
the ShutdownHandler to perform graceful shutdowns.
  • Loading branch information
AlexITC committed Jan 18, 2018
1 parent fa5bdc9 commit 33a987c
Showing 1 changed file with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.alexitc.coinalerts.tasks

import akka.actor.ActorSystem
import com.alexitc.coinalerts.core.ShutdownHandler
import org.slf4j.LoggerFactory

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import scala.util.control.NonFatal

trait ShutdownableTaskRunner {

private val logger = LoggerFactory.getLogger(this.getClass)

protected def shutdownHandler: ShutdownHandler

protected def actorSystem: ActorSystem

protected def initialDelay: FiniteDuration

protected def interval: FiniteDuration

protected def run(): Future[Unit]

@volatile
private var running = false

protected def register() = {
logger.info("Registering task")

shutdownHandler.addShutdownHook(() => Future {

if (running) {
logger.info("Waiting for task to complete")
}

// if we are shutting down, do we care no making a mess with threads? I hope we don't
scala.concurrent.blocking {
while (running) {
Thread.sleep(1000)
}
}
})

val _ = actorSystem.scheduler.schedule(
initialDelay = initialDelay,
interval = interval) { execute() }
}

private def execute() = {
if (!shutdownHandler.isShuttingDown) {
running = true

logger.info("Running task")

run()
.recover {
case NonFatal(ex) =>
logger.error("Failed to run task", ex)
}
.foreach { _ =>
running = false
}
}
}
}

0 comments on commit 33a987c

Please sign in to comment.