Stop Hook Is Not Being Called When Play Application Is Shutting Down

I am developing an application using Play Framework 2.5.5 and I'm having issues with adding a stop hook to my class. I have a class called Broom like following to schedule a cleaning job.

@ImplementedBy(classOf[Broom])
trait AbstractBroom

@Singleton
class Broom @Inject()(ActorSystem: ActorSystem,
                      ApplicationLifecycle: ApplicationLifecycle,
                      Conf: AbstractConf) extends AbstractBroom with Logging {
  private val enabled: Boolean             = Conf.getBoolean("enabled", defaultValue = true)
  private val initialDelay: FiniteDuration = Conf.getFiniteDuration("initialDelay", FiniteDuration(10, TimeUnit.SECONDS))
  private val interval: FiniteDuration     = Conf.getFiniteDuration("interval",     FiniteDuration(1, TimeUnit.DAYS))

  private val actor: ActorRef = ActorSystem.actorOf(Props(new BroomActor(Conf)), BroomActor.actorName)

  private val cancellable: Option[Cancellable] = {
    if (enabled) {
      val firstRun: LocalDateTime = LocalDateTime.now.plusSeconds(interval.toSeconds).withNano(0)

      Log.warn(s"Starting Broom scheduled to $firstRun...")

      val c: Cancellable = ActorSystem.scheduler.schedule(
        initialDelay,
        interval,
        actor,
        Wipe
      )

      Option(c)
    } else {
      None
    }
  }

  ApplicationLifecycle.addStopHook {
    () =>
      actor ! PoisonPill
      cancellable.foreach {
        c: Cancellable =>
          Log.warn("Shutting down Broom...")
          c.cancel()
      }
      ActorSystem.terminate()
  }
}

It just regularly sends Wipe message to BroomActor so it can perform some cleaning on old data. As I want to stop Broom when application is shutting down, I added a dependency to ApplicationLifecycle and called addStopHook method.

For starting Broom when application starts, I have a class named Modules in com.example package like following

class Modules extends AbstractModule {
  override def configure(): Unit = {
    bind(classOf[Broom]).asEagerSingleton()
  }
}

which is referenced in my configuration as following.

play.modules.enabled += "com.example.Modules"

I see starting message when application starts but I don't see stopping message while shutting down. I noticed that cleaning actually continues in the background even though my application is not running. I guess I might be leaking something.

I had pretty much same configuration in another Play 2.5.4 project of mine here https://github.com/mehmetakiftutuncu/quupNotificationsServer for Heartbeat class and it just works. What am I doing wrong for this one here?

Upvotes: 2

Views: 1135

Answers (1)

Mikesname
Mikesname

Reputation: 8901

This is a bug with Play 2.5.5, see this issue. You are probably best sticking with 2.5.4 until 2.5.6 is released.

Upvotes: 3

Related Questions